Generates a wrapper function that silences the output, messages, and/or warnings of a given function.
.silenceF(f, level = 7L)
f | function to silence |
---|---|
level | a single numeric (integer) that indicates the silencing level, which encodes the set of output to be silenced. It is interpreted like unix permission bit system, where each bit of the binary expression of the silencing level corresponds to a given type of output:
For example, level Negative values are supported and mean "silence everything except the corresponding type",
e.g., |
a function
f <- function(){ cat("stdout message\n") message("stderr message") warning("stderr warning", immediate. = TRUE) } # example of generated wrapper g <- .silenceF(f) g#> function (...) #> { #> utils::capture.output(suppressPackageStartupMessages(suppressMessages(suppressWarnings(res <- withVisible(f(...)))))) #> if (res$visible) #> res$value #> else invisible(res$value) #> } #> <environment: 0x5624226fd6a8>#> #>#> #>#> stdout message#> #>#>#> #>#> stdout message#>#> #>#> Warning: stderr warning#> #>#> stdout message#> Warning: stderr warning#> #>#>#> Warning: stderr warning#> #>#> stdout message#>#> Warning: stderr warning#> #>#> stdout message#> #>#>#> #>#> stdout message#>#> #>#> Warning: stderr warning#> #>#> stdout message#> Warning: stderr warning#> #>#>#> Warning: stderr warning#> #>#> stdout message#>#> Warning: stderr warning# inline functions ifun <- .silenceF(function(){ f(); invisible(1) }) ifun() ifun <- .silenceF(function(){ f(); 1 }) ifun()#> [1] 1ifun <- .silenceF(function(){ f(); 1 }, 2L) ifun()#> stdout message#> Warning: stderr warning#> [1] 1