View source: R/convertWarnings.R
convertWarnings | R Documentation |
Function is intended to be a message converter for functions that are known to throw warning messages that should generally be treated as errors instead (e.g., non-positive definite matrix warnings, negative variance estimate warnings, etc). Specific warning messages can be caught if specified, otherwise all detected warning messages will be converted to errors for the evaluated R expression.
convertWarnings(expr, warning2error = NULL, muffle = FALSE)
expr |
expression to be evaluated (e.g., |
warning2error |
a character vector of warning messages
that should be converted to errors. Each warning message is
matched using a |
muffle |
logical; muffle any warning message not caught by
|
General goal of this function is to explicitly
indicate warning that are problematic. In many function implementations
only a subset of identified warnings should be treated
as errors, rather than the more nuclear default of treating all warnings
as errors (e.g., see warnings_as_errors
in
runSimulation
, which is primarily included for debugging
purposes early in the simulation design,
as well as option(warn=2)
to convert all warnings to errors
globally).
returns the original result of eval(expr)
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")}
## Not run:
fun <- function(warn1=FALSE, warn2=FALSE, warn3=FALSE, error=FALSE){
if(warn1) warning('Show this warning')
if(warn2) warning('Show a different warning')
if(warn3) warning('Last warning message')
if(error) stop('terminate function call')
return('Returned from fun()')
}
# normal run (no warnings or errors)
out <- fun()
out
# these are all the same
convertWarnings(out <- fun())
out <- convertWarnings(fun())
out <- fun() |> convertWarnings()
# errors treated normally
fun(error=TRUE)
fun(error=TRUE) |> convertWarnings()
# all warnings converted to errors
fun(warn1=TRUE)
fun(warn1=TRUE) |> convertWarnings()
fun(warn2=TRUE) |> convertWarnings()
# muffle all non-caught warnings (not recommended unless you know
# the R expression/function very intimately!)
retmuffle <- fun(warn1=TRUE) |>
convertWarnings('Warning not caught', muffle=TRUE)
retmuffle
# Specific warnings treated as errors (others stay as warnings)
# Here, treat first warning message as error but not the second or third
fun(warn1=TRUE) # warning
ret <- fun(warn1=TRUE) |> convertWarnings("Show this warning") # now error
fun(warn2=TRUE, warn3=TRUE) # warnings
ret23 <- fun(warn2=TRUE, warn3=TRUE) |> # continues, but prints warnings
convertWarnings("Show this warning")
ret23
# Explicitly convert multiple warning messages, allowing others through.
# This is generally the best use of the function's specificity
fun(warn1=TRUE, warn2=TRUE)
ret <- fun(warn1=TRUE) |> # error given either message
convertWarnings(c("Show this warning", "Show a different warning"))
ret <- fun(warn2=TRUE) |>
convertWarnings(c("Show this warning", "Show a different warning"))
# last warning gets through (left as valid warning), but message still raised
ret3 <- fun(warn3=TRUE) |>
convertWarnings(c("Show this warning", "Show a different warning"))
ret3
## End(Not run)
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.