Keep R code running despite Errors?

时光毁灭记忆、已成空白 提交于 2019-12-02 03:29:56
Derek Redfern

Handling errors is best done with a try/catch block. In R, that would look something like this (source):

result = tryCatch({
    # write your intended code here
    Citzy<-c("Leicester, United Kingdom")
    do.call(rbind,lapply(as.character(Citzy),geo.dsk))
}, warning = function(w) {
    # log the warning or take other action here
}, error = function(e) {
    # log the error or take other action here
}, finally = {
    # this will execute no matter what else happened
})

So if you encounter the error, it will enter the error block (and skip the rest of the code in the "try" section) rather than stopping your program. Note that you should always "do something" with the error rather than ignoring it completely; logging a message to the console and/or setting an error flag are good things to do.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!