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

Steve Weston steve at revolution-computing.com
Tue May 19 23:05:26 CEST 2009


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



More information about the R-devel mailing list