Search code examples
pythonrrpy2

Using rpy2, how to call a fuction with a "." in the variable name?


I would like to use rpy2 to call the regsubsets function from the R leaps package. Sine there seems to be no python equivalent (at least non-that I have found).

import pandas
import rpy2.robjects as robjects
from rpy2.robjects import pandas2ri
import rpy2.robjects.packages as rpackages

pandas2ri.activate()
leaps = rpackages.importr("leaps")


x = pandas.DataFrame(np.random.randn(10,10))
y = pandas.DataFrame(np.random.randn(10))

leaps.regsubsets(x=x, y=y, nbest=10, nvmax=3, really.big=T)

Calling that function does require the argument really.big=T. That of course does not work, because in python the . has a direct function as opposed to R. (i get a "SyntaxError: keyword can't be an expression"). Can anyone tell me how to call it from rpy2?

I have tried to do it with kwargs like leaps.regsubsets({"x":x ... "really.big":T}) but that also does not work and I also do not know what "T" is in R.

OK, I found out that the "." gets transleted to "_". but sadly it still does not work, somehow the argument does not get passed on.

leaps.regsubsets(x=X, y=Y, nbest=10, nvmax=3, really_big=True)

still gives me the error: RRuntimeError:

Error in leaps.exhaustive(a, really.big = really.big) : 
  Exhaustive search will be S L O W, must specify really.big=T

Solution

  • The documentation link in the comments in for rpy2-2.2.x that is quite old. Documentation for the current release is at:

    https://rpy2.github.io/doc/v3.0.x/html/robjects_functions.html

    Otherwise the documentation is rather clear about the near-impossibility of extracting parameters that can be accepted in the R ellipsis (...) (which is what @CristiFati is pointing out in the comments).

    You can used syntactically-invalid parameter names with **. For example here:

    leaps.regsubsets(x=x, y=y,
                     nbest=10, nvmax=3,
                     **{'really.big': true})