5

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?

halfer
  • 19,824
  • 17
  • 99
  • 186
Anthony Nash
  • 834
  • 1
  • 9
  • 26
  • 1
    I cannot provide a solution, although I might have a related problem: https://discuss.streamlit.io/t/use-rpy2-to-run-r-script-and-convert-to-a-pandas-data-frame/36173 – Revan Jan 16 '23 at 12:14
  • I am fighting a similiar issue, and the problem seems to happen everytime there are multiple threads involved, like your gui example. I had this problem with streamlit, now with FastAPI too. This makes me lean away from rpy2. Unfortunately for my use case there seem to be no good alternatives for the functionalities that I need in python :( – Aleks J Jan 25 '23 at 11:48
  • This might be caused by Python `contextvars` having compatibility issues with existing multithreading code (see https://github.com/rpy2/rpy2/issues/965#issuecomment-1364753764). If jupyter is involved updating `ipykernel` should solve the issue. – lgautier Jun 25 '23 at 16:05

2 Answers2

2

This is an issue with newer versions of rpy2. I was able to successfully overcome this issue by downgrading:

pip install rpy2==3.5.1
Matt Dancho
  • 6,840
  • 3
  • 35
  • 26
1

I solved this issue by wrapping all relevant rpy2 code in a context

from rpy2.robjects import conversion, default_converter
with conversion.localconverter(default_converter):
    # rpy2 code here

Conversions happen any time you materialise R objects concretely within python. In this case the print() is causing this to happen.

Working as of rpy2=3.5.11

Frank Martin
  • 103
  • 4