Skip to content
Prev 59981 / 63424 Next

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

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