Search code examples
pythonrrpy2

install local package with rpy2 - invalid package


I'm trying to run a local r package and store the result it returned.

I've put the R package in the same directory as the python script. I've imported importr from rpy2.robjects.packages and had imported the utils package.

When I call the install.packages like this

from rpy2.robjects.packages import importr
utils = rpackages.importr('utils')
utils.install_packages('impactr_3.gz', repos = NULL, type="source")

The error produced was:

NameError: name 'NULL' is not defined.

How do I load this local R package with rpy2?


Solution

  • Unless you define it, Python will not know any NULL (hence the error). If you want to use R's NULL, you can find it as rpy2.robjects.NULL or rpy2.rinterface.NULL.

    Otherwise, the documentation for the R function you want to use indicates that paths can also be specified with the prefix file://:

    utils.install_packages('file://impactr_1.0.1.tar.gz', type = "source")