[Rd] R_GetCurrentEnv() not working as intended

Charlie Gao ch@r||e@g@o @end|ng |rom @h|kokuchuo@net
Sat Oct 22 01:52:09 CEST 2022

Dear all,

I am attempting to use `R_GetCurrentEnv()` to return the current 
environment within C code, but it seems to always return the global 

Specifically, I would like to use it as an argument to R_NewEnv() so it 
is created with the correct enclosing environment. I also have functions 
in the environment that reference symbols in the closure and I would 
also like to use `R_GetCurrentEnv()` as an argument to `SET_CLOENV()`.

My workaround at the moment is to pass `environment()` as one of the 
arguments to the `.Call()`. For the actual code I am referring to:


where I am currently passing `environment()` as 'clo' whereas ideally I 
would be able to use `R_GetCurrentEnv()` instead.

There is an open Bugzilla report from 2020 that says `R_GetCurrentEnv()` 
only returns the base namespace from within a `.Call()`, however I see 
that the proposed patch has already been adopted in the R source.

It seems that the function was introduced (fairly) recently in R 3.6, 
presumably for such uses. I would like to know if this is not the case 
or else confirmation that this is an outstanding bug.



More information about the R-devel mailing list