【问题标题】:"Error while parsing the string" when using RPy2 and dplyr使用 RPy2 和 dplyr 时出现“解析字符串时出错”
【发布时间】:2015-09-24 01:24:55
【问题描述】:

我正在使用 R 进行一些数据操作。这是一个主要使用 Python 的大型项目的一部分,所以我没有直接使用 R,而是使用 RPy2。在我进入 dplyr 部分之前一切正常。

这行得通:

from rpy2.robjects import r

Rcode = '''library(RODBC)
library(dplyr)
# ...
# a bunch of R code that fetches data from SQL Server
# ...'''.format(db_name = 'foo')
print r(Rcode)

这给了我想要的数据。

但是当我尝试用 dplyr 做一些数据操作时,像这样:

from rpy2.robjects import r

Rcode = '''library(RODBC)
library(dplyr)
# ...
# a bunch of R code that fetches data from SQL Server
# ...
myData <- myData
    %>% group_by(someDataField)'''.format(db_name = 'foo')
print r(Rcode)

我收到一个错误:

Traceback (most recent call last):
  File "myScript.py", line 53, in <module>
    print r(Rcode)
  File "C:\Python27\lib\site-packages\rpy2\robjects\__init__.py", line 268, in __call__
    p = rinterface.parse(string)
ValueError: Error while parsing the string.

我尝试转义 % 符号、转义 > 符号并将它们放在引号内,但没有任何效果。

我错过了什么?

我已经看到关于同一错误的其他问题(例如 herehere),但它们没有帮助。

我正在使用 Python 2.7.10 和 RPy2 2.5.6。

【问题讨论】:

  • 不应该是 print r(R_code) 而不是 print r(Rcode)?
  • 是的 - 抱歉,刚刚修复了它(这是对实际代码的简化,我在这里打错了)。
  • 您是否尝试过类似的操作:myData % 函数中?
  • 做到了。谢谢!您可能应该将其作为答案,以便我接受。
  • 太棒了!我会这样做的

标签: python r dplyr rpy2


【解决方案1】:
myData <- group_by(MyData,someDataField)

【讨论】:

  • 这说明不了什么
猜你喜欢
  • 1970-01-01
  • 2017-08-27
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-12-16
  • 2012-10-04
  • 2018-04-16
  • 2018-03-14
相关资源
最近更新 更多