Virtualenv Python+R

homeblogtwitterthingiverse



The following creates a Python virtualenv that can also hold R libraries.

    virtualenv venv

    mkdir venv/R
    
    echo 'export R_LIBS=$VIRTUAL_ENV/R' >>venv/bin/activate
    for LIB in venv/lib/python*
    do
        echo 'import os,sys; os.environ["R_LIBS"]=sys.prefix+"/R"' >$LIB/sitecustomize.py
    done

    echo ". `pwd`/venv/bin/activate && `which R` \$@" >venv/bin/R
    echo ". `pwd`/venv/bin/activate && `which Rscript` \$@" >venv/bin/Rscript
    chmod a+x venv/bin/R venv/bin/Rscript

Python accessed from R using the R package "rPython" will correctly use Python packages installed in the virtualenv. (Note: As at 2014-12-05, rPython is pretty dodgy, floats are only sent accurate to 5 digits by default and strings not correctly quoted. May also need to use jsonlite, and call jsonlite::toJSON with digits=50.)

R accessed from Python with "Rpy2" etc will correctly use R packages installed in the virtualenv.


See also Packrat for R.




[æ]