Evaluates an R expression while suppressing package startup messages and
selected warnings and messages. By default, warnings containing "was built
under R version" or "Loading required namespace" are muffled. Additional
patterns (regular expressions) can be provided via ...
to suppress matching
warnings or messages.
Arguments
- expr
An R expression to be evaluated quietly. This can be a single expression or a block of code wrapped in curly brackets.
- ...
Additional character strings. Each value will be treated as a regular expression pattern to match in warning or message text to muffle. For example,
quietly(expr, "Picked up JAVA_TOOL_OPTIONS", "Scale for [xy] is already present\\.")
.
Examples
# Suppress a package startup message and a version warning
quietly({
warning("This package was built under R version 4.3.1", call. = FALSE)
})
#> Warning: This package was built under R version 4.3.1
#> function (...)
#> capture_output(.f(...))
#> <bytecode: 0x5583198c7e48>
#> <environment: 0x5583198c8120>
# Suppress a custom warning pattern (e.g., JAVA_TOOL_OPTIONS)
quietly({
warning(
"Picked up JAVA_TOOL_OPTIONS: -Djava.util.prefs.userRoot=/tmp/.java1234",
call. = FALSE)
},
"Picked up JAVA_TOOL_OPTIONS")
#> Error in quietly({ warning("Picked up JAVA_TOOL_OPTIONS: -Djava.util.prefs.userRoot=/tmp/.java1234", call. = FALSE)}, "Picked up JAVA_TOOL_OPTIONS"): unused argument ("Picked up JAVA_TOOL_OPTIONS")
# Suppress multiple custom warning patterns
quietly({
warning("Couldn't flush user prefs", call. = FALSE)
warning("java.util.prefs.FileSystemPreferences error", call. = FALSE)
},
"Couldn't flush user prefs", "java.util.prefs.FileSystemPreferences")
#> Error in quietly({ warning("Couldn't flush user prefs", call. = FALSE) warning("java.util.prefs.FileSystemPreferences error", call. = FALSE)}, "Couldn't flush user prefs", "java.util.prefs.FileSystemPreferences"): unused arguments ("Couldn't flush user prefs", "java.util.prefs.FileSystemPreferences")
# Show that a non-matching warning still prints
quietly({
warning("This is a normal warning and should be displayed", call. = FALSE)
},
"Picked up JAVA_TOOL_OPTIONS")
#> Error in quietly({ warning("This is a normal warning and should be displayed", call. = FALSE)}, "Picked up JAVA_TOOL_OPTIONS"): unused argument ("Picked up JAVA_TOOL_OPTIONS")
# Use a code block with multiple lines, some of which trigger warnings
quietly({
# suppressed by default
warning("Loading required namespace: foo", call. = FALSE)
# suppressed by default
warning("was built under R version 4.3.0", call. = FALSE)
# not suppressed, will be shown
warning("Something else", call. = FALSE)
})
#> Warning: Loading required namespace: foo
#> Warning: was built under R version 4.3.0
#> Warning: Something else
#> function (...)
#> capture_output(.f(...))
#> <bytecode: 0x5583198c7e48>
#> <environment: 0x5583197334b0>
if (FALSE) {
# Error if ... is not character
quietly({ warning("test", call. = FALSE) }, 1L)
# Error if expr is not a language object
quietly("not an expression")
}
# Suppress specific messages using regular expression patterns
quietly({
message("Scale for y is already present.")
message("Scale for x is already present.")
message("TTT")
warning("Something else")
},
"Scale for [xy] is already present\\."
)
#> Error in quietly({ message("Scale for y is already present.") message("Scale for x is already present.") message("TTT") warning("Something else")}, "Scale for [xy] is already present\\."): unused argument ("Scale for [xy] is already present\\.")