如何在函数中获取传递给...的参数的名称?

时间:2012-12-07 19:32:13

标签: r

  

可能重复:
  using substitute to get argument name with

请注意,这与 - 使用list(...)或某种形式的某些东西获取向量本身是不同的。在完成任何解析之前,我希望能够做的只是'echo'传递给...的所有参数。

例如:我想要的功能可能就像:

f(apple, banana, car)
## --> returns c("apple", "banana", "car"), 
## ie, skips looking for the objects apple, banana, car

我最接近的是

f <- function(...) {
  return( deparse( substitute( ... ) ) )
}

但这仅返回...的第一个参数'catch'。想法?

1 个答案:

答案 0 :(得分:6)

f <- 
  function(...){
     match.call(expand.dots = FALSE)$`...`  
  }

来自?match.call的一些探索:

 1. match.call returns a call in which all of the specified arguments are specified by their full names .
 2. Here it is used to pass most of the call to another function, often model.frame. 
    Here the common idiom is that expand.dots = FALSE

这里有一些测试:

f(2)        # call of a static argument  
[[1]]
[1] 2

> f(x=2)  # call of setted argument
$x
[1] 2

> f(x=y)  # call of symbolic argument
$x
y