From torpedofisken at gmail.com  Mon Jan  5 11:14:15 2009
From: torpedofisken at gmail.com (torpedo fisken)
Date: Mon, 5 Jan 2009 11:14:15 +0100
Subject: [Rd] can't get names of R_env
Message-ID: <6b50a24c0901050214w2ac73bb8t635a65b261e47696@mail.gmail.com>

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.


[Rd] can't get names of R_env
torpedo fisken