convertWarnings: Wrapper to convert all/specific warning messages to errors

View source: R/convertWarnings.R

convertWarningsR Documentation

Wrapper to convert all/specific warning messages to errors

Description

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.

Usage

convertWarnings(expr, warning2error = NULL, muffle = FALSE)

Arguments

expr

expression to be evaluated (e.g., ret <- myfun(args) should be wrapped as either convertWarnings(ret <- myfun(args)), ret <- convertWarnings(myfun(args)), or more readably ret <- myfun(args) |> convertWarnings() )

warning2error

a character vector of warning messages that should be converted to errors. Each warning message is matched using a grepl expression, so partial matching is supported (though more specific messages are less likely to throw false positives). If NULL then all observed warning messages will be treated as errors

muffle

logical; muffle any warning message not caught by warning2error specification? Generally not recommended unless you know all of the warning messages returned by a function

Details

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).

Value

returns the original result of eval(expr)

Author(s)

Phil Chalmers rphilip.chalmers@gmail.com

References

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")}

Examples

## 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)


philchalmers/SimDesign documentation built on April 29, 2024, 11:43 p.m.