Keep R code running despite Errors?

后端 未结 1 670
走了就别回头了
走了就别回头了 2021-01-24 11:15

Using this code function from a previous stackoverflow:R: How to GeoCode a simple address using Data Science Toolbox

require(\"RDSTK\")
library(httr)
library(rjs         


        
相关标签:
1条回答
  • 2021-01-24 11:58

    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.

    0 讨论(0)
提交回复
热议问题