[R] Embedded R: Test if initialized

Matthias Gondan m@tth|@@-gond@n @end|ng |rom gmx@de
Wed Jun 16 21:51:24 CEST 2021


Dear R friends,

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 the reverse connection to R. In that case, i.e., R -> Prolog -> R, I do not want to initialize R a second time.



	[[alternative HTML version deleted]]



More information about the R-help mailing list