Skip to content Skip to sidebar Skip to footer

Prepare A Python String For R Using Rpy2

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

Solution 1:

Just add the strg value to the command string:

robjects.r('''

test <- gsub("to", "",''' + strg + ''')

''')

or, by using .format:

robjects.r('''

test <- gsub("to", "",%s)

'''.format(strg))

Do note that you'll need to watch out for backslashes, see the question here


Solution 2:

I'd recommend you to create an function, as the R function exposed by rpy2 can be called just as if it was a Python function.

my_func = robjects.r('''
function(strg) {
    test <- gsub("to", "",strg)
    test
}
''')

my_func(strg)

Post a Comment for "Prepare A Python String For R Using Rpy2"