我的包中有一个功能应该谨慎使用。
用户应该意识到这一点,但如果他/她认为情况正常,那么每次调用该函数时都会显示警告。
我经常看到只显示一次的警告。调试它们非常痛苦,所以我找不到可重现的示例(如果有的话,我会添加一个)但它们显示特定的警告消息,然后是rlang信息:
此警告每个会话显示一次
有很多帮助想要调试这些消息(例如这里,这里,或者这里,只是谷歌“r 这个警告每个会话显示一次”)
我认为该软件包lifecyle经常使用那些进行软弃用,但我无法在lifecycle:::lifecycle_build_message.
我怎样才能在我的包裹中抛出这样的警告?
编辑:
这是一个可重现的示例。您必须重新启动 R 会话才能再次显示。如您所见,options(warn=2)没有影响。
options(warn=2)
xx=c("Sepal.Width")
tidyselect::vars_select(names(iris), xx)
Run Code Online (Sandbox Code Playgroud)
在 的情况下tidyselect::vars_select,诀窍在于tidyselect:::inform_once。
if (env_has(inform_env, id)) {
return(invisible(NULL))
}
inform_env[[id]] <- TRUE
# ....
inform(paste_line(
msg, silver("This message is displayed once per session.")
))
Run Code Online (Sandbox Code Playgroud)
inform_env维护一个记录给定消息是否已显示的环境。
在 的情况下lifecycle,它的工作原理与deprecation_env使用的环境类似deprecate_warn
deprecate_warn <- function(....) {
msg <- lifecycle_build_message(when, what, with, details, "deprecate_warn")
# ....
if (verbosity == "quiet") {
return(invisible(NULL))
}
if (verbosity == "default" && !needs_warning(id) && ....) {
return(invisible(NULL))
}
# ....
if (verbosity == "default") {
# Prevent warning from being displayed again
env_poke(deprecation_env, id, Sys.time());
msg <- paste_line(
msg,
silver("This warning is displayed once every 8 hours."),
silver("Call `lifecycle::last_warnings()` to see where this warning was generated.")
)
}
# ....
}
needs_warning <- function(id) {
last <- deprecation_env[[id]]
if (is_null(last)) {
return(TRUE)
}
# ....
# Warn every 8 hours
(Sys.time() - last) > (8 * 60 * 60)
}
Run Code Online (Sandbox Code Playgroud)
2021 年中期更新:
\n现在有一个内置选项{rlang}。请参阅此处的帮助。
rlang::warn("This message is displayed once per session.", .frequency = "once")\nRun Code Online (Sandbox Code Playgroud)\n原答案:
\n虽然 Aur\xc3\xa8le\ 的答案显然赢得了比赛,tidyselect\ 的函数并不完全符合我的需求,因为它需要一些未导出的函数。
对于那些想要在他们的包中使用简单函数的人来说,这是我的:
\n#\' @importFrom rlang env env_has inform\n#\' @importFrom crayon silver has_color\n#\' @author tidyselect (https://github.com/r-lib/tidyselect/blob/2fab83639982d37fd94914210f771ab9cbd36b4b/R/utils.R#L281)\nwarning_once = function(msg, id=msg) {\n stopifnot(is_string(id))\n \n if (env_has(warning_env, id)) {\n return(invisible(NULL))\n }\n inform_env[[id]] = TRUE\n \n x = "This message is displayed once per session."\n if(is_installed("crayon") && crayon::has_color())\n x=crayon::silver(x)\n warn(paste(msg, x, sep = "\\n"))\n}\nwarning_env = rlang::env()\nRun Code Online (Sandbox Code Playgroud)\n