[R] problem with repeated formal arguments and ...

Sundar Dorai-Raj sundar.dorai-raj at pdf.com
Thu Aug 18 21:47:00 CEST 2005



Sundar Dorai-Raj wrote:
> 
> Ross Boylan wrote:
> 
>>I want to add an argument if it is not present.  Following the Green
>>Book, p. 337:
>>test <- function(x, ...){
>>  dots <- list(...)
>>  if (!hasArg(from))
>>    from <- 0
>>  else
>>    from <- dots$from
>>  curve(x, from=from, ...)
>>}
>>
>>
>>
>>>test(sin)
>>>test(sin, from=4)
>>
>>Error in curve(x, from = from, ...) : formal argument "from" matched by
>>multiple actual arguments
>>
>>The FAQ says, in the section on differences between R and S,
>>"R disallows repeated formal arguments in function calls."
>>
>>That seems a perfectly reasonable rule,  but how do I handle this
>>situation?
>>
> 
> 
> Hi, Ross,
> 
> Add "from" to your function:
> 
> test <- function(x, from = 0, ...) {
>    curve(x, from = from, ...)
> }
> 
> Or another way:
> 
> test <- function(x, ...) {
>    dots <- list(...)
>    if(!hasArg(from)) dots$from <- 0
>    dots$expr <- x
>    do.call("curve", dots)
> }
> 

I didn't check this example, but for curve, since `x' is an expression, 
we should actually do the following:

test <- function(x, ...) {
   dots <- list(...)
   if(!hasArg(from)) dots$from <- 0
   dots$expr <- substitute(x)
   invisible(do.call("curve", dots))
}

test(x^3 - 3 * x, to = 5)
test(x^3 - 3 * x, from = -5, to = 5)


HTH,

--sundar

> I actually prefer the latter if I'm changing many arguments. I do this 
> quite often when writing custom lattice plots and I want to override 
> many of the defaults.
> 
> HTH,
> 
> --sundar
> 
> ______________________________________________
> R-help at stat.math.ethz.ch mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html




More information about the R-help mailing list