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
Try this:> test <- function(x, ...) {+ print(x) + dots <- list(...) + if ("y" %in% names(dots)) print(dots$y) + }> test(3, y = 43)[1] 3 [1] 43> test(4, z = 44)[1] 4>On 11/30/06, Carmen Meier <carmei3 at web.de> 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 > > ______________________________________________ > 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 > and provide commented, minimal, self-contained, reproducible code. >
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 CarmenCarmen, 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
Thanks, a lot I was not able to find it the hole day ... Carmen Phil Spector schrieb:> Carmen - > You certainly can write functions that use ..., but you need > to extract the arguments that the dots represent with list(). > Here's a modified version of your function that may help explain > how this feature works. > > test <- function(x,...){ > print(x) > args = list(...) > if('y' %in% names(args))print(args$y) > if('z' %in% names(args))print(args$z) > } > > - Phil Spector > Statistical Computing Facility > Department of Statistics > UC Berkeley > spector at stat.berkeley.edu > >