[英]Matching vector of default values using match.arg() with or without error [R]
I want to write a function that applies one of two different statistical methods to its input. 我想编写一个函数,将两种不同的统计方法之一应用于其输入。 In the process, I noticed some behavior of different functions that I do not understand.
在这个过程中,我注意到了一些我不理解的不同功能的行为。 The function I want to write should have the following properties:
我想写的函数应该具有以下属性:
Basically, I want the function to behave like cor
does in R. There, you have a default value method = c("pearson", "kendall", "spearman")
, and the functions calculated the Pearson correlation if method
isn't specified. 基本上,我希望函数的行为与R中的
cor
一样。在那里,你有一个默认值method = c("pearson", "kendall", "spearman")
,如果method
不是,则函数计算出Pearson相关性指定。 If the user asks for several methods at once, the function returns an error. 如果用户一次请求多个方法,则该函数返回错误。
From looking at cor
, this appears to be done using match.arg(method)
. 通过查看
cor
,这似乎是使用match.arg(method)
。 This behavior is illustrated here: 此行为如下所示:
x <- y <- 1:5
cor(x, y, method="pearson")
# = 1
cor(x, y, method="kendall")
# = 1
cor(x, y, method=c("pearson","kendall"))
# gives an error
I tried writing my own function, also using match.arg(method)
, but I realized that the result is somehow different. 我尝试编写自己的函数,也使用
match.arg(method)
,但我意识到结果有些不同。 Even when choosing a vector for method
, the function doesn't terminate with an error, but returns the results of the first method. 即使为
method
选择向量,该函数也不会以错误终止,而是返回第一个方法的结果。
This is illustrated here: 这在这里说明:
myfun <- function(x, method=c("add","multiply")){
method <- match.arg(method)
if(method=="add") return(sum(x))
if(method=="multiply") return(prod(x))
}
x <- 1:5
myfun(x, method="add")
# = 15
myfun(x, method="multiply")
# = 120
myfun(x, method=c("add","multiply"))
# = 15
I don't understand this behavior, and I would be glad if you could help me out here. 我不明白这种行为,如果你能帮助我,我会很高兴的。 From my attempts on Google, I realize that it might be related to non-standard evaluation, but I can't put two and two together just yet.
从我在谷歌的尝试中,我意识到它可能与非标准评估有关,但我还不能把两个和两个放在一起。
Thanks in advance, your help is much appreciated! 在此先感谢,非常感谢您的帮助!
Cheers! 干杯!
EDIT: 编辑:
I could also re-phrase my question: 我也可以重新说出我的问题:
What powerful sorcery does cor
do that it returns the Pearson correlation when method
is not supplied, but that it returns an error when method = c("pearson", "kendall", "spearman")
is explicitly specified? 没有什么强大的巫术
cor
做它返回时Pearson相关method
没有提供,但是当它返回一个错误method = c("pearson", "kendall", "spearman")
明确规定?
If choices
and args
are the same in match.arg
, then the first element is returned. 如果
match.arg
choices
和args
相同,则返回第一个元素。 Otherwise arg
has to be length 1. From match.arg
: 否则
arg
必须是长度1.来自match.arg
:
Since default argument matching will set arg to choices, this is allowed as an exception to the 'length one unless several.ok is TRUE' rule, and returns the first element.
由于默认参数匹配会将arg设置为选项,因此允许将此作为“length one除非:many.ok为TRUE”规则的例外,并返回第一个元素。
match.arg(c("pearson", "kendall", "spearman"), c("pearson", "kendall", "spearman"))
## [1] "pearson"
match.arg(c("pearson", "kendall"), c("pearson", "kendall", "spearman"))
## Error in match.arg(c("pearson", "kendall"), c("pearson", "kendall", "spearman")) :
## 'arg' must be of length 1
You can get your desired behavior using a dummy argument: 您可以使用伪参数获得所需的行为:
myfun <- function(x, method=c("add","multiply","other.return.error")){
method <- match.arg(method)
if("other.return.error" %in% method) stop("this option should not be used")
if(method=="add") return(sum(x))
if(method=="multiply") return(prod(x))
}
The main question was answered by @shadow (see above). @shadow回答了主要问题(见上文)。
Another way of getting the desired behavior for myfun
is to perform a check if method
is supplied or not and printing an error if it was explicitly supplied with more than one element. 获得
myfun
所需行为的另一种方法是执行检查是否提供method
,如果显式提供了多个元素,则打印错误。
myfun <- function(x, method=c("add","multiply")){
if(!missing(method) & length(method)>1) stop("Only one 'method' allowed.")
method <- match.arg(method)
if(method=="add") return(sum(x))
if(method=="multiply") return(prod(x))
}
x <- 1:5
myfun(x)
# = 15
myfun(x, method="add")
# = 15
myfun(x, method="multiply")
# = 120
myfun(x, method=c("add","multiply"))
# gives error
This circumvents the exception in match.arg
pointed out by @shadow by which supplying a vector to the function may not cause an error. 这绕过了@shadow指出的
match.arg
的异常,通过该异常向函数提供向量可能不会导致错误。 Instead, this error is given right away. 相反,这个错误立即给出。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.