suppress_warnings: Suppress specific warning messages

View source: R/suppress_warnings.R

suppress_warningsR Documentation

Suppress specific warning messages

Description

Sometimes R throws warning messages that we don't want to see. The base suppressWarnings() function permits one to suppress warnings, but it is tricky to selectively suppress only certain warnings on the basis of a regular expression or another condition. This function allows one to do that.

Usage

suppress_warnings(.expr, .f, ...)

Arguments

.expr

Expression to be evaluated.

.f

String or function. If a string (possibly representing a regular expression), any warning message generated when .expr is evaluated will be suppressed if grepl{} finds that the string matches the warning message.
\indent If a function, the warning message will be passed to the function, and the function must return TRUE or FALSE. See the examples for details.

...

Additional arguments to be passed to rlang::as_function().

Note

Most functions in the Bullock package have camelCase names. This one does not: it would ordinarily have been called suppressWarnings, but that name is taken by a function in base\NBR.

Author(s)

The function was created by Antoine Fabri ("Moody_Mudskipper"): see https://stackoverflow.com/a/55182432/697473.

Examples

suppress_warnings( {sqrt(-1); warning("oops", call. = FALSE)}, startsWith, "o" )
  # Warning message:
  # In sqrt(-1) : NaNs produced
suppress_warnings( {sqrt(-1); warning("oops", call. = FALSE)}, ~nchar(.)>10 )
  # Warning message:
  # oops
suppress_warnings( {sqrt(-1); warning("ooops", call. = FALSE)}, "NaN" )
  # Warning message:
  # oops
suppress_warnings( {sqrt(-1); invisible()}, "NaN" )
  # Nothing is printed.

jbullock35/Bullock documentation built on April 1, 2022, 6:21 p.m.