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)
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.