Question

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.

Was it helpful?

Solution

How about

newmean <- function(X, ...){
  args <- as.list(substitute(list(...)))[-1L]
  z<-list(X)
  z<-c(z,args)
  do.call(mean,z)
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top