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)