Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
600 views
in Technique[技术] by (71.8m points)

python - Use `rpy2` with packages installed for `R` in conda virtual environment?

I have been trying to keep my environment non-redundant and clean so I made an R environment and wiped out all other existing Rs on my computer.

That environment is called r-conda and it is in:

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

I realized I didn't have rpy2 installed and to install it through conda it wanted to install the a new version of R and all of the r-essentials which I don't want since I already have a perfectly working R environment.

I realized I could install rpy2 for the Python associated within the R conda environment:

source activate r-conda
pip install rpy2
source deactivate

But not all of the paths are lined up

How can I make rpy2 recognize all of my R associated files and paths in my r-conda environment?

It's not finding the files correctly when I am trying to import packges:

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

Fixed that error by adding this to my ~/.bash_profile but generated a similar new error: I gave this a try, and the error changed:

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

So I tried this, then got the same error:

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"

If I use conda install rpy2 it wants to install a Python=3.5.2 even though my default version of my main conda environment is Python=3.6. @asmeurer gave a suggestion to specify Python=3.6 when installing rpy2 in my r-conda environment but now it's looking like a package conflicting error:

(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
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

You should conda install rpy2 instead of pip installing it. Also, keep the environment activated. You should conda install python and any Python packages you want to use into the same R environment, so that everything is done in a single environment.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...