gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-04 11:18:06 25 4
gpt4 key购买 nike

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

正如 Wickham 的 Advanced R 中所述:

Using ... comes at a price — any misspelled arguments will not raise an error, and any arguments after ... must be fully named. This makes it easy for typos to go unnoticed.



下面是一个例子:
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.

在这里, toLower 的微妙拼写错误正如用户可能预期的那样,除了输出为小写失败之外,不会导致任何可见错误。在最后一个例子中,用户认为有用的参数不是,但由于 ... 的性质,它们只是消失在以太中。 .

当然我可以查看 ...的内容通过 list(...) ,但我想知道是否有更好的方法,链接到函数中定义的形式或那些 ...通过。

顺便说一句,有谁知道我们应该怎么称呼 ... ?我几乎可以肯定我在 Julia 文档中看到了一个带有名称的引用,但现在找不到了!

补充:

理想情况下,当被调用函数范围内的某些内容通过省略号传递给辅助参数时,如果参数不是应该在省略号中传递的内容,我想找出生成错误的最佳方法。例如,如何最好地捕获这些错误:
myfun2 <- function(x, ...) 
UseMethod("my fun")

myfun2.character <- function(x, toLower = FALSE, ...)
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
myfun2("Test String with CaPiTaLs.", toLower = TRUE, notDefined = 1)
## Sent and transformed by myfun: test string with capitals.
## 1
myfun2("Test String with CaPiTaLs.", sep = "\tXX\t")
## Sent and transformed by myfun: XX Test String with CaPiTaLs. XX

最佳答案

我不会返回错误。一个警告就足够了。像这样的东西:

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

我不认为最后一个应该警告。无论如何你应该避免位置参数匹配,如果你在那里使用名称匹配,你会得到一个错误,你需要捕获。

关于r - 在定义中使用省略号时如何在 R 函数调用中捕获错误或未定义的参数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33693500/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com