Wednesday 22 May 2013

Undefined symbol (Rf_PrintWarnings) when using rpy 2.3.1 and R 3.0.1

So it seems the version of rpy2 (2.3.1) that comes with Ubuntu Raring isn't currently compatible with the version of R (3.0.1) in CRAN.

If you try to import it you'll get something like this:

$ python
Python 2.7.4 (default, Apr 19 2013, 18:28:01) 
[GCC 4.7.3] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import rpy2
>>> import rpy2.rinterface
Traceback (most recent call last):
  File "", line 1, in 
  File "/usr/lib/python2.7/dist-packages/rpy2/rinterface/__init__.py", line 93, in 
    from rpy2.rinterface._rinterface import *
ImportError: /usr/lib/python2.7/dist-packages/rpy2/rinterface/_rinterface.so: undefined symbol: Rf_PrintWarnings

However, the solution is pretty straightforward.

First download the source for rpy2 2.3.1. Then extract it and edit the file rpy2-2.3.1/rpy/rinterface/_rinterface.c. Then comment out the following on lines 1764-1765:
  extern void Rf_PrintWarnings(void);
  Rf_PrintWarnings(); /* show any warning messages */


Such that it appears as:
  //extern void Rf_PrintWarnings(void);
  //Rf_PrintWarnings(); /* show any warning messages */


Change to the rpy2-2.3.1 directory where you extracted the source to and reinstall rpy2 using:

sudo python setup.py install

Which should allow it to work again.
$ python
Python 2.7.4 (default, Apr 19 2013, 18:28:01) 
[GCC 4.7.3] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import rpy2
>>> import rpy2.rinterface
>>> 

You won't get any of the warning messages that Rf_PrintWarnings() would have output when it calls an R function.  This has already been fixed in the development branch but this quick fix means you don't have to wait for an official update to get rpy2 running again.

5 comments:

  1. After doing this I am receiving Segmentation fault error when importing rpy2.orobjects

    ReplyDelete
    Replies
    1. Hi,

      At this point the latest version of rpy2 is now 2.3.8 which incorporates the fix this post addresses. As such you are probably best just installing the latest version of rpy2 which should generally just work.

      Tony

      Delete
  2. Assume you have installed a compatible "python" version.
    Firstly use
    > sudo -i
    Then
    > pip install rpy2

    It can resolve the problems such as "ImportError: /usr/lib/python2.7/dist-packages/rpy2/rinterface/_rinterface.so: undefined symbol: Rf_PrintWarning".

    SUBRATA GHOSH

    ReplyDelete
  3. Hi Subrata, thanks for the comment. This article was written at a point in time where this did not work. I mention in my last post (2013-12-03) that installing the latest version of rpy2 is probably now the best solution.

    ReplyDelete
  4. Thanks for this. Upgrading is not an option for one of my users, so this has just saved me in 2017.

    ReplyDelete

Note: only a member of this blog may post a comment.