R 按列表获取所有参数

R 按列表获取所有参数,r,R,R是否提供对象/函数/方法/关键字来获取所有函数参数 举个例子: 函数(a,b=“default”,…)将在函数环境中提供a和b以及…。是否有类似于list(…)的语句,也会在结果中包含a和b 或者换一种方式:一个列表(a=a,b=b,…),给定函数(a,b,…)试试args函数 mean函数的参数是什么 > args(mean) function (x, ...) NULL > args(lm) function (formula, data, subset, weig

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

举个例子:
函数(a,b=“default”,…)
将在函数环境中提供
a
b
以及
。是否有类似于
list(…)
的语句,也会在结果中包含
a
b


或者换一种方式:一个
列表(a=a,b=b,…)
,给定
函数(a,b,…)
试试
args
函数

mean
函数的参数是什么

> args(mean)
function (x, ...) 
NULL
    > 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
那么
lm
功能呢

> args(mean)
function (x, ...) 
NULL
    > 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))

我相信您正在寻找
正式版

formals(sd)
$x


$na.rm
[1] FALSE
在这个问题上使用
dput
可以得到您在问题中指定的形式:

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

请注意,
formals
不适用于基本函数,只适用于闭包。

我认为您需要
匹配。调用

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
tmpfun tmpfun(a=1,b=2,c=3,d=4)
[[1]]
tmpfun
一美元
[1] 1
b美元
[1] 2
$c
[1] 3
美元
[1] 4
[[1]]
tmpfun
一美元
[1] 1
b美元
[1] 2
$...
$…$c
[1] 3
$…$d
[1] 4

一种解决方案是使用:

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

tempf在寻找相关信息时偶然发现了这个问题。虽然我意识到这已经有好几年了,但答案似乎并不令人满意,而且似乎没有现成的解决方案

可以使用
形式
环境
功能的组合来进行(不雅观的)变通。下面的示例使用从形式中提取的名称从环境中提取参数,然后附加省略号列表。如果希望将值设置为函数调用时设置的值,请将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

allargs
test
rlang::fn_fmls
给出了一个简洁明了的解决方案:

库(ggplot2)
图书馆(rlang)
#行动
参数列表[1]“成对列表”
is.list(参数列表)
#>[1]是的
参数列表
#>$mapping
#>空的
#> 
#>$data
#>空的
#> 
#>$stat
#>[1]“身份”
#> 
#>美元头寸
#>[1]“身份”
#> 
#> $...
#> 
#> 
#>$na.rm
#>[1]错误
#> 
#>$show.legend
#>[1]NA
#> 
#>$inherit.aes
#>[1]是的

由(v0.3.0)于2020-02-25创建。

如果在函数调用中未覆盖默认值,则此操作将失败。它不会列出默认设置但未被覆盖的参数。您是否知道有一个包导出这样一个函数(只有
..
作为参数)?当tmpfun从另一个包装函数接收参数时,这不起作用。(列表中只有未计算的参数)出于这些原因,接受的答案应该是@user399470中使用
c(as.list(environment()),
捕获默认参数和提供的参数的答案。您知道有一个包导出这样一个函数(只有
作为参数)?还是简单地打印(as.list(environment()))如果您没有…因为Arguments非常好用,它比公认的答案有一个优势,即它捕获传递给函数的值,而不是可以用来计算值的表达式。如果您试图保存参数以便稍后调用函数,您可能需要这些值。值得指出的是在这一阶段,“捕获”通常很重要运行
argg的参数这只列出了默认参数。与已提出的解决方案相比,使用此方法的好处是什么?特别是,这似乎与用户399470的答案非常相似。这个问题的问题是不清楚您在问什么。您是在问如何获得1)调用函数时使用的值;2) 调用函数时使用的表达式;3) 函数定义中的默认值?你的问题一点也不清楚,所以这三个选项都有三种不同的答案。更糟糕的是,更多的函数不配合args调用,比如predict。所以骗子不得不到别处去记住你试图记住的一个参数/标志。