[Rd] Terminating and restarting an embedded R instance possible?

Christian Sigg christian at sigg-iten.ch
Wed Aug 29 16:08:36 CEST 2012

I am developing R functions that interface with C++ code from the OpenCV library. During  development and unit testing, I embed an R instance to prepare the environment in which the function encapsulating OpenCV code is called.

Running the unit testing suite results in starting (and later terminating) an embedded R instance for each test. This produces a series of error messages when Rf_initEmbeddedR is called for the second time. This error is reproduced by running

#include <Rembedded.h>

int main(int argc, char **argv) {
    Rf_initEmbeddedR(argc, argv);
    Rf_initEmbeddedR(argc, argv);
    return 0;

which outputs the following two lines

Error: bad target context--should NEVER happen;
please bug.report() [R_run_onexits]

a great number of times (this is on OS X 10.7 with Xcode 4.4.1, version of the command line tools and R 2.15.1).

Debugging the above program indicates that the error happens in installFunTab(). Not knowing R internals, I can only guess that state from the first embedded instance leaks into the second instance.

Soeren Sonnenburg asked a similar question on March 5th 2009 but there was no reply. He also mentioned a bug report


but that link no longer works and bug # 12644 is not in the current database.

Can the above mentioned code be made to work?

Thank you,

More information about the R-devel mailing list