This question relates to python variable to R and perhaps also to this python objects to rpy2 but none of the two completely overlaps and the first one is actually unanswered.
My question is actually very simple. I have a string, say:
In [21]: strg
Out[21]: 'I want to go home'
and I want to pass it to R through robjects.r(''' ''')
like this, for example:
robjects.r('''
test <- gsub("to", "",strg)
''')
but of course, when I run this I obtain: Error in gsub("me", "", strg) : object 'strg' not found
.
I have not used rpy2
much (as obvious) but I guess is related to the environments in which R and Python see the objects.
I have tried a few things, like transforming the string strg
to an robject
first and then feed it to robjects.r(''' ''')
but I get the same error message. Overall, I do not know how do this so that strg
is seen at the R
environment.
Any help is much appreciated!
Thanks in advance for your time!
Just add the
strg
value to the command string:or, by using
.format
:Do note that you'll need to watch out for backslashes, see the question here