11

I have run into a situation where I need to take all the extra arguments passed to an R function and roll them into an object for later use. I thought the previous question about ellipses in functions would help me, but I still can't quite grasp how to do this. Here is a very simple example of what I would like to do:

newmean <- function(X, ...){
  args <- as.list(substitute(list(...)))[-1L]
  return(mean(X, args))
}

I've tried a number of different formulations of args in the above example and tried unlisting args in the return call. But I can't make this work. Any tips?

I realize that I could do this:

newmean <- function(X, ...){
    return(mean(X, ...))
}

But I need to have the ... arguments in an object which I can serialize and read back into another machine.

Community
  • 1
  • 1
JD Long
  • 59,675
  • 58
  • 202
  • 294
  • I completely misunderstood your question so I delete my answer. One thing to add - use `substitute[-1L]` hack only when is needed, if you need values only then `list(...)` is sufficient, if you want pass arguments further then pass as `...`. – Marek Jun 30 '10 at 06:53

1 Answers1

12

How about

newmean <- function(X, ...){
  args <- as.list(substitute(list(...)))[-1L]
  z<-list(X)
  z<-c(z,args)
  do.call(mean,z)
}
Jyotirmoy Bhattacharya
  • 9,317
  • 3
  • 29
  • 38