4

I'm using R to do some data manipulation. This is a part of a larger project that is mostly in Python, so I'm not using R directly but RPy2 instead. Things work fine until I get to the dplyr part.

This works:

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)

That gives me the data I want.

But when I try to do some data manipulation with dplyr, like this:

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)

I get an error:

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.

I've tried escaping the % signs, escaping the > sign, and putting them within quotation marks, but nothing worked.

What am I missing?

I've seen the other questions about the same error (like here and here) but they didn't help.

I'm using Python 2.7.10 and RPy2 2.5.6.

5
  • Shouldn't it be print r(R_code) instead of print r(Rcode)? Commented Jul 6, 2015 at 19:20
  • Yes - sorry, just fixed it (this is a simplification of the actual code and I mistyped here). Commented Jul 6, 2015 at 19:23
  • 1
    did you try to do something like : myData <- group_by(MyData,someDataField), just to see if the error is really in the %>% function ? Commented Jul 6, 2015 at 19:31
  • 1
    That did it. Thanks! You should probably make it an answer so I can accept it. Commented Jul 6, 2015 at 19:35
  • Great! I will do that Commented Jul 6, 2015 at 19:36

1 Answer 1

1
myData <- group_by(MyData,someDataField)
Sign up to request clarification or add additional context in comments.

1 Comment

this explains nothing

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.