From ggrothendieck at gmail.com  Mon Oct 23 07:13:26 2006
From: ggrothendieck at gmail.com (Gabor Grothendieck)
Date: Mon, 23 Oct 2006 01:13:26 -0400
Subject: [Rd] Changing function arguments
In-Reply-To: <67DCA285A2D7754280D3B8E88EB5480211DDA481@MSGBOSCLB2WIN.DMN1.FMR.COM>
References: <67DCA285A2D7754280D3B8E88EB5480211DDA481@MSGBOSCLB2WIN.DMN1.FMR.COM>
Message-ID: <971536df0610222213g4f08515sfd462dc7080c26a@mail.gmail.com>

Try this.  If the first arg of FUN is x then it increments it.

incrx <- function (e) {
    is.node <- function(x) is.symbol(x) || is.double(x)
    if (is.node(e)) return(e)
    if (is.name(e[[1]]) && e[[1]] == as.name("FUN") && names(e)[2] == "x")
        e[[2]] <- e[[2]] + 1
    for (i in 1:length(e)) e[[i]] <- incrx(e[[i]])
    return(e)
}

incrx(expression(FUN(x = 0) + log(FUN(x = 3))))


On 10/22/06, McGehee, Robert <Robert.McGehee at geodecapital.com> wrote:
> R-Developers,
> I'm looking for some help computing on the R language.
>
> I'm hoping to write a function that parses a language or expression
> object and returns another expression with all instances of certain
> argument of a given function altered. For instance, say I would like my
> function, myFun to take an expression and whenever the argument 'x'
> appears within the function FUN inside that expression, return an
> altered expression in which 'x' is incremented by one.
>
> Thus,
> > x <- expression(FUN(x = 0) + log(FUN(x = 3)))
> > myFun(x)
> [1] expression(FUN(x = 1) + log(FUN(x = 4)))
>
> Conceptually, it looks like I want to recursively break a language
> object into its component functions, search for FUN and increment one of
> its arguments ('x'), then reassemble and return the resulting
> expression. However, I haven't been able to come up with a clean way to
> do this. Suggestions would be greatly appreciated.
>
> Thanks in advance,
> Robert
>
> Robert McGehee
> Quantitative Analyst
> Geode Capital Management, LLC
> 53 State Street, 5th Floor | Boston, MA | 02109
> Tel: 617/392-8396    Fax:617/476-6389
> mailto:robert.mcgehee at geodecapital.com
>
>
>
> This e-mail, and any attachments hereto, are intended for us...{{dropped}}
>
> ______________________________________________
> R-devel at r-project.org mailing list
> https://stat.ethz.ch/mailman/listinfo/r-devel
>




[Rd] Changing function arguments
Gabor Grothendieck