[R] substitute question
Gabor Grothendieck
ggrothendieck at myway.com
Fri Mar 19 02:11:08 CET 2004
I don't think I expressed myself very well on that.
Looking at what we get from the example:
> z <- substitute(substitute(expression(f),list(a=quote(b))),list(f=f))
> z
substitute(expression(function ()
{
a + 1
}), list(a = quote(b)))
> class(z);mode(z);typeof(z)
[1] "call"
[1] "call"
[1] "language"
we see that the function seems to be expanded correctly and
the statement does produce a call object. However,
applying eval one, two or three times does not give what
you would think if you looked at z above. In all cases,
a is still there. Some posters have claimed that the
problem is that list(f=f) cannot hold a function and must be
an expression but that's not what the help page says, the
examples are not all of class expression or call and
even if that were true then my parse/deparse example should
work since it is unquestionably an expression.
Date: Thu, 18 Mar 2004 16:41:31 -0700
From: Tony Plate <tplate at blackmesacapital.com>
To: <ggrothendieck at myway.com>, <p.dalgaard at biostat.ku.dk>
Cc: <tlumley at u.washington.edu>, <R-help at stat.math.ethz.ch>
Subject: Re: [R] substitute question
Gabor, you might have less frustration if you work with Peter's esub()
suggestion (use it instead of substitute()):
esub <- function(expr, subst.list) do.call("substitute", list(expr,
subst.list))
When you say
"In the other one, it is not an expression in the inner substitute
but should be by the time it gets to the outer one since I explicitly added
expression(...). It is expanded which shows it did do that part right."
something to bear in mind is the "natural order" of evaluation is reversed
with nested substitute()'s (because the inner substitute() is quoted by the
outer substitute()) (but I haven't carefully reasoned through this being
an explanation for your particular frustration here.)
hope this might help,
Tony Plate
At Thursday 04:06 PM 3/18/2004, Gabor Grothendieck wrote:
> > Date: 18 Mar 2004 23:52:47 +0100
> > From: Peter Dalgaard <p.dalgaard at biostat.ku.dk>
> > To: <ggrothendieck at myway.com>
> > Cc: <p.dalgaard at biostat.ku.dk>, <tlumley at u.washington.edu>,
> <tplate at blackmesacapital.com>, <R-help at stat.math.ethz.ch>
> > Subject: Re: [R] substitute question
> >
> >
> > "Gabor Grothendieck" <ggrothendieck at myway.com> writes:
> >
> > > 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.
> >
> > Er, I don't think so. All I have seen is a couple of cases where you
> > tried to pass something that was not a language object (e.g. a
> > function as opposed to an expression or call generating a function.)
> >
> >
>
>The parse/deparse one definitely is an expression:
>
> > z <- parse(text=deparse(f))
> > class(z);mode(z);typeof(z)
>[1] "expression"
>[1] "expression"
>[1] "expression"
>
>In the other one, it is not an expression in the inner substitute
>but should be by the time it gets to the outer one since I
>explicitly added expression(...). It is expanded which shows
>it did do that part right.
>
More information about the R-help
mailing list