> 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.
Make xlab and ylab arguments to your function. plotx2 <- function(x, y, ..., xlab="", ylab="") { plot(x, y, xlab=xlab, ylab=ylab, ...) } (I put them after the ... in the argument list so they must be fully named in the call.) Bill Dunlap TIBCO Software wdunlap tibco.com > -----Original Message----- > From: r-help-boun...@r-project.org [mailto:r-help-boun...@r-project.org] On > Behalf > Of Dustin Fife > Sent: Wednesday, February 05, 2014 8:11 AM > To: r-help > Subject: [R] avoiding "multiple actual arguments" in default function behavior > > 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@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. ______________________________________________ R-help@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.