Question

Here's what should and does happen using rpy2.robjects.packages.importr for base R-packages (e.g. stats):

>>> from rpy2.robjects.packages import importr
>>> importr('stats')
<rpy2.robjects.packages.SignatureTranslatedPackage object at 0x7f3810>

but with an external package (e.g. ggplot2) this is the result:

>>> importr('ggplot2')
Warning message:
In library(package, lib.loc = lib.loc, character.only = TRUE, logical.return = TRUE,  :
  there is no package called 'ggplot2'
Error in .Primitive("as.environment")("package:ggplot2") : 
  no item called "package:ggplot2" on the search list
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/Library/Frameworks/Python.framework/Versions/7.0/lib/python2.7/site-packages/rpy2/robjects/packages.py", line 100, in importr
    env = _as_env(rinterface.StrSexpVector(['package:'+name, ]))
rpy2.rinterface.RRuntimeError: Error in .Primitive("as.environment")("package:ggplot2") : 
  no item called "package:ggplot2" on the search list

A complication is that in my home directory I have the file .Renviron define my user library location (where, for instance, ggplot2 libs are), and I have no problems with either of the R-commands library() or require() using R and Rscript. The path looks something like this:

R_LIBS_USER="/path/to/my/packages"

So my question is why my user-library path excluded from the "search list" Rpy2 uses? Or, rather, how do I direct Rpy2 to look in the R_LIBS_USER path as well?

I assume the problem stems from the environment Rpy2 uses, but my ignorance is high in that regard.

R: 2.13.0
Platform: x86_64-apple-darwin9.8.0/x86_64 (Mac, 10.6, 64-bit)

replicated with

Rpy2: 2.1.8, 2.2.1 (dev)

I use R, ggplot2, and python regularly, so any insight is very much welcome.

Was it helpful?

Solution

By default, rpy2 is initializing in "vanilla" mode, and this ignores R_LIBS and friends.

>>> import rpy2.rinterface 
>>> rpy2.rinterface.get_initoptions()
('rpy2', '--quiet', '--vanilla', '--no-save')
>>> 

You can use 'rinterface.set_initoptions()' to change those.

For example:

import rpy2.rinterface as ri
ri.set_initoptions(('rpy2', '--verbose', '--no-save'))
ri.initr()

# from now on, just import the rest of rpy2 modules without thinking of the above.
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top