首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >R:未能跳过请求失败[500]使用tryCatch()循环中的错误

R:未能跳过请求失败[500]使用tryCatch()循环中的错误
EN

Stack Overflow用户
提问于 2021-12-10 15:23:59
回答 1查看 90关注 0票数 0

我看了几个不同的相关问题:

我希望遍历数据文件中的坐标,并使用USGS nhdplustools包中的discover_nhdplus_id()函数搜索最近的下坡流,并在矩阵中记录该流的ID号(COMID)。如果遇到错误,我希望循环记录'NA‘,然后转到下一个坐标。

示例dataframe (这里有两对坐标无法返回结果: 0,0和-111.2395,36.5396):

代码语言:javascript
复制
# Minimal example dataframe

y <- c(38.27691,
       38.440779,
       37.784306,
       0,
       36.5396,
       38.293296,
       36.5375
)
x <- c(-112.64105,
       -111.643221,
       -111.633194,
       0,
       -111.2395,
       -111.550817,
       -111.2371
)

test_df <- data.frame(x, y)

理想情况下,输出应该如下所示:

代码语言:javascript
复制
[1] 1215201
[1] 4900445
[1] 3277825
[1] NA
[1] NA
[1] 944070011
[1] 3528735

但是,当我实现tryCatch()时,当遇到错误时,循环仍然会崩溃。这些往往是“请求失败500”错误。下面是我的tryCatch()尝试:

代码语言:javascript
复制
library(nhdplusTools)

output <- matrix(NA, nrow=nrow(test_df), ncol=1)
colnames(output) <- c("COMID_raindrop")

for (i in 1:nrow(test_df)){
  
  latitude <- test_df$y[i]
  longitude <- test_df$x[i]
  
  start_point <- st_sfc(st_point(c(longitude, latitude)), crs = 4269)
  
  raindrop_trace <- tryCatch(
      {
        discover_nhdplus_id(start_point)
      },
      error = function(e) {
        NA
      }
    )
  
  output[i] <- raindrop_trace
  print(raindrop_trace)
  
}

[1] 1215201
[1] 4900445
[1] 3277825
No data returned for: https://labs.waterdata.usgs.gov/api/nldi/linked-data/comid/position?coords=POINT%280%200%29FALSE
Request failed [500]. Retrying in 1.2 seconds...
Request failed [500]. Retrying in 1 seconds...
Error in: https://labs.waterdata.usgs.gov/api/nldi/linked-data/comid/NULL/
Error in output[i] <- raindrop_trace : replacement has length zero

在查找有关这方面的信息时,我看到了purrr::可能是推荐的,但当我尝试实现时,返回一个不同的错误:

代码语言:javascript
复制
output <- matrix(NA, nrow=nrow(test_df), ncol=1)
colnames(output) <- c("COMID_raindrop")

for (i in 1:nrow(test_df)){
  
  latitude <- test_df$y[i]
  longitude <- test_df$x[i]
  
  start_point <- st_sfc(st_point(c(longitude, latitude)), crs = 4269)
  
  get_data <- discover_nhdplus_id(start_point)
  raindrop_trace <- purrr::possibly(get_data, otherwise = NA) 
  
  output[i] <- raindrop_trace
  print(raindrop_trace) 
  
}

Error in output[i] <- raindrop_trace : 
  incompatible types (from closure to logical) in subassignment type fix

我认为这个错误可能与赋值'NA‘有关(一个非数字值),但当赋值otherwise = 0时,我会得到相同的错误。

任何帮助都是非常感谢的。

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2021-12-10 16:15:53

您需要添加带有返回函数的NA来将其写入输出。

编辑:

您只需扫描messages并使用withCallingHandlers创建一个基于它们的错误。我添加了有问题的坐标(注释)来测试它。

代码语言:javascript
复制
library(nhdplusTools)
library(sf)
    
y <- c(38.27691,
       38.440779,
       37.784306,
       0,
       0,
       38.293296,
       36.5375,
       36.5396)

x <- c(-112.64105,
       -111.643221,
       -111.633194,
       0,
       0,
       -111.550817,
       -111.2371,
       -111.2395)

test_df <- data.frame(x, y)
test_df <- st_as_sf(x = test_df, coords = c("x", "y"), crs = 4269)

output <- matrix(NA, nrow=nrow(test_df), ncol=1)
colnames(output) <- c("COMID_raindrop")

for (i in seq_along(test_df[[1]])){
  start_point <- test_df[i,]
  
  raindrop_trace <- tryCatch(
    {
      withCallingHandlers(discover_nhdplus_id(point = start_point), message = function(c) if (inherits(c, "message")){stop("")})
      discover_nhdplus_id(point = start_point)
    },
    error = function(e) {
      return(NA)
    }
  )
  output[i] <- raindrop_trace
  print(raindrop_trace)
}

[1] 1215201
[1] 4900445
[1] 3277825
[1] NA
[1] NA
[1] 944070011
[1] 3528735
[1] NA
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/70306764

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档