[R] avoiding "multiple actual arguments" in default function behavior

arun smartpink111 at yahoo.com
Wed Feb 5 17:35:24 CET 2014


Hi,
Try:

 plotx <- function(x,y, ...){
 labels <- list(xlab="x",ylab="y")
 args <- modifyList(labels,list(x=x,...))
 do.call("plot",args)
 } 

 plotx(1:100,1:100,xlab="I Don't Work!")
A.K.


On Wednesday, February 5, 2014 11:14 AM, Dustin Fife <fife.dustin at gmail.com> wrote:
Suppose I'm creating a function that sets default ylab and xlab behaviors:

plotx = function(x, y, ...){
    plot(x,y, ylab="", xlab="",...)
}

The problem is, on occasion, I actually want to override the defaults
in my function. I would like to do the following:

plotx(1:100, 1:100, xlab="I Don't Work!")


But I get the "multiple actual arguments" error message (which makes
sense). Does anyone know how to set a default (like plotx does), but
allow it to change if the user specifies the same argument via ...?

I tried doing something like this:

plotx = function(x, y, ...){
    args = list(...)
    yl = ifelse(!is.null(args$ylab), args$ylab, "")
    xl = ifelse(!is.null(args$xlab), args$xlab, "")
    plot(x,y, ylab=yl, xlab=xl,...)
}

but got the same error. I then started thinking that maybe I can
remove the ylab and xlab arguments from ..., but I don't know how I'd
do that. Any ideas of how to remove it? Or, is there a better way of
doing this? Thanks!

______________________________________________
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
and provide commented, minimal, self-contained, reproducible code.





More information about the R-help mailing list