This workflow will either produce a value or an error. I want that, in the event of an error, will perform an arbitrary code (it could print a message to the console, create an objet).
a <- 1
b <- 2
e <- 3
Using the function from How can I use an error as an if statement in R
is_simple_error <- function(x) inherits(x, "simpleError")
This will evaluate to the desired printed message:
if (is_simple_error(a + b) == TRUE) {
stop("Check the inputs")
} else {
print("You can continue with the script")
}
But in the event of an error, the desired output ("Check the inputs") will not be printed to the terminal. Instead, we have an error: "Error: object 'd' not found"):
if (is_simple_error(e + d) == TRUE) {
stop("Check the inputs")
} else {
print("You can continue the script")
c
away from a function name to stick with remarking on other errors.