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

Kev*_*hey 3 r

可能重复:
使用substitute获取参数名称

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

例如:我想要一个可能就像:

f(apple, banana, car)
## --> returns c("apple", "banana", "car"), 
## ie, skips looking for the objects apple, banana, car
Run Code Online (Sandbox Code Playgroud)

我得到的最接近的是

f <- function(...) {
  return( deparse( substitute( ... ) ) )
}
Run Code Online (Sandbox Code Playgroud)

但这只会返回第一个被"抓住"的参数....思考?

ags*_*udy 6

f <- 
  function(...){
     match.call(expand.dots = FALSE)$`...`  
  }
Run Code Online (Sandbox Code Playgroud)

来自?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
Run Code Online (Sandbox Code Playgroud)

这里有一些测试:

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
Run Code Online (Sandbox Code Playgroud)

  • `match.call(expand.dots = F)$ \`... \``会稍微更惯用 (2认同)