9

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?

Samo
  • 2,065
  • 20
  • 41
  • Simply point [library paths](https://stackoverflow.com/q/2698269/1422451) to the non-conda R's library or set needed [environment variables](https://stackoverflow.com/q/30921570/1422451) to point to needed library. – Parfait Jul 23 '18 at 19:51
  • @Parfait Tried to do that but do not know how obviously, since it still does not work. Is there maybe an article or blog post detailing this? It seems this is all very well document and easy if you are primary using Python and would want to add some R stuff. If you are a pure R user with a lot of custom packages and default R installation there seems to be no details on how to set things up... – Samo Jul 25 '18 at 20:34
  • Did you try specifying R packages in `importr` call: `importr("my_package", lib_loc="/path/to/non-conda/R/lib")`? You can find this path by opening non-conda R session and call `.libPaths()`. – Parfait Jul 26 '18 at 16:41
  • What's the downside of using the r conda package? – Nehal J Wani Aug 01 '18 at 19:04
  • 1
    If I install RPy2 with conda it also installs R (another version) changes R home folder and all sorts of stuff. I am an R user(meaning I have a working R installation with specific version that needs to be maintained on a system wide basis since I have many many many R jobs running and many proprietary R packages written and used. Now, I need to use Python and call functions from my proprietary R packages from Python. The issue I have is that it looks like people using RPy2 mostly use Python and just need to call some R functions and they do not care where and which version of R they have... – Samo Aug 02 '18 at 18:00

1 Answers1

2

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.

Maria
  • 327
  • 4
  • 13
qjgods
  • 958
  • 7
  • 7
  • I have a big problem when installing with _conda install rpy2_ and is that it broke my R_PATH to the condo environment. then I got error like this one: `ValueError: r_home is None. Try python -m rpy2.situation` which can be solved by: `import os` and `os.environ['R_HOME'] = '/home/my_user/anaconda3/envs/my_envi/lib/R'` . Do you know how to do it globally? – Lucho May 11 '20 at 21:53
  • 1
    @Lucho you can set a global environment variable by something like this: https://stackoverflow.com/questions/1641477/how-to-set-environment-variable-for-everyone-under-my-linux-system – qjgods May 12 '20 at 02:04
  • "The environment variable for R (R_HOME and PATH) should be properly set up before you install the rpy2." -- What is "properly"? Could you please specify this in more detail? Thanks! – András Aszódi Aug 17 '22 at 09:36