[R] Error trapping in R

Bernard Comcast mcg@rvey@bern@rd @end|ng |rom comc@@t@net
Thu Feb 28 00:56:31 CET 2019


Sent from my iPhone so please excuse the spelling!"

> On Feb 27, 2019, at 4:05 PM, Duncan Murdoch <murdoch.duncan using gmail.com> wrote:
>> On 27/02/2019 3:55 p.m., Bernard Comcast wrote:
>> What is the recommended way to trap errors in R? My main need is to be able to trap an error and then skip a section of code if an error has occurred. In VB for Excel I used the “On Error goto  .....” construct to do this.
> 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

More information about the R-help mailing list