[R] modify "..." (optional args)

Duncan Murdoch murdoch.duncan at gmail.com
Fri Oct 7 14:13:38 CEST 2011


On 11-10-07 4:39 AM, Pascal A. Niklaus wrote:
> Hi all,
>
> Is there a way to modify the optional arguments (...) passed to a
> function, so that these can be passed in modified form to a subsequent
> function call? I checked "Programming with Data" but could not find a
> solution there.

Not as such, but you get the same result using do.call.  For example:

test <- function(x, y, ...) {
   args <- list(x=x, y=y, ...)
   if (!hasArg(xlab)) args <- c(args, list(xlab="My label"))
   do.call(plot, args)
}

This has a disadvantage that the argument to plot has already been 
evaluated, so the y label shows the value rather than the expression 
test() received for y.  Presumably you can construct a call and evaluate 
it to get the labels you want; the tools are quote(), bquote(), 
substitute(), etc.

Duncan Murdoch

>
> What I'd like is something along these lines:
>
> test<- function(x,y,...) {
>     if(!hasArg(xlab)) { ___add xlab to ...___ }
>     if(hasArg(xlab)) { ___remove xlab from ...___ }  # alternative
>
>     plot(x,y,...)
> }
>
> Of course, I could use separate calls, like
>
> if(hasArg(xlab)) plot(x,y,...) else plot(x,y,xlab="label",...)
>
> but this gets complicated if there is more than one such case to consider.
>
> Thanks
>
> Pascal
>



More information about the R-help mailing list