Re: [R] Help needed understanding eval,quote,expression

From: <toby_marks_at_americancentury.com>
Date: Fri 30 Jun 2006 - 00:46:04 EST


Great help, & thanks to both of you. I actually think I get it.

I think I was locked into eval(expression ... ) as the solution. I did search the archives for this question, but it must not have clicked with me. The Thomas Lumley R-help (February 2005) was on the money. I was missing the power & flexibility of the [[ operation. It is certainly more direct.

I do believe this pattern will satisfy my original problem.

> x = list(y=list(y1="hello",y2="world"),z=list(z1="foo",z2="bar"))
> ids = list( zIds=c("z1","z2"),yIds=c("y1","y2"))
> str="y1"
> x$y[[str]]

[1] "hello"

btw: I set my prompt to $, so the first post, the $ at the beginning of the line was the prompt. apologies for the confusion.

cheers.

Prof Brian Ripley <ripley@stats.ox.ac.uk> 06/29/2006 04:06 AM

To
Joerg van den Hoff <j.van_den_hoff@fz-rossendorf.de> cc
toby_marks@americancentury.com, r-help@stat.math.ethz.ch Subject
Re: [R] Help needed understanding eval,quote,expression

On Thu, 29 Jun 2006, Joerg van den Hoff wrote:

> Prof Brian Ripley wrote:
>> You are missing eval(parse(text=)). E.g.
>> 

>>> x <- list(y=list(y1="hello",y2="world"),z=list(z1="foo",z2="bar"))
>> (what do you mean by the $ at the start of these lines?)

>>> eval(parse(text="x$y$y1"))
>> [1] "hello"
>> 
>> However, bear in mind
>> 

>>> fortune("parse")
>> 
>> If the answer is parse() you should usually rethink the question.
>>     -- Thomas Lumley
>>        R-help (February 2005)
>> 
>> In your indicated example you could probably use substitute() as 
>> effectively.
>> 
>> 
>> On Wed, 28 Jun 2006, toby_marks@americancentury.com wrote:
>> 

>>> I am trying to build up a quoted or character expression representing
a
>>> component in a list in order to reference it indirectly.
>>> For instance, I have a list that has data I want to pull, and another
list
>>> that has character vectors and/or lists of characters containing the
names
>>> of the components in the first list.
>>>
>>> It seems that the way to do this is as evaluating expressions, but I
seem
>>> to be missing something. The concept should be similar to the snippet
>>> below:
>>>
>>>
>>> For instance:
>>>
>>> $x = list(y=list(y1="hello",y2="world"),z=list(z1="foo",z2="bar"))
>>> $y = quote(x$y$y1)
>>> $eval(y)
>>> [1] "hello"
>>>
>>>
>>> but, I'm trying to accomplish this by building up y as a character and
>>> then evaluating it, and having no success.
>>>
>>> $y1=paste("x$y$","y1",sep="")
>>> $y1
>>> [1] "x$y$y1"
>>>
>>>
>>> How can I evaluate y1 as I did with y previously? or can I?
>>>
>>>
>>> Much Thanks !
>>>
>>>
>
> if I understand you correctly you can achieve your goal much easier than with
> eval, parse, substitute and the like:
>
> x <- list(y=list(y1="hello",y2="world"),z=list(z1="foo",z2="bar"))
>
> s1 <- 'y'
> s2 <- 'y1'
>
> x[[s1]][[s2]]
>
> i.e. using `[[' instead of `$' for list component extraction allows to 
use
> characters for indexing (in other words: x$y == x[['y']])

But what he actually asked for was

>>> I am trying to build up a quoted or character expression representing
a
>>> component in a list in order to reference it indirectly.

You just typed in x[[s1]][[s2]], not 'built [it] up'. Suppose the specification had been

r <- "x"
s <- c("y", "y1")

and s was of variable length? Then you need to construct a call similar to x[["y"]][["y1"]] from r and s.

[There was another reason for sticking with $ rather than using [[: the latter makes unnecessary copies in released versions of R.]

-- 
Brian D. Ripley,                  ripley@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


------------------------------------------------------------
CONFIDENTIALITY NOTICE: This electronic mail transmission (i...{{dropped}}

______________________________________________
R-help@stat.math.ethz.ch mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html
Received on Fri Jun 30 02:08:10 2006

Archive maintained by Robert King, hosted by the discipline of statistics at the University of Newcastle, Australia.
Archive generated by hypermail 2.1.8, at Fri 30 Jun 2006 - 04:12:58 EST.

Mailing list information is available at https://stat.ethz.ch/mailman/listinfo/r-help. Please read the posting guide before posting to the list.