如何修复该错误的“在输入没有可用的行” R?(How to fix the error in R o

2019-07-04 05:43发布

我需要做的是从几百个链接中读取数据,而其中一些链接不包含任何数据,因此,如下代码:

urls <-paste0("http://somelink.php?station=",station, "&start=", Year, "01-01&etc")
myData <- lapply(urls, read.table, header = TRUE, sep = '|')

错误弹出说“没有输入可用线”,我用“尝试”试过了,但同样的错误,请帮忙,谢谢。

Answer 1:

这里有两个可能的解决方案(未经测试,因为你的例子是不可复制的):

使用try

myData <- lapply(urls, function(x) {
  tmp <- try(read.table(x, header = TRUE, sep = '|'))
  if (!inherits(tmp, 'try-error')) tmp
})

使用tryCatch

myData <- lapply(urls, function(x) {
  tryCatch(read.table(x, header = TRUE, sep = '|'), error=function(e) NULL)
})


Answer 2:

这是否帮助?

  dims <- sapply(myData, dim)[2,]
  bad_Ones <- myData[dims==1]
  good_Ones <- myData[dims>1]

如果myData仍然抓住的东西离站页面,上面的代码应该单独myData列表分为两个独立的团体。 good_Ones将是你想与之合作的名单。 (假设以上是准确的,当然)



文章来源: How to fix the error in R of “no lines available in input”?
标签: r url