[R] list of symbols to substitution

Michael Spiegel michael.m.spiegel at gmail.com
Wed Sep 16 05:50:41 CEST 2009


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



More information about the R-help mailing list