Forums

rpy2 in web2py

Hi,

I am trying to use rpy2 in web2py, to use R functions in python. Using "import rpy2 as robj" in controller doesn't give any error, but if I write "import rpy2.robjects as ro", it gives the following error:

<type 'exceptions.OSError'> [Errno 11] Resource temporarily unavailable

If I write the same line in pythonanywhere bash console, it doesn't give this error.

Is there anything I am doing wrongly?

Thanks! Tauno

Could you try reloading your web app (from the "Web" tab) and see if it fixes the problem? That error normally means that you're trying to start too many processes, and our last release restricted the number of processes you can run too much. We've fixed the problem, but web apps that were started before that fix need reloading to pick up the looser controls.

Thanks a lot for the quick answer, this worked!

Great! Thanks for confirming :-)

Hi,

Sorry to bother again, but I am trying to install an R package now to use it inside R code and couldn't figure out how to get it working. My code is as follows:

import rpy2.robjects as ro

from rpy2.robjects.packages import SignatureTranslatedAnonymousPackage

code = """

install.packages("linprog", repos = "http://cran.us.r-project.org")

library("linprog")

"""

opt = SignatureTranslatedAnonymousPackage(code, "opt")

The message that appears is

<class 'rpy2.rinterface.RRuntimeError'> Error in library("linprog") : there is no package called ‘linprog’

Should I set the folder first where to install packages? I also tried the instructions here: http://stackoverflow.com/questions/11561258/r-python-install-packages-on-rpy2, but no success, either.

Best, Tauno

Hi Tauno,

Can you try installing the R package from outside of Python, just via a normal Bash command-line? If there's some way of installing it with normal user permissions, it might work. Otherwise, if you let us know the name of the package, we might be able to install it for you when we next do a server image build...

Thanks, Harry!

I got it working! Here are steps if anybody is interested:

  1. Download the package source from CRAN, e.g. in my case, it was from http://cran.r-project.org/web/packages/lpSolve/index.html -> lpSolve_5.6.8.tar.gz.

  2. Upload the archive to your pythonanywhere account, e.g. to the folder /home/username/R/.

  3. Install the package from pythonanywhere console: cd /home/username/R/; R CMD INSTALL lpSolve_5.6.8.tar.gz

  4. Check which folder was used for installing. In my case it was /home/username/R/x86_64-pc-linux-gnu-library/3.0/.

  5. In R code, you should set the same folder to load package:

.libPaths("/home/username/R/x86_64-pc-linux-gnu-library/3.0/")

library("lpSolve")

If somebody knows any easier solution, you are welcome to write about it. Also, I don't know how to install dependencies easily. Running R from pythonanywhere console and calling install.packages("lpSolve") didn't work, it gave error message "HTTP status was '403 Forbidden'".

Best, Tauno

Thanks for posting that, Tauno! Hopefully it'll be helpful for anyone else with the same question.

Hi, Thanks for your posting I had an error when trying to install the file

Error: ERROR: no permission to install to directory /usr/local/lib/R/site-library

Did you have a similar error? Thanks Luis

Hi, at the time of writing, moving to a custom folder before installing ("cd /home/username/R/;") set the default folder where the package was installed. Probably this is not the case any more, so maybe you can try to set the install folder using "-l lib" in the R CMD INSTALL command (haven't tried myself). See https://stat.ethz.ch/R-manual/R-devel/library/utils/html/INSTALL.html.

Best, Tauno

Hi, I am trying to use rpy2 in my django application. But when I run the command - python manage.py migrate I get the following error -

ffi.error: symbol 'R_tryCatchError' not found in library '/usr/lib/R/lib/libR.so': /usr/lib/R/lib/libR.so: undefined symbol: R_tryCatchError

What versions of python and rpy2 do you use? Do you do it in a virtual environment or with the "batteries included" package or with --local installed one?

I am using rpy2 version 3.3.5 and python version 3.6. I am using a virtual environment.

Then the version of rpy2 that you have installed is not compatible with the C libraries that we have installed. The version of rpy2 that we know works with Python 3.6 is 3.2.1.

Can you try installing the R package from outside of Python, just via a normal Bash command-line?