【发布时间】: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