How to write trycatch in R

前端 未结 5 2320
灰色年华
灰色年华 2020-11-21 23:03

I want to write trycatch code to deal with error in downloading from the web.

url <- c(
    \"http://stat.ethz.ch/R-manual/R-devel/library/ba         


        
5条回答
  •  生来不讨喜
    2020-11-21 23:23

    Since I just lost two days of my life trying to solve for tryCatch for an irr function, I thought I should share my wisdom (and what is missing). FYI - irr is an actual function from FinCal in this case where got errors in a few cases on a large data set.

    1. Set up tryCatch as part of a function. For example:

      irr2 <- function (x) {
        out <- tryCatch(irr(x), error = function(e) NULL)
        return(out)
      }
      
    2. For the error (or warning) to work, you actually need to create a function. I originally for error part just wrote error = return(NULL) and ALL values came back null.

    3. Remember to create a sub-output (like my "out") and to return(out).

提交回复
热议问题