这是一个关于如何从R中的子函数提供干净错误消息的简单问题。
通常,将call. = FALSE
添加到stop
时,如果用户显示来自函数的错误消息,则不会向用户显示周围的代码:
sub_function <- function() {
please <- "don't show this code"
stop("testing stop call equals false", call. = FALSE)
}
sub_function()
Error: testing stop call equals false
Called from: sub_function()
但是,如果你把它放在另一个函数里面,可以用mapply
调用它,它就不会尊重那个设置:
stop_test <- function(pkgs) {
sub_function <- function(pkg_name) {
please <- "don't show this code"
if (pkg_name == "ahah!") stop("testing stop call equals false", call. = FALSE)
}
mapply(sub_function, pkgs)
}
stop_test(c("dplyr", "ahah!"))
Error: testing stop call equals false
Called from: (function (pkg_name)
{
please <- "don't show this code"
if (pkg_name == "ahah!")
stop("testing stop call equals false", call. = FALSE)
})(dots[[1L]][[2L]])
如何让R只显示来自子函数调用的错误消息,如第二个例子?