[R] substitute question

Thomas Lumley tlumley at u.washington.edu
Fri Mar 19 00:56:59 CET 2004


On Thu, 18 Mar 2004, Gabor Grothendieck wrote:

>
>
> From:   Peter Dalgaard <p.dalgaard at biostat.ku.dk>
> > (The real pain in these examples is that substitute autoquotes its
> > expr argument. Therefore, when you want to modify an expression that
> > is already stored in a variable, you need an extra outer layer of
> > eval(substitute(...)) to poke the content of the variable into the
> > inner substitute. An "esub" function with standard evaluation
> > semantics would make this much easier.)
>
> That is one of the frustrations of using substitute.
>
> The other is that even if you do perform two levels of substitute,
> as I have been trying, you still can't count on it working for
> an arbitrary unevaluated expression, as my examples show.

You can, but a function isn't an arbitrary unevaluated expression
 You still aren't passing an expression containg the symbol `a`. You are
passing an expression containing the function f, whose body is an
expression containing the symbol `a`.  Functions aren't expressions.
>
> f <- function() { a + 1 }
> z <- substitute(substitute(f=f,list(a=quote(b))),list(f=parse(text=deparse(f)$
> eval(eval(z))
>
> or
>
> f <- function() { a + 1 }
> z <- substitute(substitute(expression(f),list(a=quote(b))),list(f=f))
> eval(eval(eval(z)))


> (1) One can either pick apart the function using body, or

Actually, I think this is fairly natural -- it is only body(f) that is an
expression.  Certainly substitute() could have been written to operate on
functions as well, but it wasn't.



	-thomas




More information about the R-help mailing list