在定义中使用省略号时,如何在R函数调用中捕获错误或未定义的参数

在定义中使用省略号时,如何在R函数调用中捕获错误或未定义的参数,r,function,error-handling,R,Function,Error Handling,我正在努力解决一个已被承认但尚未找到简单解决方案的问题:当函数定义包含省略号或三点…时,如何捕获传递给R函数的错误指定参数 正如韦翰的高级R中所述: 使用…是要付出代价的-任何拼写错误的参数都不会引起争议 错误,…之后的任何参数必须完整命名。这使得 打字错误很容易被忽视 下面是一个例子: myfun <- function(x, ...) UseMethod("myfun") myfun.character <- function(x, toLower = FALSE,

我正在努力解决一个已被承认但尚未找到简单解决方案的问题:当函数定义包含省略号或三点
时,如何捕获传递给R函数的错误指定参数

正如韦翰的高级R中所述:

使用
是要付出代价的-任何拼写错误的参数都不会引起争议 错误,
之后的任何参数必须完整命名。这使得 打字错误很容易被忽视

下面是一个例子:

myfun <- function(x, ...) 
    UseMethod("myfun")

myfun.character <- function(x, toLower = FALSE, ...)
    cat("Sent and transformed by myfun:", ifelse(toLower, tolower(x), x), "\n")

myfun("Test String with CaPiTaLs.")
## Sent and transformed by myfun: Test String with CaPiTaLs.
myfun("Test String with CaPiTaLs.", toLower = TRUE)
## Sent and transformed by myfun: test string with capitals. 
myfun("Test String with CaPiTaLs.", tolower = TRUE)
## Sent and transformed by myfun: Test String with CaPiTaLs.
myfun("Test String with CaPiTaLs.", toLower = TRUE, notDefined = 1)
## Sent and transformed by myfun: test string with capitals. 

我不会返回错误。一个警告就足够了。大概是这样的:

myfun2.character <- function(x, toLower = FALSE, ...) {
  elli <- names(list(...))
  check <- elli %in% names(formals(cat))
  if (any(!check)) warning(sprintf("%s is not an expected parameter. Did you misstype it?", 
                                   paste(elli[!check], collapse = ", ")))
  cat("Sent and transformed by myfun:", ifelse(toLower, tolower(x), x), "\n", ...)
}


myfun2("Test String with CaPiTaLs.", tolower = TRUE)
#Sent and transformed by myfun: Test String with CaPiTaLs. 
#TRUE
#Warning message:
#  In myfun2.character("Test String with CaPiTaLs.", tolower = TRUE) :
#  tolower is not an expected parameter. Did you misstype it?
myfun2("Test String with CaPiTaLs.", toLower = TRUE, notDefined = 1)
#Sent and transformed by myfun: test string with capitals. 
#1
#Warning message:
#  In myfun2.character("Test String with CaPiTaLs.", toLower = TRUE,  :
#                        notDefined is not an expected parameter. Did you misstype it?
myfun2("Test String with CaPiTaLs.", sep = "\tXX\t")
## Sent and transformed by myfun:   XX  Test String with CaPiTaLs.  XX

myfun2.character是否要在
myfun.character
中使用
..
?如果不是,检查可以是
If(length(list(…))stop(…)
。它被称为省略号。在这个例子中,我看到了两个选项:不要为
toLower
提供默认值,也不要将可选拼写作为参数,例如
toLower=toLower
。这是一个非常简单的例子,但我正在考虑如何在一个具有较长函数签名和大量命名形式的包中管理它。我担心如果我试图在
列表(…)
内容上设置手动检查,那么当我的检查与函数签名不一致时,我会增加出错的可能性。我认为S4可能会有所帮助,但它不会。你能澄清一下你想要实现什么吗?
myfun(“用大写字母测试字符串”,tolower=TRUE)
应该返回什么?为什么要返回它?现在你的问题看起来像是你同时想要使用而不是使用它。非常有意义,谢谢。也找到了此讨论:。还有一些不错的选择。
myfun2.character <- function(x, toLower = FALSE, ...) {
  elli <- names(list(...))
  check <- elli %in% names(formals(cat))
  if (any(!check)) warning(sprintf("%s is not an expected parameter. Did you misstype it?", 
                                   paste(elli[!check], collapse = ", ")))
  cat("Sent and transformed by myfun:", ifelse(toLower, tolower(x), x), "\n", ...)
}


myfun2("Test String with CaPiTaLs.", tolower = TRUE)
#Sent and transformed by myfun: Test String with CaPiTaLs. 
#TRUE
#Warning message:
#  In myfun2.character("Test String with CaPiTaLs.", tolower = TRUE) :
#  tolower is not an expected parameter. Did you misstype it?
myfun2("Test String with CaPiTaLs.", toLower = TRUE, notDefined = 1)
#Sent and transformed by myfun: test string with capitals. 
#1
#Warning message:
#  In myfun2.character("Test String with CaPiTaLs.", toLower = TRUE,  :
#                        notDefined is not an expected parameter. Did you misstype it?
myfun2("Test String with CaPiTaLs.", sep = "\tXX\t")
## Sent and transformed by myfun:   XX  Test String with CaPiTaLs.  XX