[Rd] can't get names of R_env

torpedo fisken torpedofisken at gmail.com
Mon Jan 5 11:14:15 CET 2009


Hi,
I'm quite knew in R, so I might not have the R specific jargon.

But here is my problem,
I'm trying to access and use variabels given by a function environment,
more specifically the rho in do_optim in src/main/optim.c

According to the documentation
http://cran.r-project.org/doc/manuals/R-ints.html#The-_0027data_0027
the envsxp is defined as a tagged pairlist.

"ENVSXP: Pointers to the frame, enclosing environment and hash table
(NULL or a VECSXP). A frame is a tagged pairlist with tag the symbol
and CAR the bound value."

But I'm having troubles accessing the data.
I've written a small function called printNames that looks like
-----------------------
void printNames(SEXP list){
    SEXP names = getAttrib(list, R_NamesSymbol);
    int i;
    Rprintf("List is of length:%d\n",length(list));
    for (i = 0; i < length(list); i++)
      Rprintf("name of id:%d\t%s\n",i,CHAR(STRING_ELT(names, i)));
}
-----------------------
This is basicly just a copy of
-----------------------
static SEXP getListElement(SEXP list, char *str)
{
    SEXP elmt = R_NilValue, names = getAttrib(list, R_NamesSymbol);
    int i;

    for (i = 0; i < length(list); i++)
	if (strcmp(CHAR(STRING_ELT(names, i)), str) == 0) {
	    elmt = VECTOR_ELT(list, i);
	    break;
	}
    return elmt;
}
-----------------------
But this crashes and looks like
-----------------------
List is of length:16
name of id:0	Xd�
name of id:1	Xd�
name of id:2	
 *** caught segfault ***
address 0x28, cause 'memory not mapped'
------------------------
I've just added printNames(rho) in the very first line of do_optim().
Futhermore I've checked that the typeof(rho) corresponds to the

#define ENVSXP	     4 in Rinternals.h at line 84

Can anyone clarify how to access these enviroment variables.
I believe my problem lies in the sentence "A frame is a tagged
pairlist with tag the symbol and CAR the bound value."
Or more precisely, I don't quite understand the struct for env exp
--------------------------
struct envsxp_struct {
    struct SEXPREC *frame;
    struct SEXPREC *enclos;
    struct SEXPREC *hashtab;
};
--------------------------

thanks in advance.


More information about the R-devel mailing list