我需要做的是从数百个链接中读取数据,其中一些链接不包含任何数据,因此,这里的代码如下:
urls <-paste0("http://somelink.php?station=",station, "&start=", Year, "01-01&etc")
myData <- lapply(urls, read.table, header = TRUE, sep = '|')
Run Code Online (Sandbox Code Playgroud)
弹出一个错误,说"输入中没有行可用",我尝试使用"尝试",但是出现同样的错误,请帮助,谢谢.
GSe*_*See 22
以下是两种可能的解决方案(未经测试,因为您的示例不可重复):
使用try:
myData <- lapply(urls, function(x) {
tmp <- try(read.table(x, header = TRUE, sep = '|'))
if (!inherits(tmp, 'try-error')) tmp
})
Run Code Online (Sandbox Code Playgroud)
使用tryCatch:
myData <- lapply(urls, function(x) {
tryCatch(read.table(x, header = TRUE, sep = '|'), error=function(e) NULL)
})
Run Code Online (Sandbox Code Playgroud)