【问题标题】:How to use Trycatch to skip errors in data downloading in R如何使用 Trycatch 跳过 R 中数据下载中的错误
【发布时间】:2021-08-06 15:22:30
【问题描述】:

我正在尝试使用 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 时做错了什么?

【问题讨论】:

  • 您还没有描述您的解决方案出了什么问题。
  • 我刚刚编辑了我的问题!

标签: r dataframe error-handling try-catch data-retrieval


【解决方案1】:

回答

您在getstreamflow 之外编写了tryCatch。因此,如果一个站点出现故障,那么getstreamflow 将返回一个错误而不会返回任何其他内容。您应该一次提供 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))

这会返回:

[1]“错误1”“错误2”“错误3”“错误4”“错误5”

多个参数

你表示你有 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

【讨论】:

  • 谢谢。如果我想一次提供一个,我有一个问题,但我的功能取决于两个输入(siteNumberdatatype)。我该怎么做?我正在做下面的代码PrecData = sapply(c(siteNumber,datatype), function(siteNumber,datatype) tryCatch(getweather(siteNumber,datatype), error = function(e) paste0("Error in station ", siteNumber))),但它不起作用
  • 我添加了一些新示例,这次使用Map 和for循环。
猜你喜欢
  • 2017-02-06
  • 2021-06-16
  • 2015-08-20
  • 2019-10-29
  • 2017-02-25
  • 1970-01-01
  • 2022-01-15
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多