From steve at revolution-computing.com  Tue May 19 23:05:26 2009
From: steve at revolution-computing.com (Steve Weston)
Date: Tue, 19 May 2009 17:05:26 -0400
Subject: [Rd] Qs: The list of arguments, wrapping functions...
In-Reply-To: <c2350ba40905191322y2bffbfdu79f4cb480732252e@mail.gmail.com>
References: <c2350ba40905191322y2bffbfdu79f4cb480732252e@mail.gmail.com>
Message-ID: <4f7bf0100905191405t750fddfbrf5258788d5c1a390@mail.gmail.com>

On Tue, May 19, 2009 at 4:22 PM, Kynn Jones <kynnjo at gmail.com> wrote:

> 2. I have a package in which most of the functions have the form:
>
> the.function <- function(some, list, of, params) {
> ? ?return( some.other.function(the.list.of.params.to.this.function));
> }
>
> Is there a way that I can use a loop to define all these functions?

How about something like this:

    funnames <- c('f1', 'f2', 'f3')

    funfactory <- function(n) {
      f <- function(...) NULL
      body(f) <- substitute(REPLACE(...), list(REPLACE=as.name(n)))
      f
    }

    funlist <- lapply(funnames, funfactory)

"funlist" is a list of function objects that wrap calls to the
named functions.

> In general, I'm looking for all the information I can find on the subject of
> dynamic function definition (i.e. using code to automate the definition of
> functions at runtime). ?I'm most interested in introspection facilities and
> dynamic code generation. ?E.g. is it possible to write a module that
> "redefines itself" when sourced? ?Or can a function redefine itself when
> first run? ?Or how can a function find out about how it was called?

You should read chapter 6 of the "R Language Definition" manual, which is
titled "Computing on the language".

-- 
Steve Weston
REvolution Computing
One Century Tower | 265 Church Street, Suite 1006
New Haven, CT  06510
P: 203-777-7442 x266 | www.revolution-computing.com



[Rd] Qs: The list of arguments, wrapping functions...
Steve Weston