[R] Messing with the "..." argument
Wacek Kusnierczyk
Waclaw.Marcin.Kusnierczyk at idi.ntnu.no
Thu Feb 12 22:11:20 CET 2009
here's an example which may give you a hint -- it is not supposed to
solve your particular problem:
f = function(...)
list(...)
g = function(...) {
args = list(...)
names = names(args)
if (is.null(names))
f(...)
else
do.call(f, args[names(args) != 'foo']) }
g()
# list()
g(1)
# list(1)
g(a=1)
# list(a=1)
g(a=1, foo=2)
# list(a=1)
g(1, foo=2)
# list(1)
g(foo=1)
# list()
consider using grep for finding the variables to be removed.
vQ
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?
>
> Thanks for any help,
> -steve
>
> ______________________________________________
> R-help at r-project.org mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
More information about the R-help
mailing list