88

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

举个例子: function(a, b="default", ...)将提供ab以及...在函数环境中。是否有类似的声明list(...)也会在结果中包含ab

或者换一种说法:list(a=a, b=b, ...)给定的简写function(a, b, ...)

4

7 回答 7

85

一种解决方案是使用:

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

这将创建参数值的命名列表。

于 2013-06-21T20:46:18.393 回答
72

我认为你想要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
于 2012-08-09T22:07:19.347 回答
17

尝试args功能

函数的参数是mean什么?

> args(mean)
function (x, ...) 
NULL

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

如果您想获取参数列表,请尝试

as.list(args(lm))
于 2012-08-09T14:06:00.303 回答
9

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

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
于 2017-12-23T20:39:29.650 回答
8

我相信您正在寻找formals

formals(sd)
$x


$na.rm
[1] FALSE

dput在此上使用为您提供了您在问题中指定的形式:

dput(formals(sd))
list(x = , na.rm = FALSE)

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

于 2012-08-09T14:36:04.653 回答
4

rlang::fn_fmls给出了一个简短而干净的解决方案:

library(ggplot2)
library(rlang)

# action
argument_list <- rlang::fn_fmls(fn = geom_point)

# evaluate output
class(argument_list)
#> [1] "pairlist"

is.list(argument_list)
#> [1] TRUE

argument_list
#> $mapping
#> NULL
#> 
#> $data
#> NULL
#> 
#> $stat
#> [1] "identity"
#> 
#> $position
#> [1] "identity"
#> 
#> $...
#> 
#> 
#> $na.rm
#> [1] FALSE
#> 
#> $show.legend
#> [1] NA
#> 
#> $inherit.aes
#> [1] TRUE

reprex 包于 2020-02-25 创建(v0.3.0)

于 2020-02-25T21:59:08.717 回答
2
test <- function(
  x = 1,
  y = 2,
  ...
) {
  if(length(list(...)) == 0) {
    print(as.list(environment()))
  } else {
    print(c(as.list(environment()), list(...)))
  }
}

test()
test(z = 3)
于 2019-03-08T19:13:47.543 回答