简体   繁体   English

NotImplementedError: 转换 'rpy2py' 没有为类型 ' 的对象定义<class 'rpy2.rinterface.sexpclosure'> ' 只有在我运行代码两次之后</class>

[英]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:如果我在 Jupyter notebook 中运行此单元格两次,我会收到以下错误消息:

---------------------------------------------------------------------------
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?可能是新版本的 python 或 rpy2 是问题所在? How can I fix the problem such that I am able to run this code multiple times within one Jupyter notebook.我该如何解决这个问题,以便我能够在一个 Jupyter notebook 中多次运行此代码。

This bug occurs for the vollowing version: ipykernel version 5.3.4,此错误发生在以下版本: ipykernel版本 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结合rpy2版本 3.5.5

import rpy2
print(rpy2.__version__)

3.5.5

This is caused by an issue in older releases of ipykernel .这是由旧版本的ipykernel中的问题引起的。 I'd recommend to upgrade it rather than downgrade rpy2 .我建议升级它而不是降级rpy2

See https://github.com/rpy2/rpy2/issues/952请参阅https://github.com/rpy2/rpy2/issues/952

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 ).在 Jupyter-notebook 开始时为了回滚到版本3.4.2 ,这个问题没有发生(参见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 )有关如何更改 python 包版本的更多信息,请参阅在 Goolge Colab 中回滚到特定版本的 python 包使用 pip 安装特定包版本

It would still be interesting to understand how one could use the latest version of rpy2 correctly.了解如何正确使用最新版本的rpy2仍然很有趣。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Python 错误 - NotImplementedError: 转换 'py2rpy' 没有为类型的对象定义<class 'ipywidgets.widgets.widget_string.text'> '</class> - Python Error - NotImplementedError: Conversion 'py2rpy' not defined for objects of type '<class 'ipywidgets.widgets.widget_string.Text'>' Django 中的 Rpy2 错误 - 转换 'py2rpy' 未为类型 ' 的对象定义<class 'str'></class> - Rpy2 Error in Django - Conversion 'py2rpy' not defined for objects of type '<class 'str'> Python 脚本中的 RPY2 错误! (未为类型为“的对象定义转换‘py2rpy’<class 'pandas.core.frame.dataframe'> ')</class> - RPY2 error in Python script! (Conversion 'py2rpy' not defined for objects of type '<class 'pandas.core.frame.DataFrame'>') AWS Lambda Python / R rpy2问题:“无法导入模块&#39;py_test&#39;:没有名为&#39;rpy2.rinterface._rinterface&#39;的模块” - AWS Lambda Python/R rpy2 issue: “Unable to import module 'py_test': No module named 'rpy2.rinterface._rinterface'” 为什么我收到“模块'rpy2.robjects.conversion'没有属性'py2rpy'”错误? - Why am I getting “module 'rpy2.robjects.conversion' has no attribute 'py2rpy'” error? 如何在 Python 中捕获 rpy2.rinterface.RRuntimeError? - How do I catch an rpy2.rinterface.RRuntimeError in Python? Rpy2错误:“没有名为_rinterface的模块”? - Rpy2 error: “No module named _rinterface”? rpy2在ubuntu上测试获取“ NotImplementedError:从rpy2 DataFrame转换为熊猫的DataFrame” - rpy2 tests getting “NotImplementedError: Conversion from rpy2 DataFrame to pandas' DataFrame” on ubuntu 可以并行运行rpy2代码吗? - Can rpy2 code be run in parallel? Anaconda:无法从“rpy2.rinterface”导入“RRuntimeError” - Anaconda: Cannot import 'RRuntimeError' from 'rpy2.rinterface'
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM