Skip to content Skip to sidebar Skip to footer

What Object To Pass To R From Rpy2?

I'm unable to make the following code work, though I don't see this error working strictly in R. from rpy2.robjects.packages import importr from rpy2 import robjects import numpy a

Solution 1:

This is one manifestation (see this other related issue for example) of the same underlying issue: R expressions are evaluated lazily and can be manipulated within R and this leads to idioms that do not translate well (in Python expression are evaluated immediately, and one has to move to the AST to manipulate code).

An answers to the second part of your question. In R, substitute(rep(.2, 1000)) is passing the unevaluated expression rep(.2, 1000) to substitute(). Doing in rpy2

substitute('rep(.2, 1000)')`

is passing a string; the R equivalent would be

substitute("rep(.2, 1000)")

The following is letting you get close to R's deparse(substitute()):

from rpy2.robjects.packages import importr
base = importr('base')
from rpy2 import rinterface

# expression
e = rinterface.parse('rep(.2, 1000)')
dse = base.deparse(base.substitute(e))

>>> len(dse)
1
>>> print(dse) # not identical to R
"expression(rep(0.2, 1000))"

Currently, one way to work about this is to bind R objects to R symbols (preferably in a dedicated environment rather than in GlobalEnv), and use the symbols in an R call written as a string:

from rpy2.robjects import Environment, reval

env = Environment()
for k,v in (('y', y), ('xreg', X), ('order', robjects.IntVector((1, 0, 0)))):
    env[k] = v

# make an expression
expr = rinterface.parse("forecast.Arima(y, xreg=X, order=order)")
# evaluate in the environment
res = reval(expr, envir=env)

This is not something I am happy about as a solution, but I have never found the time to work on a better solution.

edit: With rpy2-2.4.0 it becomes possible to use R symbols and do the following:

RSymbol = robjects.rinterface.SexpSymbol
pairlist = (('x', RSymbol('y')),
            ('xreg', RSymbol('xreg')),
            ('order', RSymbol('order')))
res = forecast.Arima.rcall(pairlist,
                           env)

This is not yet the most intuitive interface. May be something using a context manager would be better.


Solution 2:

there is a way to just simply pass your variables to R without sub-situations and return the results back to python. You can find a simple example here https://stackoverflow.com/a/55900840/5350311 . I guess it is more clear what you are passing to R and what you will get back in return, specially if you are working with For loops and large number of variables.


Post a Comment for "What Object To Pass To R From Rpy2?"