View source: R/manageMessages.R
manageMessages | R Documentation |
Function provides more nuanced management of known message outputs
messages that appear in function calls outside the front-end users control
(e.g., functions written in third-party packages). Specifically,
this function provides a less nuclear approach than
quiet
and friends, which suppresses all cat
and
message
s raised, and instead allows for specific messages to be
raised either to warnings or, even more extremely, to errors. Note that for
messages that are not suppressed the order with which the output and message
calls appear in the original function is not retained.
manageMessages(
expr,
allow = NULL,
message2warning = NULL,
message2error = NULL,
...
)
expr |
expression to be evaluated (e.g., ret <- |
allow |
(optional) a |
message2warning |
(optional) Input can be a |
message2error |
(optional) Input can be a |
... |
additional arguments passed to |
returns the original result of eval(expr)
, with warning
messages either left the same, increased to errors, or suppressed (depending
on the input specifications)
Phil Chalmers rphilip.chalmers@gmail.com
Chalmers, R. P., & Adkins, M. C. (2020). Writing Effective and Reliable Monte Carlo Simulations
with the SimDesign Package. The Quantitative Methods for Psychology, 16
(4), 248-280.
\Sexpr[results=rd]{tools:::Rd_expr_doi("10.20982/tqmp.16.4.p248")}
manageWarnings
, quiet
## Not run:
myfun <- function(x, warn=FALSE){
message('This function is rather chatty')
cat("It even prints in different output forms!\n")
message('And even at different ')
cat(" many times!\n")
cat("Too many messages can be annoying \n")
if(warn)
warning('It may even throw warnings ')
x
}
out <- myfun(1)
out
# tell the function to shhhh
out <- quiet(myfun(1))
out
# same default behaviour as quiet(), but potential for nuance
out2 <- manageMessages(myfun(1))
identical(out, out2)
# allow some messages to still get printed
out2 <- manageMessages(myfun(1), allow = "many times!")
out2 <- manageMessages(myfun(1), allow = "This function is rather chatty")
# note: . matches single character (regex)
out2 <- manageMessages(myfun(1), allow = c("many times.",
"This function is rather chatty"))
# convert specific message to warning
out3 <- manageMessages(myfun(1), message2warning = "many times!")
identical(out, out3)
# other warnings also get through
out3 <- manageMessages(myfun(1, warn=TRUE), message2warning = "times!")
identical(out, out3)
# convert message to error
manageMessages(myfun(1), message2error = "m... times!")
# multiple message intensity changes
manageMessages(myfun(1),
message2warning = "It even prints in different output forms",
message2error = "many times!")
manageMessages(myfun(1),
allow = c("This function is rather chatty",
"Too many messages can be annoying"),
message2warning = "It even prints in different output forms",
message2error = "many times!")
## End(Not run)
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.