1

I have been following the instructions on Working with R's OOPS in the rpy2 documentation here: https://rpy2.readthedocs.io/en/version_2.8.x/robjects_oop.html. I am trying to create a Python class to call the function rfsrc in the R package randomForestSRC.

Under what conditions does this work? Does rpy2 have a fixed list of packages that it allows you to access and if so is randomForestSRC on this list?

When I run the code below from a Jupyter Notebook (Python 3, R 3.5.1), I get the error: Error in (function (f, signature = character(), where = topenv(parent.frame()), : no generic function found for 'rfsrc'

import rpy2.robjects.packages as rpackages
from rpy2.robjects.vectors import StrVector

utils = rpackages.importr('utils')
utils.chooseCRANmirror(ind=1) # select the first mirror in the list
packnames = ('randomForestSRC', 'survival', 'tidyverse', 'magrittr', 'ggRandomForests', 'mlr')
utils.install_packages(StrVector(packnames))

from rpy2.robjects.packages import importr
randomForestSRC = importr('randomForestSRC')
from rpy2.robjects.methods import RS4Auto_Type
import six

class rfsrc(six.with_metaclass(RS4Auto_Type)):
    __rname__ = 'rfsrc'
    __rpackagename__ = 'randomForestSRC'

What else do I need to do to get this to work?

I also tried the manual method as shown below and got the same error.

import rpy2.robjects as robjects
import rpy2.rinterface as rinterface
from rpy2.robjects.packages import importr

lme4 = importr("randomForestSRC")
getmethod = robjects.baseenv.get("getMethod")

StrVector = robjects.StrVector

class rfsrc(robjects.methods.RS4):

    _coef = getmethod("rfsrc", 
                  signature = StrVector(["rfsrc", ]),
                  where = "package:randomForestSRC")

    def _call_get(self):
        return self.do_slot("call")
    def _call_set(self, value):
        return self.do_slot("call", value)
    call = property(_call_get, _call_set, None, "Get or set the RS4 slot      'call'.")

    def coef(self):
        """ fitted coefficients """
        return self._coef(self)

1 Answer 1

1

There are several questions. I picked the one that might be the origin of all others.

Does rpy2 have a fixed list of packages that it allows you to access and if so is randomForestSRC on this list?

rpy2 is only able to use the R packages available with your R installation. The documentation has something about installing R packages from Python (also, note the URL for the documentation of rpy2-2.9.x): https://rpy2.github.io/doc/v2.9.x/html/robjects_rpackages.html#installing-removing-r-packages

Edit:

The question was edited to now focus on the error message no generic function found for 'rfsrc'. The error is coming from R because no generic function called "rfsrc" was found.

I think that rfsrc is just a regular R function (not a generic, see https://github.com/cran/randomForestSRC/blob/master/R/rfsrc.R ). Also, I am not the packages randomForest uses S4 much, and the rpy2 features you are trying to use are specifically designed for R's S4 OOP system.

Sign up to request clarification or add additional context in comments.

3 Comments

Thanks. I have edited the code above to add the installation of packages, but I am still getting the error "Error in get0(cname, where, inherits = inherits) : invalid 'envir' argument". The packages were already installed in my copy of R and I had been loading them with R library calls, using the R Magic operator %%R.
Thanks @lgautier. I have edited the question again as I am now getting the same error using both methods: Error in (function (f, signature = character(), where = topenv(parent.frame()), : no generic function found for 'rfsrc'. So my main question now is: does this error mean it is not possible to use this method to call the function rfsrc in the package randomForestSRC?
Answered as an addendum to the existing answer.

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.