R中函数自变量的所有有效值 [英] All valid values of an argument of a function in R

查看:94
本文介绍了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)
}

即如果将选项定义为向量,以便稍后使用 match.arg 函数进行检查,则可以使用 formals 函数,该函数将为您提供带有值的参数列表就像下面的示例一样

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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆