R是否提供对象/函数/方法/关键字来获取所有函数参数?
用一个例子:
function(a, b="default", ...)
将提供a
和b
以及...
内部的功能环境。有没有一种说法类似list(...)
,这也将包括a
与b
在结果?
或者换另一种方式:一个速记list(a=a, b=b, ...)
,给function(a, b, ...)
R是否提供对象/函数/方法/关键字来获取所有函数参数?
用一个例子:
function(a, b="default", ...)
将提供a
和b
以及...
内部的功能环境。有没有一种说法类似list(...)
,这也将包括a
与b
在结果?
或者换另一种方式:一个速记list(a=a, b=b, ...)
,给function(a, b, ...)
Answers:
我认为您想要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
...
作为参数)?
c(as.list(environment()), list(...))
捕获默认args和提供的args的答案 。
一种解决方案是使用:
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
这将创建参数值的命名列表。
...
作为参数)?
试试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))
在寻找相关内容时偶然发现了这个问题。虽然我认识到这已经有好几年了,但答复似乎并不令人满意,并且似乎没有针对该问题的现成解决方案。
可以使用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
我相信您正在寻找formals
:
formals(sd)
$x
$na.rm
[1] FALSE
并dput
以此为您提供您在问题中指定的表格:
dput(formals(sd))
list(x = , na.rm = FALSE)
请注意,这formals
不适用于原始函数,仅适用于闭包。
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)
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软件包(v0.3.0)创建于2020-02-25