Is there a way to use two ‘…’ statements in a function in R?

An automatic way:

foo.plot <- function(x,y,...) {
    lnames <- names(formals(legend))
    pnames <- c(names(formals(plot.default)), names(par()))
    dots <- list(...)
    do.call('plot', c(list(x = x, y = x), dots[names(dots) %in% pnames]))
    do.call('legend', c("bottomleft", "bar", pch = 1, dots[names(dots) %in% lnames]))
}

pch must be filtered from the lnames to avoid duplication in the legend call in case the user supplies ‘pch’, but you got the idea.
Edited Jan 2012 by Carl W: “do.call” only works with the functions in quotes, as in the updates by Henrik. I edited it here to avoid future confusion.

Leave a Comment