在conda虚拟环境中使用`rpy2`为`R`安装包?

时间:2017-02-09 00:18:15

标签: python r environment-variables conda rpy2

我一直在努力让我的环境保持非冗余和清洁,因此我创建了一个R环境并清除了计算机上所有其他现有R

该环境称为r-conda,它位于:

/Users/jespinoz/anaconda/envs/r-conda/bin/R

我意识到我没有安装rpy2并通过conda安装它想要安装新版本的R和所有r-essentials我想要的因为我已经拥有一个完美的R环境,所以我想要。

我意识到我可以为rpy2环境中关联的Python安装R conda

source activate r-conda
pip install rpy2
source deactivate

但并非所有路径都排成一行

如何让rpy2识别我R环境中的所有r-conda相关文件和路径?

我在尝试导入数据包时没有正确找到文件:

os.environ['R_HOME'] = "/Users/jespinoz/anaconda/envs/r-conda/bin/R"
from rpy2.robjects.packages import importr
importr("dynamicTreeCut")

---------------------------------------------------------------------------
ImportError                               Traceback (most recent call last)
<ipython-input-29-8b0a88dfe12d> in <module>()
      1 # os.environ['R_HOME'] = '/Users/jespinoz/anaconda/envs/r-conda/bin/'
      2 os.environ['R_HOME'] = "/Users/jespinoz/anaconda/envs/r-conda/bin/R"
----> 3 from rpy2.robjects.packages import importr
      4 importr("dynamicTreeCut")

/Users/jespinoz/anaconda/lib/python3.6/site-packages/rpy2/robjects/__init__.py in <module>()
     14 import itertools
     15 from datetime import datetime
---> 16 import rpy2.rinterface as rinterface
     17 import rpy2.rlike.container as rlc
     18 

/Users/jespinoz/anaconda/lib/python3.6/site-packages/rpy2/rinterface/__init__.py in <module>()
     90 del(os)
     91 
---> 92 from rpy2.rinterface._rinterface import (baseenv,
     93                                          emptyenv,
     94                                          endr,

ImportError: dlopen(/Users/jespinoz/anaconda/lib/python3.6/site-packages/rpy2/rinterface/_rinterface.cpython-36m-darwin.so, 2): Library not loaded: @rpath/R/lib/libR.dylib
  Referenced from: /Users/jespinoz/anaconda/lib/python3.6/site-packages/rpy2/rinterface/_rinterface.cpython-36m-darwin.so
  Reason: image not found

通过将此添加到我的~/.bash_profile修复了该错误,但生成了类似的新错误: 我试了一下,错误改变了:

export LD_LIBRARY_PATH="/Users/jespinoz/anaconda/envs/r-conda/lib/R/lib/:$LD_LIBRARY_PATH" 

>>> from rpy2.robjects.packages import importr
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/Users/jespinoz/anaconda/lib/python3.6/site-packages/rpy2/robjects/__init__.py", line 16, in <module>
    import rpy2.rinterface as rinterface
  File "/Users/jespinoz/anaconda/lib/python3.6/site-packages/rpy2/rinterface/__init__.py", line 92, in <module>
    from rpy2.rinterface._rinterface import (baseenv,
ImportError: dlopen(/Users/jespinoz/anaconda/lib/python3.6/site-packages/rpy2/rinterface/_rinterface.cpython-36m-darwin.so, 2): Library not loaded: libicuuc.54.dylib
  Referenced from: /Users/jespinoz/anaconda/lib/python3.6/site-packages/rpy2/rinterface/_rinterface.cpython-36m-darwin.so
  Reason: image not found

所以我尝试了这个,然后得到了同样的错误:

export LD_LIBRARY_PATH="/Users/jespinoz/anaconda/envs/r-conda/lib/R/lib/:/Users/jespinoz/anaconda/pkgs/icu-54.1-0/lib/:$LD_LIBRARY_PATH"

如果我使用conda install rpy2,即使我的主要conda环境的默认版本为Python=3.5.2,也要安装Python=3.6。 @asmeurer在我的Python=3.6环境中安装rpy2时提出了指定r-conda的建议,但现在它看起来像是一个包冲突的错误:

(r-conda) jespinozlt-osx:~ jespinoz$ conda install rpy2 python=3.6
Fetching package metadata .............
Solving package specifications: .


UnsatisfiableError: The following specifications were found to be in conflict:
  - python 3.6*
  - r-permute
  - rpy2
Use "conda info <package>" to see the dependencies for each package

1 个答案:

答案 0 :(得分:3)

你应该conda install rpy2而不是pip安装它。此外,保持环境激活。您应该将python和要使用的任何Python软件包安装到同一个R环境中,以便一切都在一个环境中完成。