Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Avoid argument duplication passed through (...)

Tags:

r

I have a function

somefun <- function(someparameters , ...) { plot(stuff, ...)}

Now I would like to provide some defaults for plot in the case that the user hasn't specified those arguments. (i.e. xlab="").

How do I provide a set of default plot options but still allow the user to override those arguments? Since if the same argument is inputted twice, R will throw the error: formal argument matched by multiple actual arguments.

I am aware that I can pass on all these options through my function

somefun <- function(someparameters, main, xlab, ylab, xlim....)

but I would rather not do that.

Is there some easy neat solution to achieve this?

like image 498
LostLin Avatar asked Dec 20 '11 19:12

LostLin


1 Answers

Try modifyList used as follows:

f <- function(x, ...) {
    defaults <- list(xlab = "x", ylab = "y")
    args <- modifyList(defaults, list(x = x, ...))
    do.call("plot", args)
}
like image 51
G. Grothendieck Avatar answered Oct 12 '22 00:10

G. Grothendieck