使用tryCatch在出错时跳过执行而不退出lapply()

时间:2018-09-25 15:51:50

标签: r error-handling try-catch lapply

我正在尝试编写一个清理电子表格的函数。但是,某些电子表格已损坏,无法打开。我希望函数识别出该错误,输出错误消息,并跳过其余函数的执行(因为我使用lapply()来遍历文件),然后继续。我目前的尝试是这样的:

candidate.cleaner <- function(filename){

  #this function cleans candidate data spreadsheets into an R dataframe

  #dependency check
  library(readxl)

  #read in

    cand_df <-  tryCatch(read_xls(filename, col_names = F),
    error = function (e){
            warning(paste(filename, "cannot be opened; corrupted or does not exist"))
    })
  print(filename)

  #rest of function

  cand_df[1,1]

}

test_vec <- c("test.xls", "test2.xls", "test3.xls")
lapply(FUN = candidate.cleaner, X = test_vec)

但是,在给定不存在的tryCatch文件的情况下,它仍然在.xls语句之后执行函数的行,这会停止,因为我正在尝试索引不存在的数据帧不存在。这将退出lapply调用。如何编写tryCatch调用以使其跳过其余函数而不退出lapply

2 个答案:

答案 0 :(得分:1)

结果证明,这可以通过简单的方法try()和附加的帮助功能来完成。

candidate.cleaner <- function(filename){

  #this function cleans candidate data spreadsheets into an R dataframe

  #dependency check
  library(readxl)

  #read in
  cand_df <- try(read_xls(filename, col_names = F))
  if(is.error(cand_df) == T){
  return(list("Corrupted: rescrape", filename))
  } else {
  #storing election name for later matching
  election_name <- cand_df[1,1]
}
}

is.error()取自Hadley Wickham的 Advanced R chapter on debugging。定义为:

is.error <- function(x) inherits(x, "try-error")

答案 1 :(得分:1)

可以在tryCatch()的开头设置一个信号灯,以指示到目前为止一切正常,然后处理该错误并发出信号,指出错误了,最后检查该信号灯并从函数返回适当的值。

lapply(1:5, function(i) {
    value <- tryCatch({
        OK <- TRUE
        if (i == 2)
            stop("stopping...")
        i
    }, error = function(e) {
        warning("oops: ", conditionMessage(e))
        OK <<- FALSE                    # assign in parent environment
    }, finally = {
        ## return NA on error
        OK || return(NA)
    })
    ## proceed
    value * value
})

这允许人们继续使用tryCatch()基础架构,例如将警告转换为错误。 tryCatch()块封装了所有相关代码。

相关问题