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:
wget http://pypi.python.org/packages/source/r/rpy2/rpy2-2.2.4.tar.gz
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 setup.py 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.
Pingback: How to Fix: RPY2 Is not working with multiple R versions Installed | Dushyanth Jyothi