Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add global options to silence messages and warnings #1188

Merged
merged 1 commit into from
May 4, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
# rlang (development version)

* `inform()` and `warn()` messages can now be silenced with the global
options `rlib_message_verbosity` and `rlib_warning_verbosity`.

* `check_dots_empty()`, `check_dots_unused()`, and
`check_dots_unnamed()` have been moved from ellipsis to rlang. The
ellipsis package is deprecated and will eventually be archived.
Expand Down
17 changes: 13 additions & 4 deletions R/cnd-abort.R
Original file line number Diff line number Diff line change
Expand Up @@ -39,15 +39,24 @@
#' * `"full"`: Display a full backtrace tree.
#' * `"none"`: Display nothing.
#'
#' @section Mufflable conditions:
#' @section Muffling and silencing conditions:
#'
#' Signalling a condition with `inform()` or `warn()` causes a message
#' to be displayed in the console. These messages can be muffled with
#' [base::suppressMessages()] or [base::suppressWarnings()].
#'
#' On recent R versions (>= R 3.5.0), interrupts are typically
#' signalled with a `"resume"` restart. This is however not
#' guaranteed.
#' `inform()` and `warn()` messages can also be silenced with the
#' global options `rlib_message_verbosity` and
#' `rlib_warning_verbosity`. These options take the values:
#'
#' - `"default"`: Verbose unless the `.frequency` argument is
#' supplied.
#' - `"verbose"`: Always verbose.
#' - `"quiet"`: Always quiet.
#'
#' When set to quiet, the message is not displayed and the condition
#' is not signalled.
#'
#'
#' @inheritParams cnd
#' @param message The message to display. Character vectors are
Expand Down
22 changes: 16 additions & 6 deletions R/cnd-signal.R
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,7 @@ warn <- function(message = NULL,

.frequency <- arg_match0(.frequency, c("always", "regularly", "once"))

if (needs_signal(.frequency, .frequency_id, warning_freq_env)) {
if (needs_signal(.frequency, .frequency_id, warning_freq_env, "rlib_warning_verbosity")) {
message <- add_message_freq(message, .frequency, "warning")
} else {
return(invisible(NULL))
Expand Down Expand Up @@ -142,7 +142,7 @@ inform <- function(message = NULL,
validate_signal_args(.subclass)

.frequency <- arg_match0(.frequency, c("always", "regularly", "once"))
if (!needs_signal(.frequency, .frequency_id, message_freq_env)) {
if (!needs_signal(.frequency, .frequency_id, message_freq_env, "rlib_message_verbosity")) {
return(invisible(NULL))
}

Expand Down Expand Up @@ -210,13 +210,23 @@ validate_signal_message <- function(msg, class) {
warning_freq_env <- new.env(parent = emptyenv())
message_freq_env <- new.env(parent = emptyenv())

needs_signal <- function(frequency, id, env) {
peek_verbosity <- function(opt) {
out <- peek_option(opt) %||% "default"
out <- arg_match0(out, c("default", "verbose", "quiet"), opt)
out
}

needs_signal <- function(frequency, id, env, opt) {
switch(
peek_verbosity(opt),
verbose = return(TRUE),
quiet = return(FALSE),
default = NULL
)

if (is_string(frequency, "always")) {
return(TRUE)
}
if (is_true(peek_option("rlang:::message_always"))) {
return(TRUE)
}

if (is_null(id)) {
abort("`.frequency_id` should be supplied with `.frequency`.")
Expand Down
17 changes: 13 additions & 4 deletions man/abort.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

20 changes: 16 additions & 4 deletions tests/testthat/test-cnd-signal.R
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ test_that("`inform()` returns invisibly", {
})

test_that("warn() respects frequency", {
local_options(`rlang:::message_always` = FALSE)
local_options(rlib_warning_verbosity = "default")

expect_warning(
warn("foo", .frequency = "always", .frequency_id = "warn_always"),
Expand Down Expand Up @@ -118,7 +118,7 @@ test_that("warn() respects frequency", {
})

test_that("inform() respects frequency", {
local_options(`rlang:::message_always` = FALSE)
local_options(rlib_message_verbosity = "default")

expect_message(
inform("foo", .frequency = "always", .frequency_id = "inform_always"),
Expand Down Expand Up @@ -147,7 +147,10 @@ test_that("inform() respects frequency", {
})

test_that("warn() and inform() use different periodicity environments", {
local_options(`rlang:::message_always` = FALSE)
local_options(
rlib_message_verbosity = "default",
rlib_warning_verbosity = "default"
)

expect_message(
inform("foo", .frequency = "once", .frequency_id = "warn_inform_different_envs"),
Expand All @@ -160,7 +163,7 @@ test_that("warn() and inform() use different periodicity environments", {
})

test_that("periodic messages can be forced", {
local_options(`rlang:::message_always` = TRUE)
local_options(rlib_warning_verbosity = "verbose")
expect_warning(
warn("foo", .frequency = "once", .frequency_id = "warn_forced"),
"foo"
Expand All @@ -171,6 +174,15 @@ test_that("periodic messages can be forced", {
)
})

test_that("messages can be silenced", {
local_options(
rlib_message_verbosity = "quiet",
rlib_warning_verbosity = "quiet"
)
expect_message(inform("foo"), NA)
expect_warning(warn("foo"), NA)
})

test_that("`.frequency_id` is mandatory", {
expect_error(warn("foo", .frequency = "once"), "frequency_id")
})
Expand Down