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.