[R] How to solve: Error in * unused argument(s) ?

Uwe Ligges ligges at statistik.tu-dortmund.de
Wed Feb 10 19:35:56 CET 2010



On 10.02.2010 18:58, Tal Galili wrote:
> Hi all,
> For some reason, I would like to use functions bellow (see example code
> bellow), but instead I get the following error message:
> *Error in foo2(...) : unused argument(s) (arg3 = 3)*
>
>
> #---------------------
> # example code
> #---------------------
> foo1<- function(arg1,...)
> {
> print(arg1)
>   foo2(...)
> foo3(...)
> }
>
> foo2<- function(arg2)
> {
> print(arg2)
> }
>
> foo3<- function(arg3)
> {
> print(arg3)
> }
>
>
> foo1(arg1 = 1, arg2 = 2, arg3  =3)
>
> #---------------------
>
>
> I tried looking through the R Language Definition, but couldn't find
> something that helped me understand the issue (way it should happen, and
> what is a smart strategy to avoid it)

With "..." you are passing arg2 and arg3 to foo2() which only expects 
one argument.

If you have rather different arguments to be passed to more than 1 
function, you may want to

- either list all arguments as arguments in the calling function (here 
foo2) or

- restrict by calculating on
  args <- list(...)
before the foo2(...) call that need to be replaced by
  do.call("foo2", args)
or

- invent two arguments for foo1 such as:

foo1<- function(arg1, foo2args = list(), foo3args = list())
{
   print(arg1)
   do.call("foo2", foo2args)
   do.call("foo3", foo3args)
}

foo2<- function(arg2)
{
   print(arg2)
}

foo3<- function(arg3)
{
   print(arg3)
}

foo1(arg1 = 1, foo2args=list(arg2 = 2), foo3args=list(arg3  =3))

This way it is still possible to collect various arguments to a function 
without defining all of them explicitly in the formal definition of foo1.

Uwe Ligges


>
> Any insight will be greatly appreciated.
> Thanks,
> Tal
>
> 	[[alternative HTML version deleted]]
>
> ______________________________________________
> 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