Skip to contents

This is mainly used for code that either (a) generates a bunch of warnings, which makes it hard to find a warning that may have happened 78 warnings ago, or (b) generates a warning and an error, but it's hard to get at the warning because the error causes the code to crash before the warning is signalled.

Usage

catchWarning(expr)

Arguments

expr

An R expression

Examples

out <- catchWarning(log('a'))
out$value
#> <simpleError in log("a"): non-numeric argument to mathematical function>
out <- catchWarning(as.numeric(c('1', 'a')))
out
#> $value
#> [1]  1 NA
#> 
#> $warning
#> <simpleWarning in doTryCatch(return(expr), name, parentenv, handler): NAs introduced by coercion>
#> 
out <- catchWarning(1+1)
out
#> $value
#> [1] 2
#> 
#> $warning
#> NULL
#>