[R] pass ... to multiple sub-functions
Dear list, I know I have seen this discussed before but I haven't been successful in searching for ellipsis, dots, ... in the archives. I would like to filter ... arguments according to their name, and dispatch them to two sub-functions, say fun1 and fun2. I looked at lm() but it seemed more complicated than I need as it modifies the calling function among other things. What is the best approach for this? My current version presented below seems very awkward. Best regards, baptiste sessionInfo() R version 2.9.2 (2009-08-24) i386-apple-darwin8.11.1 fun1 - function(col, row){ print(col) print(row) } fun2 - function(x){ print(x) } foo - function(..., lty=1){ dots - list(...) cl - match.call() col - eval.parent(cl$col) row - eval.parent(cl$row) params.fun1 - c(col, row) removed - na.omit(match(names(cl), params.fun1)) # index whichever arguments were passed to fun1 fun1(col, row) fun2(dots[seq_along(dots)[-removed]]) } foo() foo(col=1) foo(col=1, row=1, g=2, test = abc) __ R-help@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.
Re: [R] pass ... to multiple sub-functions
Dear Baptiste, I know I have seen this discussed before but I haven't been successful in searching for ellipsis, dots, ... in the archives. I would like to filter ... arguments according to their name, and dispatch them to two sub-functions, say fun1 and fun2. I looked at lm() but it seemed more complicated than I need as it modifies the calling function among other things. What is the best approach for this? My current version presented below seems very awkward. Best regards, baptiste sessionInfo() R version 2.9.2 (2009-08-24) i386-apple-darwin8.11.1 fun1 - function(col, row){ print(col) print(row) } fun2 - function(x){ print(x) } foo - function(..., lty=1){ dots - list(...) cl - match.call() col - eval.parent(cl$col) row - eval.parent(cl$row) params.fun1 - c(col, row) removed - na.omit(match(names(cl), params.fun1)) # index whichever arguments were passed to fun1 fun1(col, row) fun2(dots[seq_along(dots)[-removed]]) Instead of passing all remaining arguments stacked into one list (= 1 argument x to function fun2) you might want to be able to retain them as distinct arguments; so you might want to replace fun2 by fun2.a - function(...) print(list(...)) and the call to fun2 in foo by do.call(fun2.a, dots.remaining) where, sticking to your code, you could obtain dots.remaining as removed - na.omit(match(names(cl), params.fun1)) dots.remaining - dots[seq_along(dots)[-removed]] or by removed - c(lty,params.fun1) ## I assume you do not want to pass on argument lty... dots.remaining - cl[-1] ### remove the function name dots.remaining - dots.remaining[! names(dots.remaining) %in% removed] Best, Peter __ R-help@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.
Re: [R] pass ... to multiple sub-functions
2009/10/1 Peter Ruckdeschel peter.ruckdesc...@web.de: removed - c(lty,params.fun1) ## I assume you do not want to pass on argument lty... dots.remaining - cl[-1] ### remove the function name dots.remaining - dots.remaining[! names(dots.remaining) %in% removed] Best, Peter Thank you, that's very helpful. I vaguely remember there was also a package to deal with this problem (among other things). Have I dreamed that? Best, baptiste __ R-help@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.