R中函数参数的所有有效值 [英] All valid values of an argument of a function in R
问题描述
假设我们有一个 R 函数,其参数必须从有限的元素集中选择.像 qplot(..., geom="")
.而 geom
只能取一些值,例如 bar
或 point
.
Suppose we have an R function whose arguments must be selected out of a finite set of elements. Like qplot(..., geom="")
. And geom
can take only some values, like bar
or point
.
如何找出给定函数的参数可能采用的所有有效值?除了经常错过所有可能值的文档或 Internet.也许,一些 R 函数可以提供帮助?
How can I find out all the valid values the argument of a given function may take? Apart from docs or Internet, which often miss all possible values. Perhaps, some R function can help?
推荐答案
如果感兴趣的函数是这样定义的
If the function of interest is defined like
f <- function(a = c("foo","bar")) {
match.arg(a)
}
即when the options are defined as a vector to be later checked with match.arg
function, then you could use the formals
function which would give you a list of arguments with the values就像下面的例子
i.e. when the options are defined as a vector to be later checked with match.arg
function, then you could use the formals
function which would give you a list of arguments with the values like in the following example
> formals(f)
$a
c("foo", "bar")
否则我认为没有 RTFSing 就不可能获得所有有效的参数值.
Otherwise I don't think it is possible to get all valid argument values without RTFSing.
这篇关于R中函数参数的所有有效值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!