[R] Messing with the "..." argument
Duncan Murdoch
murdoch at stats.uwo.ca
Thu Feb 12 21:24:53 CET 2009
On 2/12/2009 2:34 PM, Steve Lianoglou wrote:
> Hi all,
>
> Sorry if this is documented somewhere, but trying to search and google for
> ways to alter my "..." argument is having me chasing my tail.
>
> Is there someway that I can manipulate the elements in "..."?
> Specifically I'd like to use certain vars in "...", then remove them
> from "..." and pass the rest of "..." to another function.
>
> Here's a short example: I'm adding more functionality to a generic
> method by defining how it works on a new class "MyClass":
>
> setMethod('existingFunction', 'MyClass', function (object, ...) {
> vars <- list(...)
> doSomethingWith(vars$myCustomVar)
>
> # now I want to pass the rest of the vars down to another function,
> # let's say some plotting function, so I'd like to remove
> # `myCustomVar` from "...", keep the rest of the vars and just
> # pass the valid "..." elements down through the call chain
>
> doSomePlot(somethingSane, ...)
> })
>
> Currently the `doSomePlot` that's called does fine until it passes the
> rest of it's vars to the "real" plot method, at which point I'll get
> a warning like:
>
> Warning messages:
> 1: In plot.window(...) : "myCustomVar" is not a graphical parameter
> 2: In plot.xy(xy, type, ...) : "myCustomVar" is not a graphical parameter
> 3: In axis(side = side, at = at, labels = labels, ...) :
> "myCustomVar" is not a graphical parameter
> 4: In axis(side = side, at = at, labels = labels, ...) :
> "myCustomVar" is not a graphical parameter
> 5: In box(...) : "myCustomVar" is not a graphical parameter
> 6: In title(...) : "myCustomVar" is not a graphical parameter
>
> Depending on what the name of myCustomVar is, this can work even with
> the warnings, which is passable. It might also bail if myCustomVar
> happens to share a name with a real plotting var, but has an illegal
> value for it.
>
> Anyway, while there are ways it can work in this case, I'm just wondering
> if I can remove elements from "..." and side-step these warning/error issues
> altogether.
>
> Since "vars <- list(...)" works to create a list of arguments from "...",
> I'm guessing there must be a way to take an already existing list variable
> and turn its name/value pairs into an appropriate "..." object?
I don't think so, but an alternative is to put together a parameter list
and call do.call(). For example,
> g <- function(...) {
+ list(...) # just return the arg list to see what arrived
+ }
>
> f <- function(...) {
+ all <- list(...)
+ all["nothanks"] <- NULL
+ do.call("g", all)
+ }
>
> g(nothanks=1, yesplease=2)
$nothanks
[1] 1
$yesplease
[1] 2
> f(nothanks=1, yesplease=2)
$yesplease
[1] 2
Duncan Murdoch
More information about the R-help
mailing list