获取所有参数列表

Luk*_*ebe 64 r

R是否提供了一个对象/函数/方法/关键字来获取所有函数参数?

用一个例子: function(a, b="default", ...)将提供a和b以及...功能的环境中.有没有一种说法类似list(...),这也将包括a与b在结果?

或者换一种方式:list(a=a, b=b, ...)给出一个简写function(a, b, ...)

Gre*_*now 57

我想你想要match.call:

tmpfun <- function(a,b,...) {
print(as.list(match.call()))
print(as.list(match.call(expand.dots=FALSE)))
}
> tmpfun(a=1, b=2, c=3, d=4)
[[1]]
tmpfun

$a
[1] 1

$b
[1] 2

$c
[1] 3

$d
[1] 4

[[1]]
tmpfun

$a
[1] 1

$b
[1] 2

$...
$...$c
[1] 3

$...$d
[1] 4
Run Code Online (Sandbox Code Playgroud)

  • 如果在函数调用中未覆盖默认值,则会失败.它不会使用默认设置列出参数但不会覆盖. (17认同)
  • 由于这些原因,接受的答案应该是来自 @user399470 的答案,它使用 `c(as.list(environment()), list(...))` 来捕获默认参数和提供的参数。 (3认同)
  • 当tmpfun从另一个包装函数接收到参数时,此方法不起作用。(列表中只有未评估的参数) (2认同)

小智 57

一种解决方案是使用:

tempf <- function(a, b = 2, ...) {
    argg <- c(as.list(environment()), list(...))
    print(argg)
}
tempf(1, c = 3)
$a
[1] 1

$b
[1] 2

$c
[1] 3
Run Code Online (Sandbox Code Playgroud)

这将创建一个命名的参数值列表.

  • 或者只是 print(as.list(environment())) 如果你没有 ... 作为参数 (3认同)
  • 这很好用,它比公认的答案有优势,它捕获传递给函数的 *values*,而不是可用于计算值的 *expressions*。如果您尝试保存 args 以便稍后调用该函数,则您可能需要这些值。 (3认同)
  • 可能值得指出的是,通过在函数的开头运行 `argg &lt;- c(as.list(environment()), list(...))` 来“捕获”参数通常很重要。否则,“environment()”最终将包含在“tempf()”中创建的其他变量 (3认同)

Jil*_*ina 10

尝试args功能

mean函数的论据是什么?

> args(mean)
function (x, ...) 
NULL
Run Code Online (Sandbox Code Playgroud)

lm功能怎么样?

    > args(lm)
function (formula, data, subset, weights, na.action, method = "qr", 
    model = TRUE, x = FALSE, y = FALSE, qr = TRUE, singular.ok = TRUE, 
    contrasts = NULL, offset, ...) 
NULL
Run Code Online (Sandbox Code Playgroud)

如果你想获得参数列表,请尝试

as.list(args(lm))
Run Code Online (Sandbox Code Playgroud)

  • 这似乎有效,但不在函数本身内... (3认同)

小智 7

在搜索相关内容时偶然发现了这个问题.虽然我意识到这已有几年的历史了,但回答似乎并不令人满意,而且似乎没有任何现成的解决方案.

使用formals和environment函数的组合可以制作(不优雅的)解决方法.下面的示例使用从形式中提取的名称从环境中提取参数,然后附加省略号列表.如果希望获得在函数调用时设置的值,请​​将orig_values参数设置为TRUE.该函数仅包括在函数调用时隐式或显式设置的变量.

allargs <- function(orig_values = FALSE) {
  # get formals for parent function
  parent_formals <- formals(sys.function(sys.parent(n = 1)))

  # Get names of implied arguments
  fnames <- names(parent_formals)

  # Remove '...' from list of parameter names if it exists
  fnames <- fnames[-which(fnames == '...')]

  # Get currently set values for named variables in the parent frame
  args <- evalq(as.list(environment()), envir = parent.frame())

  # Get the list of variables defined in '...'
  args <- c(args[fnames], evalq(list(...), envir = parent.frame()))


  if(orig_values) {
    # get default values
    defargs <- as.list(parent_formals)
    defargs <- defargs[unlist(lapply(defargs, FUN = function(x) class(x) != "name"))]
    args[names(defargs)] <- defargs
    setargs <- evalq(as.list(match.call())[-1], envir = parent.frame())
    args[names(setargs)] <- setargs
  }
  return(args)
}


tempf <- function(a, b = 2, ...) {
  d <- 5
  b <- 3

  cat("Currently set values defined in call or formals\n")
  print(allargs())
  cat("Values as defined at the time of the call\n")
  print(allargs(T))
}

tempf(1, c = 3)

Currently set values defined in call or formals
$a
[1] 1

$b
[1] 3

$c
[1] 3

Values as defined at the time of the call
$a
[1] 1

$b
[1] 2

$c
[1] 3
Run Code Online (Sandbox Code Playgroud)


Jam*_*mes 5

我相信您正在寻找formals:

formals(sd)
$x


$na.rm
[1] FALSE
Run Code Online (Sandbox Code Playgroud)

并dput以此为您提供您在问题中指定的形式:

dput(formals(sd))
list(x = , na.rm = FALSE)
Run Code Online (Sandbox Code Playgroud)

请注意,这formals不适用于原始函数,仅适用于闭包。

  • 这仅列出 _default_ 参数。 (2认同)