R: using a list for ellipsis arguments

五迷三道 提交于 2019-11-29 09:00:02

问题


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.


回答1:


How about

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


来源:https://stackoverflow.com/questions/3142731/r-using-a-list-for-ellipsis-arguments

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!