如何修复“输入中无可用行”的R中的错误?

时间:2022-10-07 22:34:50

What I need to do is to read data from hundreds of links, and among them some of the links contains no data, therefore, as the codes here:

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

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

an error pops up saying "no lines available in input", I've tried using "try", but with same error, please help, thanks.

弹出一个错误,说“输入中没有可用的行”,我尝试使用“尝试”,但是出现同样的错误,请帮助,谢谢。

2 个解决方案

#1


15  

Here are 2 possible solutions (untested because your example is not reproducible):

以下是两种可能的解决方案(未经测试,因为您的示例不可重复):

Using try:

使用try:

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

Using tryCatch:

使用tryCatch:

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

#2


0  

Does this help?

这有帮助吗?

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

If myData still grabs something off the station page, the above code should separate the myData list into two separate groups. good_Ones would be the list you would want to work with. (assuming the above is accurate, of course)

如果myData仍然从站点页面抓取某些内容,则上述代码应将myData列表分成两个单独的组。 good_Ones将是您想要使用的列表。 (当然,假设上述内容是准确的)

#1


15  

Here are 2 possible solutions (untested because your example is not reproducible):

以下是两种可能的解决方案(未经测试,因为您的示例不可重复):

Using try:

使用try:

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

Using tryCatch:

使用tryCatch:

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

#2


0  

Does this help?

这有帮助吗?

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

If myData still grabs something off the station page, the above code should separate the myData list into two separate groups. good_Ones would be the list you would want to work with. (assuming the above is accurate, of course)

如果myData仍然从站点页面抓取某些内容,则上述代码应将myData列表分成两个单独的组。 good_Ones将是您想要使用的列表。 (当然,假设上述内容是准确的)