Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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
like image 599
O.rka Avatar asked Feb 09 '17 00:02

O.rka


People also ask

Does rpy2 need R installed?

rpy2 will typically require an R version that is not much older than itself. This means that even if your system has R pre-installed, there is a chance that the version is too old to be compaible with rpy2. At the time of this writing, the latest rpy2 version is 2.8 and requires R 3.2 or higher.

Is rpy2 discontinued?

Rpy2 (discontinued, unstable): provide access to the R software environment for statistical computing and graphics.

What version of R does rpy2 use?

First up, install the necessary packages. You must have Python >=3.7 and R >= 4.0 installed to use rpy2 3.5. 2. Once R is installed, install the rpy2 package by running pip install rpy2 .

What is rpy2 package?

rpy2 is an interface to R running embedded in a Python process. on Pypi. Questions and issues.


1 Answers

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.

like image 77
asmeurer Avatar answered Oct 17 '22 09:10

asmeurer