Dustin Fife
2014-Feb-05 16:11 UTC
[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!
William Dunlap
2014-Feb-05 16:19 UTC
[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.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-bounces at r-project.org [mailto:r-help-bounces at 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 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.
arun
2014-Feb-05 16:35 UTC
[R] avoiding "multiple actual arguments" in default function behavior
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.