R中如何使用Trycatch跳过数据下载错误

How to use Trycatch to skip errors in data downloading in R

我正在尝试使用 R 的 dataRetrieval 包从 USGS 网站下载数据。

为此,我在 R 中生成了一个名为 getstreamflow 的函数,例如当我 运行 时它可以正常工作。

siteNumber <- c("094985005","09498501","09489500","09489499","09498502")
Streamflow = getstreamflow(siteNumber)

函数的输出是数据帧列表

我可以运行在下载数据没有问题的情况下使用该功能,但是对于某些站点,我收到以下错误:

Request failed [404]. Retrying in 1.1 seconds...
Request failed [404]. Retrying in 3.3 seconds...
For: https://waterservices.usgs.gov/nwis/site/?siteOutput=Expanded&format=rdb&site=0946666666

为了避免函数在遇到错误时停止,我尝试使用 tryCatch 如下代码:

Streamflow = tryCatch(
  expr = {
    getstreamflow(siteNumber)
  }, 
  error = function(e) {
  message(paste(siteNumber," there was an error"))
})

我希望功能在遇到错误时跳到下一站。目前,我得到的输出是下面显示的输出,这显然是错误的,因为它说对于所有站都有一个错误:

094985005 there was an error09498501 there was an error09489500 there was an error09489499 there was an error09498502 there was an error09511300 there was an error09498400 there was an error09498500 there was an error09489700 there was an error09500500 there was an error09489082 there was an error09510200 there was an error09489100 there was an error09490500 there was an error09510180 there was an error09494000 there was an error09490000 there was an error09489086 there was an error09489089 there was an error09489200 there was an error09489078 there was an error09510170 there was an error09493500 there was an error09493000 there was an error09498503 there was an error09497500 there was an error09510000 there was an error09509502 there was an error09509500 there was an error09492400 there was an error09492500 there was an error09497980 there was an error09497850 there was an error09492000 there was an error09497800 there was an error09510150 there was an error09499500 there was an error... <truncated>

我在使用 tryCatch 时做错了什么?

回答

您在 getstreamflow 之外写了 tryCatch。因此,如果一个站点出现故障,那么 getstreamflow 将 return 一个错误,仅此而已。您应该一次提供 1 个站点,或者将 tryCatch 放在 getstreamflow.

例子

x <- 1:5
fun <- function(x) {
  for (i in x) if (i == 5) stop("ERROR")
  return(x^2)
}

tryCatch(fun(x), error = function(e) paste0("wrong", x))

这个returns:

[1] "wrong1" "wrong2" "wrong3" "wrong4" "wrong5"

多个参数

您表示要迭代 siteNumberdatatype

使用Map,我们可以定义一个接受两个输入的函数:

Map(function(x, y) tryCatch(fun(x, y), 
                            error = function(e) message(paste(x, " there was an error"))), 
    x = siteNumber, 
    y = datatype)

使用 for 循环,我们可以迭代它们:

Streamflow <- vector(mode = "list", length = length(siteNumber))
for (i in seq_along(siteNumber)) {
  Streamflow[[i]] <- tryCatch(getstreamflow(siteNumber[i], datatype), error = function(e) message(paste(x, " there was an error")))
}

或者按照建议修改getstreamflow.