I'm trying to implement R code inside some Python (3.10) software using rpy2 (3.5.7). I want to know whether I can get rpy2 to work before trying anything complicated. This is an "off-the-shelf" execution, using one of the earliest examples in the documentation introduction. I am running this from inside the PyCharm IDE. There is no mention of performing any prerequisites in the documentation.
There is a slight nuisance to this simple code. It is being executed within an event call (clicking a button) using the DearPyGUI package.
This is the rpy2 code:
import rpy2.robjects as objects
print(robjects.r)
Unfortunately, this throws:
...
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.
This is a working example of the error:
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()
With the full error message:
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.
What is going on?