I am trying to run a multipack Heroku app that uses Python and R. I have the multi buildpack installing R first, but RPy\'s installation is not able to find R despite my mo
After some helpful suggestions from @bwarren2, I think I have a somewhat cleaner way to use R, python, and rpy2 together on Heroku.
In this example, I'm using the python buildpack heroku-buildpack-python-sklearn, that contains numpy, scipy, and scikit-learn from binary builds. The Rpy2 library has nice numpy integration so you are likely to want to start with this. If not, then the same approach works with normal python buildpack.
Make a .buildpacks file like this:
https://github.com/virtualstaticvoid/heroku-buildpack-r.git
https://github.com/dbrgn/heroku-buildpack-python-sklearn/
An optional init.r file that installs R libraries, and this requirements.txt file:
numpy==1.7.0
scipy==0.11.0
scikit-learn==0.13.1
matplotlib==1.1.0
rpy2==2.3.8
Because we're picking up binary builds of these (due to BLAS and other dependencies), the version numbers must exactly match.
Then we do the normal process to use the multi-build packs. However, the python buildpack needs to know where R and some libraries were installed. The slug building system on Heroku doesn't pass all the environment variables set in the R buildpack to the python buildpack.
However, we can use the new user-env-compile feature from Heroku labs, and set variables for PATH and LD_LIBRARY_PATH explicitly. Here's what I did...
# make a test repo
git init
# add our files
git add init.r
git add requirements.txt
git add .buildpacks
# commit the files
git ci -m"testing using user-env-compile"
# create a new app using the multi buildpack code
heroku create --buildpack https://github.com/ddollar/heroku-buildpack-multi.git
# turn on user-env-compile, that allows config vars when compiling slug
# see https://devcenter.heroku.com/articles/labs-user-env-compile
heroku labs:enable user-env-compile
# set the path variables explicitly, so python knows where R is located
heroku config:set PATH=/app/vendor/R/bin:/app/vendor/gcc-4.3/bin:/app/.heroku/python/bin:/usr/local/bin:/usr/bin:/bin
heroku config:set LD_LIBRARY_PATH=/usr/lib:/usr/local/lib:/app/vendor/R/lib64/R/modules:/app/vendor/R/lib64/R/lib:/app/vendor/gcc-4.3/lib64
# create the slug
git push heroku master
# we can now access R from python using rpy2
$ heroku run python
Running `python` attached to terminal... up, run.1144
Python 2.7.4 (default, Apr 6 2013, 22:14:13)
[GCC 4.4.3] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import rpy2.robjects as robjects
>>> pi = robjects.r['pi']
>>> pi[0]
3.141592653589793
Edits: I updated LD_LIBRARY_PATH to contain /usr/lib and /usr/local/lib, to avoid the problem described here.
Also added matplotlib==1.1.0 to requirements.txt, which is an older version as described here.