从任何函数中提取函数参数和默认值 [英] Extract function parameters and default values from any function
本文介绍了从任何函数中提取函数参数和默认值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
有没有办法从函数的外部提取任何给定函数的参数及其各自的默认值?
Is there a way to extract the parameters and their respective default values of any given function from outside the function?
例如,给定:
myfunc <- function(a, b = 1) { print(c(a, b)) }
我正在寻找一些会返回的函数:
I'm looking for some function that will return:
list(a = NULL, b = 1)
或其一些变体.
推荐答案
您正在寻找 formals()
.
formals(myfunc)
# $a
#
#
# $b
# [1] 1
如果a
需要NULL
,你可以做一些检查.a
将属于name"类并且为空.
If you needed NULL
for a
, you could do some checking. a
will be of the "name" class and empty.
lapply(formals(myfunc), function(x) if(is.name(x) & !nzchar(x)) NULL else x)
# $a
# NULL
#
# $b
# [1] 1
这篇关于从任何函数中提取函数参数和默认值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文