"Error while parsing the string" 使用 RPy2 和 dplyr 时

"Error while parsing the string" when using RPy2 and dplyr

我正在使用 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.

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

我错过了什么?

我看到了关于相同错误的其他问题(例如 and here),但它们没有帮助。

我正在使用 Python 2.7.10 和 RPy2 2.5.6。

myData <- group_by(MyData,someDataField)