You will have to read more carefully: an ENVSXP is nothing like a VECSXP,
and does not have the names of its entries in a names attribute.
You access variables in an environment via findVar() and friends,
including findVarInFrame: see 'Writing R Extensions'.
On Mon, 5 Jan 2009, torpedo fisken wrote:
> 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
Very likely this is not the way to do whatever it is that you really want
to do.
> 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.
> ______________________________________________
> R-devel at r-project.org mailing list
> https://stat.ethz.ch/mailman/listinfo/r-devel
>
--
Brian D. Ripley, ripley at stats.ox.ac.uk
Professor of Applied Statistics, http://www.stats.ox.ac.uk/~ripley/
University of Oxford, Tel: +44 1865 272861 (self)
1 South Parks Road, +44 1865 272866 (PA)
Oxford OX1 3TG, UK Fax: +44 1865 272595