简体   繁体   English

RPY2:导入程序因.Renviron而失败

[英]RPY2: importr fails with .Renviron

Here's what should and does happen using rpy2.robjects.packages.importr for base R-packages (eg stats ): 以下是使用rpy2.robjects.packages.importr进行基本R-packages(例如stats )应该和确实发生的事情:

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

but with an external package (eg ggplot2 ) this is the result: 但是使用外部包(例如ggplot2 ),结果如下:

>>> 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 . 一个复杂的问题是,在我的主目录中,我有文件.Renviron定义我的用户库位置(例如, ggplot2 libs所在的位置),我对R-commands library()require()任何一个都没有问题RRscript 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? 所以我的问题是为什么我的用户库路径被排除在Rpy2的“搜索列表” Rpy2 Or, rather, how do I direct Rpy2 to look in the R_LIBS_USER path as well? 或者,我如何指导Rpy2查看R_LIBS_USER路径呢?

I assume the problem stems from the environment Rpy2 uses, but my ignorance is high in that regard. 我认为这个问题源于Rpy2使用的环境,但我的无知在这方面很高。

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. 我经常使用R,ggplot2和python,因此非常欢迎任何见解。

By default, rpy2 is initializing in "vanilla" mode, and this ignores R_LIBS and friends. 默认情况下,rpy2正在“vanilla”模式下初始化,这会忽略R_LIBS和朋友。

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

You can use 'rinterface.set_initoptions()' to change those. 您可以使用'rinterface.set_initoptions()'来更改它们。

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.

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM