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
______________________________________________
R-help@r-project.org mailing list -- To UNSUBSCRIBE and more, see
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.