[英]how to use rpy2 within a packrat environment?
I try to use an R package that I have installed using the R package 'packrat' that allow to create a virtual environment similar to virtuanlenv in python. 我尝试使用我使用R包“ packrat”安装的R包,该R包允许在python中创建类似于virtuanlenv的虚拟环境。 But I do not succeed.
但是我没有成功。
Within a console using RI can run successfully the following code: 在使用RI的控制台中,可以成功运行以下代码:
cd /path/to/packrat/environment
R # this launch a R console in the packrat environment
library(mycustompackage)
result = mycustompackage::myfunc()
q()
I would like to do the same using rpy2, but I'm unable to activate the packrat environment. 我想使用rpy2做同样的事情,但是我无法激活packrat环境。 Here follow what I've tested unsuccessfully.
请遵循我未成功测试的内容。
from rpy2.robjects import r
from rpy2.robjects.packages import importr
packrat_dir = r.setwd('/path/to/packrat/environment')
importr('mycustompackage')
result = r.mycustompackage.myfunc()
But it fails at 'importr' because it cannot find the package 'mycustompackage'. 但是它在“进口商”处失败,因为找不到包“ mycustompackage”。 Either unsuccessfull :
要么不成功:
importr('mycustompackage', lib_loc='/path/to/packrat/environment')
Neither: 无论是:
os.environ['R_HOME'] = '/path/to/packrat/environment'
importr('mycustompackage', lib_loc ='/path/to/packrat/environment')
Any suggestion on how to use rpy2 with packrat environments? 关于如何在packrat环境中使用rpy2的任何建议?
I am not familiar with the R package packrat
, but I am noticing that the bash + R and Python/rpy2 code have a subtle difference that might matter a lot: in the bash + R case, when R is starting it is already in your packrat project directory whereas in the Python / rpy2 case R is starting from a different directory and is moved to the packrat project directory using setwd()
. 我不熟悉R包
packrat
,但是我注意到bash + R和Python / rpy2代码之间的细微差别可能很重要:在bash + R情况下,当R启动时,它已经存在packrat项目目录,而在Python / rpy2情况下,R从另一个目录开始,并使用setwd()
移动到packrat项目目录。
I am reading that packrat is using a file .Rprofile
( https://rstudio.github.io/packrat/limitations.html ), evaluated by R at startup time if in the current directory. 我正在阅读packrat正在使用文件
.Rprofile
( https://rstudio.github.io/packrat/limitations.html ),如果在当前目录中,则R在启动时会对其进行评估。 I suspect that the issue is down to how packrat
is used rather than an issue with rpy2. 我怀疑问题归结于
packrat
的使用方式,而不是rpy2的问题。
Very good remark (hidden file = forgotten file). 很好的评论(隐藏文件=忘记文件)。 I found out how to make it running:
我发现了如何使其运行:
from rpy2.robjects import r
from rpy2.robjects.packages import importr
# Init the packrat environment
r.setwd('/path/to/packrat/environment')
r.source('.Rprofile')
# use the packages it contains
importr('mycustompackage')
result = r.myfunc()
lgautier , you made my day, thanks a lot. lgautier ,我过得很愉快 ,非常感谢。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.