Skip to content
Prev 378734 / 398502 Next

Error trapping in R

On 27/02/2019 3:55 p.m., Bernard Comcast wrote:
The recommended way is to use tryCatch() around the expression you're 
evaluating.  A simpler, less flexible alternative is try().  The Excel 
version sounds a bit more like try().  You'd use it like this:

   value <- try({ x <- 1
                  y <- someFunction(x)
                  someOtherFunction(y)
                })
   if (inherits(value, "try-error")) {
     cat ("something went wrong.  There's information in value about 
what happened.")
   } else {
     cat ("value is fine, there was no error.")
   }

Duncan Murdoch