[R] writing function with ,... )

Marc Schwartz marc_schwartz at comcast.net
Thu Nov 30 21:55:40 CET 2006


On Thu, 2006-11-30 at 21:10 +0100, Carmen Meier wrote:
> Hi to all
> I did not found the right hints for functions with the dot-dot-dot argument.
> Is it possible to write own functions with the tree dots and if yes 
> what's wrong with the following example?
> 
> 
> test <- function(x, ...)
> {
> print (x)
> if (exists("y"))print(y)
> if (exists("z"))print(z)
> }
> 
> test(4,y=2)
> 
> With regards Carmen
 
Carmen,

The problem is that 'y' and 'z' don't exist as R objects, so the result
of both tests is FALSE.

Try this:

test <- function(x, ...)
{
  print(x)
  dotargs <- list(...)
  if ("y" %in% names(dotargs)) print(dotargs$y)
  if ("z" %in% names(dotargs)) print(dotargs$z)
}

> test(4)
[1] 4

> test(4, y = 2)
[1] 4
[1] 2

> test(4, y = 2, z = 3)
[1] 4
[1] 2
[1] 3

HTH,

Marc Schwartz



More information about the R-help mailing list