简体   繁体   English

匹配使用match.arg()的默认值向量,有或没有错误[R]

[英]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: 我想写的函数应该具有以下属性:

  • it should have a vector as a default value (so the user can see which methods are available) 它应该有一个向量作为默认值(这样用户可以看到哪些方法可用)
  • if the argument is left at the default value, then the first of the two methods should be used 如果参数保留为默认值,则应使用两种方法中的第一种
  • if the user manually supplies a vector of methods, then the function should give an error 如果用户手动提供方法向量,则该函数应该给出错误

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 choicesargs相同,则返回第一个元素。 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.

相关问题 Rigraph degree()&#39;match.arg中的错误&#39; - R igraph degree() 'Error in match.arg' 警告:match.arg中的错误:&#39;arg&#39;必须为NULL或字符向量 - Warning: Error in match.arg: 'arg' must be NULL or a character vector 对多个 arguments 使用 match.arg 时出错 - Error in using match.arg for multiple arguments match.arg(regions) 中的错误:“arg”必须为 NULL 或字符向量 - Error in match.arg(regions) : 'arg' must be NULL or a character vector 与match.arg()匹配的数字参数 - Numeric argument matching with match.arg() 运行应用程序时出现闪亮错误:match.arg(position) 中的错误:&#39;arg&#39; 必须为 NULL 或字符向量 - Shiny Error when running App: Error in match.arg(position) : 'arg' must be NULL or a character vector match.arg(opt_crit)中的错误:“ arg”必须为NULL或字符向量 - Error in match.arg(opt_crit) : 'arg' must be NULL or a character vector Shiny 应用程序中的“match.arg(position) 错误:‘arg’必须为 NULL 或字符向量” - "Error in match.arg(position) : 'arg' must be NULL or a character vector" in Shiny app match.arg(p.adjust.method)中的错误:“ arg”必须为NULL或字符向量 - Error in match.arg(p.adjust.method) : 'arg' must be NULL or a character vector match.arg(position) 中的闪亮错误:“arg”必须为 NULL 或字符向量 - shiny Error in match.arg(position) : 'arg' must be NULL or a character vector
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM