[Rd] Expanding partial names
Duncan Murdoch
murdoch at stats.uwo.ca
Wed Mar 8 03:18:14 CET 2006
Okay, here's my effort based on Deepayan's and Charles' ideas. The
newArgs function is not what I'd call transparent, but I like the way
the wrapper looks.
> newArgs <- function(..., Params) {
+ f <- function(...) list(...)
+ formals(f) <- c(Params, formals(f))
+ b <- as.list(body(f))
+ body(f) <- as.call(c(b[1], names, b[-1]))
+ f(...)
+ }
>
> lowlevel <- function(longname = 1) {
+ cat("longname = ", longname, "\n")
+ }
>
> newDefaults <- list(longname=2)
>
> wrapper <- function (...)
+ do.call("lowlevel", newArgs(..., Params=newDefaults))
newArgs sets up f to look like
function (longname = 2, ...) list(longname = longname, ...)
and then calls it. The thing I like about this, as opposed to using
pmatch, is that I'm sure the partial matching is what's used by R's
argument matching, whereas that's only pretty likely with pmatch.
I also sort of like these lines:
+ names <- as.list(names(Params))
+ names(names) <- names
+ names <- lapply(names, as.name)
but maybe I should have named Params as names, so they looked like this:
+ names <- as.list(names(names))
+ names(names) <- names
+ names <- lapply(names, as.name)
And of course I like the fact that this seems to work, but we've seen
several versions that do that:
> wrapper()
longname = 2
> wrapper(longname=3)
longname = 3
> wrapper(long=3)
longname = 3
> wrapper(long=20)
longname = 20
> wrapper(junk=20)
Error in lowlevel(longname = 2, junk = 20) :
unused argument(s) (junk ...)
Duncan Murdoch
More information about the R-devel
mailing list