catching an error and then branching logic catching an error and then branching logic r r

catching an error and then branching logic


t <- try(pJohnson(.18, parms))if("try-error" %in% class(t)) alternativeFunction()


Another option might be to use a tryCatch expression. Here's an example:

 vari <- 1 tryCatch(print("passes"),  error = function(e) print(vari)) # => passes tryCatch(stop("fails"),  error = function(e) print(vari)) # => 1

You can do whatever you want within the error block, so in your case, something like this should work:

tryCatch(pJohnson(.18, parms), error=function(e) alternativeFunction())

This isn't really the intended usage of the error, but it's a little more concise.