使用 tryCatch() 在列表中存储错误和警告

Store errors and warnings with tryCatch() in a list

我编写了一段代码,用于从 API 中提取 JSON 数据并将其转换为数据框。基本代码是:

hospitals_url <- "https://services1.arcgis.com/Hp6G80Pky0om7QvQ/arcgis/rest/services/Hospitals_1/FeatureServer/0/query?where=1%3D1&outFields=*&outSR=4326&f=json"
        hospitals_num <- c(0, 2000, 4000, 6000)
        hosget <- lapply(hospitals_num, function(num) {
          hospitals_url_loop <- paste(hospitals_url, "&resultOffset=", num)
          hospitals_json <- fromJSON(hospitals_url_loop)
          hospitals_df <- as.data.frame(hospitals_json$features$attributes)})
        
        hospitals_df <- do.call(rbind, hosget)}

我正在尝试将其添加到 tryCatch() 函数中,以便我可以更多地参与错误管理和控制。我想要做的是使用 tryCatch() 函数的错误和警告部分来创建包含错误的变量(如果有的话),并将它们放入列表中。我有以下内容:

tryCatch({
        hospitals_url <- "https://services1.arcgis.com/Hp6G80Pky0om7QvQ/arcgis/rest/services/Hospitals_1/FeatureServer/0/query?where=1%3D1&outFields=*&outSR=4326&f=json"
        hospitals_num <- c(0, 2000, 4000, 6000)
        hosget <- lapply(hospitals_num, function(num) {
          hospitals_url_loop <- paste(hospitals_url, "&resultOffset=", num)
          hospitals_json <- fromJSON(hospitals_url_loop)
          hospitals_df <- as.data.frame(hospitals_json$features$attributes)})
        
        hospitals_df <- do.call(rbind, hosget)},
      
      error = function(hosp_e){
              hosp_e <- simpleError("Error: Hospital data has not been collected due to an error. Please ensure that the number loop is correct.")
              print(hosp_e)},
      
      warning = function(hosp_w){
              hosp_w <- simpleWarning("Warning: Hospital data has not been collected. Please ensure that the number loop is correct.")
              print(hosp_w)},
        
      finally = {
              hospitals_log <- (paste("Hospitals Code Has Been Executed on ", Sys.Date()))
              hosp_error <- if (exists("hosp_e") == FALSE) {
                      ("No Error")}
              hosp_warning <- if (exists("hosp_w") == FALSE) {
                      ("No Warning")}
              print(paste(hospitals_log, ", ", hosp_error, ", ", hosp_warning))
              hospitals_output = list(hospitals_log, hosp_error, hosp_warning, paste("URL: ", hospitals_url))
              rm(hospitals_log, hosp_warning, hosp_error, hospitals_num, hosget, hospitals_url)
                })

当我创建一个错误(例如,向 hospitals_url 添加一个额外的“l”)时,我的 simpleerror 打印 (<simpleError: Error: Hospital data has not been collected due to an error. Please ensure that the number loop is correct.>),但没有存储为变量并添加到列表中。我怎样才能更改我的代码来完成此操作?

我看过以下内容 ( and here),但现在更困惑了...:(

这是一个将 tryCatch 放入循环中并保存生成的错误和警告的版本:

  hospitals_url <- "https://services1.arcgis.com/Hp6G80Pky0om7QvQ/arcgis/rest/services/Hospitals_1/FeatureServer/0/query?where=1%3D1&outFields=*&outSR=4326&f=json"
  hospitals_num <- c(0, 2000, 4000, 6000)
  hosp_e <- list()
  hosp_w <- list()
  
  hosget <- lapply(hospitals_num, function(num) {
    tryCatch({
      hospitals_url_loop <- paste(hospitals_url, "&resultOffset=", num)
      hospitals_json <- fromJSON(hospitals_url_loop)
      hospitals_df <- as.data.frame(hospitals_json$features$attributes)},
      error = function(e){
        hosp_e <<- c(hosp_e, list(e))
        print(e)},
      
      warning = function(w){
        hosp_w <<- c(hosp_w, list(w))
        print(w)}     
      ) })
    
  hospitals_df <- do.call(rbind, hosget)
  

我没有使您的 finally 代码适应这种重新排列;我会把它留给你。但最后,hosp_e 将是一个包含所有错误的列表,而 hosp_w 将是一个包含所有警告的列表。