Re: [R] substitute in a named expression

From: Gabor Grothendieck <ggrothendieck_at_gmail.com>
Date: Thu 23 Jun 2005 - 08:47:07 EST

On 6/22/05, Søren Højsgaard <Soren.Hojsgaard@agrsci.dk> wrote:
> I have a 'named expression' like
> expr <- expression(rep(1,d))
> and would like to replace the argument d with say 5 without actually evaluating the expression. So I try substitute(expr, list(d=5)) in which case R simply returns expr which when I 'evaluate' it gives
> eval(expr)
> Error in rep.default(1, d) : invalid number of copies in rep()
>
> I've looked at ?substitute and ?expression (and other places) for ideas, but - well I guess there are some details which I haven't quite understood. Can anyone point me in the right direction?

Try this:

eval(substitute(substitute(qq, list(d=5)), list(qq = expr[[1]])))

This aspect of R drove me crazy some time ago but Tony Plate finally figured it out and discussed it some time back:

   http://tolstoy.newcastle.edu.au/R/help/04/03/1247.html There is also a handy utility routine, esub, defined there.

The key points are:

Sorry if this is complicated but that seems to be how it works. Using the esub function defined in the link above you can simplify it substantially like this:

esub(cl, list(d=5))

# or

esub(expr[[1]], list(d=5))



R-help@stat.math.ethz.ch mailing list
https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html Received on Thu Jun 23 08:50:44 2005

This archive was generated by hypermail 2.1.8 : Fri 03 Mar 2006 - 03:32:57 EST