The Trycatch inside R

Source: Internet
Author: User
Tags error handling
We know that both Java and Python have error handling mechanisms, and Java is a try ... In the form of catch, Python is try ... Except form, this kind of grasping the wrong form is very good, there are similar things in R, that is the Trycatch function, just beginning is not know, and then the R Machine learning actual combat that book code to write a time to know, specific see how to use the put:
(1) Crawl error
TryCatch (Libray (XX), Error=function (e) {print ("error occurred")})
Results:
[1] "error occurred"
The error function is executed when the XX package is wrong, and the result of the error is executed.




(2) Crawl errors and warnings
TryCatch (Libray (xx), warning = function (w) {print ("warning appears")},
Error=function (e) {print ("error occurred")})


(3) Finally will execute, similar to Python and Java
TryCatch (Libray (xx), warning = function (w) {print ("warning appears")},
Error=function (e) {print ("error occurred")},


finally={
Print ("Import Ggplot2 package")
Library (GGPLOT2)
}

)




This function is still very trial, their own code to put on the other machine to carry out, will appear in the absence of the package on the other machine, R software is not like SAS is commercial software, things are unified package, this may also be the ills of R ... In short, this function is more useful, and the rest is left to you to imagine ...

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.