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 environment. 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: https://github.com/shikokuchuo/nanonext/blob/main/src/aio.c#L516-L535 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. Thanks, Charlie
ch@riie@g@o m@iii@g oii shikokuchuo@@et
2022-Nov-13 11:44 UTC
[Rd] R_GetCurrentEnv() not working as intended
Perhaps my original question was too complicated, so I will just ask: is anyone using R_GetCurrentEnv() in their C code? If so, grateful if you could point me to an example where it is working for you. I have searched Github and only come across a couple of trivial uses as an argument to Rf_eval(), where it probably returns the global environment, with the result being indistinguishable in normal use. Thanks, Charlie October 22, 2022 12:52 AM, "Charlie Gao" <charlie.gao at shikokuchuo.net> wrote:> 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 environment. > > 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: > > https://github.com/shikokuchuo/nanonext/blob/main/src/aio.c#L516-L535 > > 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. > > Thanks, > > Charlie