Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Install and use RPy2 (using conda) so that it uses default R installation in /usr/lib/R R

Tags:

python

r

conda

rpy2

I want to call functions from my R packages in Python using RPy2. I installed RPy2 using conda and realized it installed a fresh copy of R inside conda... I don't want that. I just want to have and use one R, the default one in /usr/lib/R.

How to do that? How to force conda and Python and RPy2 to use default R installed in /usr/lib/R?

like image 335
Samo Avatar asked Jul 23 '18 19:07

Samo


People also ask

Does rpy2 require R installation?

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.

Can you install R packages with conda?

The R language packages are available to install with conda at http://repo.anaconda.com/pkgs/r/. You can install any of these R language packages into your current environment with the conda command conda install -c r package-name .

How do I use rpy2?

Use rpy2 to import R objects To add a code cell, click the notebook file's add Insert a cell below button. Click play_arrow Run the selected cells and advance. Python stores an R pi object. To print the value of pi, in a new code cell, enter pi[0] and click play_arrow Run the selected cells and advance.

What is rpy2 in Python?

rpy2 is an interface to R running embedded in a Python process. on Pypi. Questions and issues. Consider having a look at the documentation. Otherwise questions should preferably be asked on the rpy mailing-list on SourceForge, or on StackOverflow.


1 Answers

Do not use the conda install to install the rpy2, just use the pip install rpy2. Here are some additional packages you may need to install before the rpy2:

conda install -y PyHamcrest
sudo apt-get install -y libreadline6-dev
pip install rpy2

Some notes:

  1. which pip should refer to anaconda's path.

  2. The environment variable for R (R_HOME and PATH) should be properly set up before you install the rpy2.

  3. After the installation, you may encounter an error when calling import rpy2.robjects as robjects:

    RRuntimeWarning: Error: package or namespace load failed for ‘stats’ in dyn.load(file, DLLpath = DLLpath, ...): unable to load shared object '/usr/local/lib/R/library/stats/libs/stats.so': libRlapack.so: cannot open shared object file: No such file or directory

To solve this, I found a solution in How I solved the error - libRlapack.so: cannot open shared object file: No such file or directory

You need to locate your libRlapack.so file (in my case this file is in /usr/local/lib/R/lib/), or the following command should show the path to this file:

R CMD ldd /usr/local/lib/R/library/stats/libs/stats.so

and then write this path to the /etc/ld.so.conf.d/libR.conf and then run ldconfig:

echo "/usr/local/lib/R/lib/" >> /etc/ld.so.conf.d/libR.conf && ldconfig

That should fix the problem.

like image 151
qjgods Avatar answered Sep 22 '22 03:09

qjgods