Hadley Wickham
2012-Jul-27 21:53 UTC
[Rd] Version of substitute that evaluates it's first argument
Hi all,
Does there already exist a version of substitute that evaluates it's
first argument? (i.e. it accepts an already quoted expression). This
seems like something that's pretty handy, but I haven't found any
existing function to do it:
substitute_e <- function(expr, env) {
eval(substitute(substitute(expr, env), list(expr = expr)))
}
f <- quote(x + y + z)
substitute(f, list(y = 1, z = as.name("b")))
substitute_e(f, list(y = 1, z = as.name("b")))
Hadley
--
Assistant Professor / Dobelman Family Junior Chair
Department of Statistics / Rice University
http://had.co.nz/
William Dunlap
2012-Jul-27 22:18 UTC
[Rd] Version of substitute that evaluates it's first argument
For what it's worth, S+'s substitute() has had an evaluate=FALSE
argument
to substitute() for quite a while (I think it came with Sv4). evaluate=TRUE
means to evaluate its first arument:
> myCall <- call("myFunc", as.name("arg1"))
> substitute(myCall, list(arg1=as.name("newArg1")),
evaluate=TRUE)
myFunc(newArg1)
> substitute(myCall, list(arg1=as.name("newArg1")))
myCall
It seems like a good name for the option and it is handy.
Bill Dunlap
Spotfire, TIBCO Software
wdunlap tibco.com
> -----Original Message-----
> From: r-devel-bounces at r-project.org [mailto:r-devel-bounces at
r-project.org] On Behalf
> Of Hadley Wickham
> Sent: Friday, July 27, 2012 2:53 PM
> To: r-devel at r-project.org
> Subject: [Rd] Version of substitute that evaluates it's first argument
>
> Hi all,
>
> Does there already exist a version of substitute that evaluates it's
> first argument? (i.e. it accepts an already quoted expression). This
> seems like something that's pretty handy, but I haven't found any
> existing function to do it:
>
> substitute_e <- function(expr, env) {
> eval(substitute(substitute(expr, env), list(expr = expr)))
> }
>
> f <- quote(x + y + z)
>
> substitute(f, list(y = 1, z = as.name("b")))
> substitute_e(f, list(y = 1, z = as.name("b")))
>
> Hadley
>
> --
> Assistant Professor / Dobelman Family Junior Chair
> Department of Statistics / Rice University
> http://had.co.nz/
>
> ______________________________________________
> R-devel at r-project.org mailing list
> https://stat.ethz.ch/mailman/listinfo/r-devel