using R's rPython to run python's rpy2

133 Views Asked by At

I'm trying to use R to run a function I've written in python. That function uses some methods from R. So when I import the function from rPython, it crashes, probably due to circular import.

Essentially, my R code may be reduced to:

library(rPython)
python.exec("import rpy2.robjects")

Which results in immediate termination of R.

Note: the code above is just for demonstration, in my code I'm doing something useful, e.g:

library(rPython)
python.exec("from my_package import foo")

And the import rpy2.robjects is within my_package.py.

How may I organize my code such that I won't get this circular importing?

1

There are 1 best solutions below

0
lgautier On

The crash is likely caused by the embedded R being initialized twice.

There is a discussion about introducing environment variables to capture the initialization status of R here and initial implementations should be in reticulate and rpy2 (although it was still crashing last time I tried).