rpy2 抛出有关转换规则的 NotImplementedError

Ant*_*ash 5 python r rpy2

我正在尝试使用 rpy2 (3.5.7) 在一些 Python (3.10) 软件中实现 R 代码。我想知道在尝试任何复杂的事情之前我是否可以让 rpy2 工作。这是一个“现成的”执行,使用文档简介中最早的示例之一。我从 PyCharm IDE 内部运行它。文档中没有提及执行任何先决条件。

这个简单的代码有点麻烦。它是使用 DearPyGUI 包在事件调用(单击按钮)中执行的。

这是 rpy2 代码:

import rpy2.robjects as objects
print(robjects.r)
Run Code Online (Sandbox Code Playgroud)

不幸的是,这会抛出:

...
    raise NotImplementedError(_missingconverter_msg)
NotImplementedError: 
    Conversion rules for `rpy2.robjects` appear to be missing. Those
    rules are in a Python contextvars.ContextVar. This could be caused
    by multithreading code not passing context to the thread.
Run Code Online (Sandbox Code Playgroud)

这是该错误的一个工作示例:

import dearpygui.dearpygui as dpg
import rpy2.robjects as robjects

def testFunction():
    print(robjects.r)

dpg.create_context()
dpg.create_viewport()
dpg.setup_dearpygui()

with dpg.window(label="Example Window"):
    dpg.add_text("Hello world")
    dpg.add_button(label="Save", callback=testFunction)

dpg.show_viewport()
dpg.start_dearpygui()
dpg.destroy_context()
Run Code Online (Sandbox Code Playgroud)

完整的错误消息:

Traceback (most recent call last):
  File "/home/anthony/CPRD-software/test.py", line 6, in testFunction
    print(robjects.r)
  File "/home/anthony/anaconda3/envs/CPRD-software/lib/python3.10/site-packages/rpy2/robjects/__init__.py", line 451, in __str__
    version = self['version']
  File "/home/anthony/anaconda3/envs/CPRD-software/lib/python3.10/site-packages/rpy2/robjects/__init__.py", line 440, in __getitem__
    res = conversion.get_conversion().rpy2py(res)
  File "/home/anthony/anaconda3/envs/CPRD-software/lib/python3.10/functools.py", line 889, in wrapper
    return dispatch(args[0].__class__)(*args, **kw)
  File "/home/anthony/anaconda3/envs/CPRD-software/lib/python3.10/site-packages/rpy2/robjects/conversion.py", line 370, in _raise_missingconverter
    raise NotImplementedError(_missingconverter_msg)
NotImplementedError: 
    Conversion rules for `rpy2.robjects` appear to be missing. Those
    rules are in a Python contextvars.ContextVar. This could be caused
    by multithreading code not passing context to the thread.
Run Code Online (Sandbox Code Playgroud)

到底是怎么回事?

Mat*_*cho 3

这是较新版本的问题rpy2。我通过降级成功解决了这个问题:

pip install rpy2==3.5.1
Run Code Online (Sandbox Code Playgroud)