[R] Returning Visibly / / Invisibly from C Function

Andrew Simmons @kw@|mmo @end|ng |rom gm@||@com
Sat Sep 17 07:40:33 CEST 2022


Hello,


I'm working on a function envvars() which I'd like to get, set, and
remove environment variables in a manner similar to options(). At the
R level, I have this function:

envvars <- function (...)
.External2(C_envvars, pairlist(...))

and then at the C level:

#define set_R_Visible(X) (eval( (X) ? R_NilValue :
lang1(install("invisible")) , R_BaseEnv))

SEXP do_envvars(SEXP call, SEXP op, SEXP args, SEXP rho)
{
    // details omitted
    set_R_Visible(visible);  /* 0 or 1 for invisible or visible return */
    UNPROTECT(nprotect);
    return value;
}

This seems to work as intended, it returns invisibly when getting 0
environment variables (setting/unsetting are not counted as getting)
and returns visibly when getting >= 1 environment variables.

My concern is that this solution feels hack-ish, like it could easily
break in a future update. So, does anyone know of a more official way
to change the visibility of a return value from a C function? Thank
you!



More information about the R-help mailing list