R in Python (via RPy2) in R (via RPython)

Issue #278 new
Antony Lee created an issue

Trying to load into R, via rPython (http://rpython.r-forge.r-project.org/), a Python module that depends on rpy2, fails because rpy2 unconditionally tries to initialize R when this has already been done:

$ R -q
> library('rPython'); python.exec('import rpy2.robjects')
R is already initialized

I don't know how easy it would be to add a check for an already initialized R process and reuse it.

Funnily enough, doing this the other way round works, because Py_Initialize (called by rPython) can be called multiple times.

$ python
Python 3.4.3 (default, Mar 25 2015, 17:13:50) 
[GCC 4.9.2 20150304 (prerelease)] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> from rpy2.robjects.packages import importr
>>> rPy = importr("rPython")
>>> rPy.python_exec("print(rPy)")
rpy2.robjecs.packages.Package as a <module 'rPython'>

Comments (5)

  1. Laurent Gautier

    When this part of rpy2 was written there was no easy to know if R was initialized so a flag internal to rpy2 was added (initializing R twice through its C API was a guaranteed countdown to segfault).

    May be a similar flag is now part of the R API. I have also considered having an R package as counterpart to rpy2 letting one start from either Python or R (but consistently kept postponing to a next release because of lack of resources).

    One note about hoping calling R calling Python while R itself is called from Python: while this qualifies as an issue, there is way to go Python->R->Python already in rpy2: http://rpy.sourceforge.net/rpy2/doc-2.5/html/rinterface.html#calling-python-functions-from-r

    (and when combined with something like numba it can lead to rather short executiom times - https://plus.google.com/116424798545383828852/posts/Z3bgn6bmFky )

  2. Antony Lee reporter

    After a quick look at the sources (src/unix/system.c) I am convinced that R does not expose num_initialized, which it checks during Rf_initialize_R and unconditionally exits if it is already set. Too bad.

  3. Laurent Gautier

    I just filed an RFC on the R-dev mailing (asking to add a way to check whether R has been initialized), but from experience there will not be much follow-up.

    I'll leave the issue open in case it does or we figure out a way to implement a reliable way to check whether R has been already initialized.

  4. Florian Schwendinger

    Hello I developed an alternative to rPython which also works under windows (https://bitbucket.org/Floooo/pythoninr).

    I also followed the discussion on R-devel, at least for me something like the following would work quite well.

    python.exec('import rpy2')
    python.exec("rpy2.doInit = False")
    ## The default of doInit would of course be True.
    ## And if rpy2.doInit is False you don't call Rf_initialize_R.
    python.exec('import rpy2.robjects')

    It's of course maybe not so easy, since I don't now how much you really have to change in your EmbeddedR_init. But a solution like this would allow me to modify my pyImport in a way that it is rpy2 aware.

  5. Laurent Gautier

    rpy2 is maintaining its own flag to avoid repeated initialization: having several imports will not be an issue. (note: import rpy2 does not initialize anything).

    The ideal situation would be if an initialization flag was available in R's C API (so we could both read it to know if initialization is required). Unfortunately the thread has shown that it will not be added...

  6. Log in to comment