[R] Error trapping in R
Rui Barradas
ru|pb@rr@d@@ @end|ng |rom @@po@pt
Wed Feb 27 22:13:40 CET 2019
Hello,
You can trap errors with ?try or ?tryCatch.
Example:
result <- vector(mode = "list", length = 5)
for(i in 1:5){
result[[i]] <- tryCatch(if(i == 3) stop("This is an error") else 2*i + 1,
error = function(e) e)
}
result
for(i in seq_along(result)) {
err <- inherits(result[[i]], "error")
print(err)
}
Hope this helps,
Rui Barradas
Às 20:55 de 27/02/2019, Bernard Comcast escreveu:
> 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.
>
> Bernard
> Sent from my iPhone so please excuse the spelling!"
> ______________________________________________
> R-help using 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.
>
More information about the R-help
mailing list