return logical TRUE/FALSE from try, tryCatch, or similar?
error-handling, r, try-catch
Solution
I had to do this on occasion or two. What I did is I used `tryCatch` along these lines. This can be handy if you're doing simulations and sometimes some algorithm doesn't converge and produces a warning, but you want to ignore it, because you don't want your simulation to die while running on 100 nodes on a super computer.
> out <- tryCatch(stop("bla"), error = function(e) e)
> any(class(out) == "error")
[1] TRUE
If process goes through uninterrupted (without error), you get FALSE.
> out <- tryCatch(1:5, error = function(e) e)
> out
[1] 1 2 3 4 5
> any(class(out) == "error")
[1] FALSE
Problem
Is there a function in R that returns "FALSE" if an error is given, otherwise returns "TRUE"? Or something along these lines, or do I need to create my own by evaluating something like `ifelse(class(try(stop())) == "try-error", FALSE, TRUE)`?