Skip to contents

This function evaluates an expression and returns, if warnings occurred, the warning message in the attribute "warningMessage" of the returned object.

Usage

catchWarning(expr, dbg = TRUE)

Arguments

expr

expression to be evaluated

dbg

if TRUE (the default) the warning text is also printed on the console, otherwise the warning message is suppressed.

Examples

catchWarning(as.numeric("1.23"))
#> [1] 1.23
result <- catchWarning(as.numeric("x"))
#> There was a warning:
#> [1] "NAs introduced by coercion"
result <- catchWarning(as.numeric("x"), dbg = FALSE)
str(result)
#>  num NA
#>  - attr(*, "warningMessage")= chr "NAs introduced by coercion"