Dear developers, is it possible to create environments in C code of packages? Simply using SEXP env; PROTECT (env = allocSExp(ENVSXP)); and assigning the enclosing environment with SET_ENCLOS seems to be insufficient. Best wishes, Martin -- Dr. Martin Becker Statistics and Econometrics Saarland University Campus C3 1, Room 206 66123 Saarbruecken Germany
Martin Becker wrote:> Dear developers, > > is it possible to create environments in C code of packages? > Simply using > SEXP env; > PROTECT (env = allocSExp(ENVSXP)); > and assigning the enclosing environment with SET_ENCLOS seems to be > insufficient. > > Best wishes,Here's a function I use in rapache to create one: static SEXP NewEnv(SEXP enclos){ SEXP env; PROTECT(env = allocSExp(ENVSXP)); SET_FRAME(env, R_NilValue); SET_ENCLOS(env, (enclos)? enclos: R_GlobalEnv); SET_HASHTAB(env, R_NilValue); SET_ATTRIB(env, R_NilValue); UNPROTECT(1); return env; } and an example that creates a new environment and then assigns a variable named OK an integer vector length 1 with value 0: SEXP env = NewEnv(R_GlobalEnv); defineVar(install("OK"),NewInteger(0),env); Best Jeff
On Oct 1, 2009, at 11:33 , Martin Becker wrote:> Dear developers, > > is it possible to create environments in C code of packages? > Simply using > SEXP env; > PROTECT (env = allocSExp(ENVSXP)); > and assigning the enclosing environment with SET_ENCLOS seems to be > insufficient. >Rf_NewEnvironment is the function that does it, e.g., Rf_NewEnvironment(R_NilValue, R_NilValue, parent) it's not part of the official API (headers) but it is visible. For hashed environments it's R_NewHashedEnv(). Cheers, Simon