简体   繁体   English

R:使用列表作为省略号参数

[英]R: using a list for ellipsis arguments

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. 我遇到了一种情况,我需要将所有额外的参数传递给R函数并将它们滚动到一个对象中供以后使用。 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. 在上面的例子中我尝试了许多不同的args配方,并在返回调用中尝试了未列出的args。 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. 但我需要在一个对象中有...参数,我可以将其序列化并读回另一台机器。

How about 怎么样

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM