[R] Problem with the special argument '...' within a function
Jim Lemon
jim at bitwrit.com.au
Fri Aug 18 00:23:04 CEST 2006
Hans-Joerg Bibiko wrote:
> Dear all,
>
> I wrote some functions using the special argument '...'. OK, it works.
>
> But if I call such a function which also called such a function, then
> I get an error message about unused arguments.
>
> Here's an example:
>
> fun1 <- function(x,a=1)
> {
> print(paste("x=",x))
> print(paste("a=",a))
> }
> fun2 <- function(y,b=2)
> {
> print(paste("y=",y))
> print(paste("b=",b))
> }
> myfun <- function(c, ...)
> {
> print(paste("c=",c))
> fun1(x=c,...)
> fun2(y=c,...)
> }
>
> This is OK.
> > myfun(c=3)
> [1] "c= 3"
> [1] "x= 3"
> [1] "a= 1"
> [1] "y= 3"
> [1] "b= 2"
>
> > myfun(c=3,a=4)
> [1] "c= 3"
> [1] "x= 3"
> [1] "a= 4"
> Error in fun2(y = c, ...) : unused argument(s) (a ...)
>
> I understand the error message because fun2 has no argument called 'a'.
>
> But how can I avoid this???
>
Try Ben Bolker's "clean.args" in the plotrix package
> myfun(clean.args(list(c=3,a=4),myfun))
[1] "c= 3" "c= 4"
[1] "x= 3" "x= 4"
[1] "a= 1"
[1] "y= 3" "y= 4"
[1] "b= 2"
Jim
More information about the R-help
mailing list