Search code examples
pythonjupyter-notebookrpy2

NotImplementedError: Conversion 'rpy2py' not defined for objects of type '<class 'rpy2.rinterface.SexpClosure'>' only after I run the code twice


If I run the following code once it works.

import numpy as np
import rpy2.robjects as robjects
x = np.linspace(0, 1, num = 11, endpoint=True)    
y = np.array([-1,1,1, -1,1,0, .5,.5,.4,  .5, -1])

r_x = robjects.FloatVector(x)
r_y = robjects.FloatVector(y)
r_smooth_spline = robjects.r['smooth.spline'] #extract R function
spline_xy = r_smooth_spline(x=r_x, y=r_y)
print('x =', x)
print('ysplined =',np.array(robjects.r['predict'](spline_xy,robjects.FloatVector(x)).rx2('y')))

If I run this cell twice in a Jupyter notebook, I obtain the following error message:

---------------------------------------------------------------------------
NotImplementedError                       Traceback (most recent call last)
<ipython-input-2-5efeb940cd16> in <module>
      6 r_x = robjects.FloatVector(x)
      7 r_y = robjects.FloatVector(y)
----> 8 r_smooth_spline = robjects.r['smooth.spline'] #extract R function
      9 spline_xy = r_smooth_spline(x=r_x, y=r_y)
     10 print('x =', x)

2 frames
/usr/local/lib/python3.8/dist-packages/rpy2/robjects/conversion.py in _rpy2py(obj)
    250     non-rpy2) objects.
    251     """
--> 252     raise NotImplementedError(
    253         "Conversion 'rpy2py' not defined for objects of type '%s'" %
    254         str(type(obj))

NotImplementedError: Conversion 'rpy2py' not defined for objects of type '<class 'rpy2.rinterface.SexpClosure'>'

This code always used to run without problems multiple times. Probably a new version of python or rpy2 is the problem? How can I fix the problem such that I am able to run this code multiple times within one Jupyter notebook.

This bug occurs for the vollowing version: ipykernel version 5.3.4,

!jupyter --version

Selected Jupyter core packages...
IPython          : 7.9.0
ipykernel        : 5.3.4
ipywidgets       : 7.7.1
jupyter_client   : 6.1.12
jupyter_core     : 4.11.2
jupyter_server   : not installed
jupyterlab       : not installed
nbclient         : not installed
nbconvert        : 5.6.1
nbformat         : 5.7.0
notebook         : 5.7.16
qtconsole        : not installed
traitlets        : 5.1.1

in combination with rpy2 version 3.5.5

import rpy2
print(rpy2.__version__)

3.5.5

Solution

  • The easiest fix is to run once:

    !pip install -Iv rpy2==3.4.2
    

    at the start of the Jupyter-notebook in order to rollback to version 3.4.2, where this problem did not occur (see Rpy2 Error depends on execution method: NotImplementedError: Conversion "rpy2py" not defined). For more information how to change the version of a python package see Rollback to specific version of a python package in Goolge Colab and Installing specific package version with pip)

    It would still be interesting to understand how one could use the latest version of rpy2 correctly.