1

the successful execution of following code depends in my case on the execution method. Executing the code for the first time via F5 works without any errors. However, executing the code for the second time via F5 raises the error:
"NotImplementedError: Conversion 'rpy2py' not defined for objects of type '<class 'rpy2.rinterface.SexpClosure'>'".
After that, I need to restart Spyder and no errors occurs for the first execution. The same error appears even for the first time, when executing single lines via F5. Can anyone fix that problem?

import os
os.environ["R_HOME"] = r"C:/Users/mea39219/AppData/Local/Programs/R/R-4.2.1"
os.environ["PATH"]   = r"C:/Users/mea39219/AppData/Local/Programs/R/R-4.2.1\bin\x64" + ";" + os.environ["PATH"]
from rpy2.robjects.packages import importr
# import R's "base" package
base = importr('base')

# import R's "utils" package
utils = importr('utils')

Version Python: 3.8.9 (WinPython); Version R: 4.2.1; Version rpy2: 3.5.5

Thanks in advance for your help!

Best regards, Alex

Alex
  • 11
  • 2
  • I cannot reproduce your error with a very similar setup (except my R is installed on `C:\Program Files` and useing the `rpy2`'s automatic detection). What happens if you remove the first 3 lines? (doubtful, but worth a try) – kesh Oct 25 '22 at 17:30
  • Dear @SamR, I still have the same problem even though changing "base" to "Rbase" and "utils" to "Rutils". Do you still have another idea? Thanks in advance for your help! – Alex Oct 26 '22 at 06:11
  • Dear @kesh, if I remove the first 3 lines and set the path in my system than I still have the same error. There was a time, 3 months ago, executing Rpy2 twice and more was not problem. But now, something changed... – Alex Oct 26 '22 at 06:17
  • 1
    I posted your issue along with similar ones from others on the GitHub issue ([click here](https://github.com/rpy2/rpy2/issues/952)), and one response indicate it's an issue with Jupyter and rolling back `rpy2` version to v3.4.2 fixed the problem for him. (You need to put these path lines back in on those versions.) Give rolling back the version a try (and perhaps comment on the issue post linked above). – kesh Oct 26 '22 at 14:45
  • Dear @kesh, thanks a lot for your help! It works! Rolling back rpy2 version to v3.4.2 fixed the problem. Best regards, Alex – Alex Oct 28 '22 at 06:03

1 Answers1

0

This is an issue with jupyterlab/ipykernel. rpy2 is not going to be the only package that fails. Others also using contextvars will likely have issues. Updating ipykernel solves the problem.

https://github.com/rpy2/rpy2/issues/952

lgautier
  • 11,363
  • 29
  • 42