Instead of eval(expr), use lapply(expr,eval)

HTH
Schalk Heunis


On Wed, Sep 16, 2009 at 5:50 AM, Michael Spiegel <
michael.m.spiegel@gmail.com> wrote:

> Hi,
>
> I'm trying to use a list of symbols as one of the values to be
> substituted in a substitute expression, but I can't figure out how to
> write the correct expression for my problem.  Let me illustrate a
> simple example of what I'm trying to do.  The following code snippet
> will evaluate to '5':
>
> symname <- 'foo'
> foo <- 5
> expr <- substitute(c(expr), list(expr = as.symbol(symname)))
> eval(expr)
>
> I would like the next similar example to evaluate to the list('5',
> '6', '7'), but instead it evaluates to the list(foo, bar, baz) where
> the type of foo, bar, and baz are all symbols.  For the purposes of
> finding a solution assume that the length and contents of symnames are
> unknown ahead of time (symname is not a constant, assume it is some
> parameter to a function).
>
> symnames <- c('foo', 'bar', 'baz')
> foo <- 5
> bar <- 6
> baz <- 7
> expr <- substitute(expr, list(expr = lapply(symnames, as.symbol)))
> eval(expr)
>
> Thanks!
> --Michael
>
> ______________________________________________
> R-help@r-project.org mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide
> http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.
>

	[[alternative HTML version deleted]]


