All valid values of an argument of a function in R

回眸只為那壹抹淺笑 提交于 2021-01-20 18:58:51

问题


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.

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?


回答1:


If the function of interest is defined like

f <- function(a = c("foo","bar")) {
    match.arg(a)
}

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")

Otherwise I don't think it is possible to get all valid argument values without RTFSing.



来源:https://stackoverflow.com/questions/20352842/all-valid-values-of-an-argument-of-a-function-in-r

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!