[R] pass "..." to multiple sub-functions
Peter Ruckdeschel
peter.ruckdeschel at web.de
Thu Oct 1 15:52:15 CEST 2009
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
More information about the R-help
mailing list