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

Tomas Kalibera tom@@@k@||ber@ @end|ng |rom gm@||@com
Thu Jul 29 14:50:09 CEST 2021


Dear Matthias,

On 6/17/21 8:25 AM, Matthias Gondan wrote:
> 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?
No, it is static (private) in the file.
>
> 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.

I think using R_TempDir should be fine (as long as you are not setting 
it yourself to a non-NULL value, which would be permitted by the 
documentation).

The documentation does not say explicitly that R_TempDir is actually set 
to a non-NULL value by R, so technically speaking one should not be 
relying on that, but in case of embedding there are probably a number of 
"worse" assumptions about R behavior one makes. In the unlikely case 
that this behavior would change, and R_TempDir remain NULL after R 
initialization, your program would trigger repeated initialization and 
exit, so that would be immediately discovered by testing. So I think 
this is reasonably safe.

> 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.

In principle, it would be easy to add a function to tell if R has been 
initialized, but I am not sure I fully understand the problem, why is it 
needed.

You have a Prolog library, which can be used from an R package (then 
should not initialize R), or from some other application (then should 
initialize R)? And you can modify the code of the Prolog library, but 
would prefer not to require that the Prolog library is being told 
whether it is linked to an R package, or not? And you would prefer not 
having to modify that other application to initialize R before calling 
the Prolog library, because, the library may not use R? So - having a 
flag in the Prolog library, set from the R package wrapper/external 
application - telling if used as an R package or not - would not work 
for you?

Best
Tomas

>
>
>
> 	[[alternative HTML version deleted]]
>
> ______________________________________________
> R-devel using r-project.org mailing list
> https://stat.ethz.ch/mailman/listinfo/r-devel



More information about the R-devel mailing list