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

Dustin Fife fife.dustin at gmail.com
Wed Feb 5 17:21:18 CET 2014


That's a good idea, but I'm hoping there's another way. The actual
function that I'm using sets LOTS of default behaviors and I try to
minimize the number of arguments I have.

On Wed, Feb 5, 2014 at 10:19 AM, William Dunlap <wdunlap at tibco.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.
>
> 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.




More information about the R-help mailing list