8.3 Ignoring conditions
A few ways:
try()
suppressWarnings()
suppressMessages()
8.3.1 try()
What it does:
- Displays error
- But continues execution after error
bad_log <- function(x) {
try(log(x))
10
}
bad_log("bad")
#> Error in log(x) : non-numeric argument to mathematical function
#> [1] 10
Better ways to react to/recover from errors:
- Use
tryCatch()
to “catch” the error and perform a different action in the event of an error. - Set a default value inside the call. See below.