[R] pass "..." to multiple sub-functions

baptiste auguie baptiste.auguie at googlemail.com
Thu Oct 1 11:45:45 CEST 2009


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")




More information about the R-help mailing list