Selectively suppress messages, warnings, cat and print
Source:R/suppress_matching.R
suppress_matching.RdSelectively suppress messages, warnings, cat and/or print whose output matches given regex pattern.
Usage
suppress_matching(
expr,
pattern = NULL,
suppress_messages = TRUE,
suppress_warnings = TRUE,
suppress_cat_print = TRUE,
...
)Arguments
- expr
An expression that potentially produces some output.
- pattern
Provide a regex-string to only suppress matching output. If
NULL, suppresses all output.- suppress_messages
If output from
message()shall be suppressed.- suppress_warnings
If output from
warning()shall be suppressed.- suppress_cat_print
- ...
Arguments passed on to
base::greplignore.caselogical. if
FALSE, the pattern matching is case sensitive and ifTRUE, case is ignored during matching.perllogical. Should Perl-compatible regexps be used?
fixedlogical. If
TRUE,patternis a string to be matched as is. Overrides all conflicting arguments.useByteslogical. If
TRUEthe matching is done byte-by-byte rather than character-by-character. See ‘Details’.
Details
Utilizes grepl() for matching output with given regex pattern.
Selective suppression of cat() and print() is tricky, as differentiation of single output is not trivial.
This function applies a simple heuristic to at least separate print() output from each other and cat().
Two consecutive cat() outputs can not be differentiated at the moment, potentially leading to unwanted selective suppression behavior.
Examples
noisy_fun <- function() {
message("I am a message")
warning("I am a warning")
cat("I am a cat")
print("I am a print")
return(TRUE)
}
# suppress all
result <- suppress_matching(noisy_fun())
# suppress by matching regex
result <- suppress_matching(noisy_fun(), pattern = ".*am A.*", ignore.case = TRUE)