6

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
O.rka
  • 29,847
  • 68
  • 194
  • 309

1 Answers1

4

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.

asmeurer
  • 86,894
  • 26
  • 169
  • 240
  • When I `conda install rpy2` from with my `r-conda` environment, it wants to install another version of `python` even though the default `python` for that environment is my main version `python=3.6`. Is my only option to either install 2 versions of R, 2 versions of Python, or merge them into the same environment? I feel like there is something I can do with my paths and environment variables to tell `rpy2` where my `R` files are located. – O.rka Feb 10 '17 at 17:29
  • I'm not sure why it does that, but you can force it to keep the python version you want by including it in the install command, like `conda install rpy2 python=3.6`. – asmeurer Feb 10 '17 at 18:09
  • I didn't know you could do that. Nice, thanks! Though, it looks like I'm getting some conflicting errors (updating the question b/c formatting) ` – O.rka Feb 10 '17 at 18:11
  • I guess it means r-permute needs to be updated. You'll have to bug the Continuum folks to update it (I don't work there anymore). – asmeurer Feb 11 '17 at 17:49