[Rd] Check if (embedded) R has been initialized from C code

Matthias Gondan m@tth|@@-gond@n @end|ng |rom gmx@de
Thu Jun 17 08:25:27 CEST 2021


Dear R developers,

(This email has also been sent to R-help before, but I was told that it doesn’t really fit there)

I am currently trying to write a piece of C code that uses „embedded R“, and for specific reasons*, I cannot keep track if R already has been initialized. So the code snippet looks like this:

LibExtern char *R_TempDir;

if(R_TempDir == NULL)
    …throw exception R not initialized…

I have seen that the source code of Rf_initialize_R itself checks if it is ivoked twice (num_initialized), but this latter flag does not seem to accessible, or is it? 

int Rf_initialize_R(int ac, char **av)
{
    int i, ioff = 1, j;
    Rboolean useX11 = TRUE, useTk = FALSE;
    char *p, msg[1024], cmdlines[10000], **avv;
    structRstart rstart;
    Rstart Rp = &rstart;
    Rboolean force_interactive = FALSE;

    if (num_initialized++) {
        fprintf(stderr, "%s", "R is already initialized\n");
        exit(1);
    }


Is the test of the TempDir a good substitute, or should I choose another solution? Having said this, it may be a good idea to expose a function Rf_R_initialized that performs such a test.

Thank you for your consideration.

Best regards,

Matthias

*The use case is an R library that connects to swi-prolog and allows the „embedded“ swi-prolog to establish an additional reverse connection to R. In that case, i.e., R -> Prolog -> R, I do not want to initialize R a second time. But since the interaction may also start from the swi-prolog system (and in the latter case, R needs to be initialized), I need a function such as the one mentioned here.



	[[alternative HTML version deleted]]



More information about the R-devel mailing list