[R] substitute, eval and hastables

Robert Gentleman rgentlem at jimmy.harvard.edu
Wed Jan 29 13:24:00 CET 2003


On Wed, Jan 29, 2003 at 01:07:52PM +0100, Serge Boiko wrote:
> 
> I have the following problem. I have an automatically generated named
> list with "stringified" names:
> 
> a <- list("A"=..., "B"=..., "C"=..., )
> 
> then I want to refer to the elements of the list, stored as an vector
> of names:
> 
> nn <- c("A", "B", "C"), so that I could get list elements like
> 
> a$nn[1], a$nn[2], etc. Obviously it doesn't work. Instead I do:
> 
> nn.Exp <- substitute(expression(a$b), list(b=nn[1]))
> eval(nn.Exp)
> 
> in a result I get
> expession(a$"A") but not the value stored in the list.
> Meanwhile if I manually construct expression as expression(a$"A") and
> then evaluate it, it works fine.
> 
> How do I solve that problem? Perhaps using such a list with
> "stringified" names are not very good programming style, but this is
> convenient to store and retrieve elements if list should be filled
> automatically from numerous sources. In this way I'm trying to emulate
> a hash-table behavior. Perhaps there is a better way?

 If you want hash table behavior you could try using environments (as
 that is really about all that they are). I have been spending some
 time thinking (and a little coding) about a hash table class but it
 is unlikely to appear before the summer.
  Robert


> 
> Any help is highly appreciated
> Thanks, 
> -Serge
> 
> ______________________________________________
> R-help at stat.math.ethz.ch mailing list
> http://www.stat.math.ethz.ch/mailman/listinfo/r-help

-- 
+---------------------------------------------------------------------------+
| Robert Gentleman                 phone : (617) 632-5250                   |
| Associate Professor              fax:   (617)  632-2444                   |
| Department of Biostatistics      office: M1B20
| Harvard School of Public Health  email: rgentlem at jimmy.dfci.harvard.edu   |
+---------------------------------------------------------------------------+




More information about the R-help mailing list