installing rpy2 with different R version already installed

The easiest way to install a python package is to use easy_install (or pip), so to install rpy2, type:

easy_install rpy2

To test that it is properly installed, type:

import rpy2;
from rpy2 import robjects; robjects.r("version")

If this code works, you should be able to import an existing R package. Yet, you may get this error:

Error in sub("[[:blank:]]*([[:alnum:]]+)", "\\1", dp) :
7 arguments passed to .Internal(sub) which requires 8
line 22, in RObjectMixin
__show = rpy2.rinterface.baseenv.get("show")
LookupError: 'show' not found

It took me a while to understand that this kind of error comes from the fact that rpy2 was compiled using a given R version, and that the current R version has changed (or is simply different). If you installed rpy2 while you were using R.2.10 and then you install R.2.11 then you will need to recompile rpy2 with the proper version.

To try out the following solution, you can use a virtual environment:

virtualenv testrpy2
cd testrpy2
source bin/activate
export PYTHONPATH=$PYTHONPATH:$VIRTUALENV/lib/python2.7/site-packages

Now, get the rpy2 source:

tar xvfz rpy2-2.2.4.tar.gz

and compile it. You need to tell rpy2 what is the R version you want to link with

cd rpy2-2.2.4
export LDFLAGS="-Wl,-rpath,<path_to_R>/R-2.13.0/lib64/R/lib"
python build --r-home <path_to_R>/R-2.13.0/ install

rpy2 is installed with the proper R version, but you also need to use the proper version yourself:

export R_HOME=<path_to_R>/R-2.13.0/lib64/

Then, you can try again the python code above to import rpy2.

Not straightforward but this solution worked for me.

Please follow and like us:
This entry was posted in Computer Science, Linux, Python and tagged , , , , . Bookmark the permalink.

8 Responses to installing rpy2 with different R version already installed

  1. Jason says:

    I just want to thank you for writing such informative post. I had this problem for a while and it was finally solved. Big thanks!

  2. Calvin says:

    Thank you, very helpful post!

  3. Pingback: How to Fix: RPY2 Is not working with multiple R versions Installed | Dushyanth Jyothi

  4. Dushyanth says:

    Thanks Thomas for your solution. In case even after re-installing RPY2 isn’t working, then you may want to read on ..

  5. Martin De Kauwe says:

    Like the others have said, thanks a lot for posting an explanation, the error wasn’t entirely obvious on first viewing.

  6. Norman says:

    If you intend to use rpy2 in an conda env and don’t rely on a specific version of R, you can activate your env in the terminal and start python or your IDE from this terminal. Even though this might be a different problem setting.

Leave a Reply

Your email address will not be published.