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?
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.
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 .
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.
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.
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:
which pip should refer to anaconda's path.
The environment variable for R (R_HOME and PATH) should be properly set up before you install the rpy2.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With