Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to fix the error in R of "no lines available in input"?

Tags:

url

r

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.

like image 629
Rosa Avatar asked Nov 28 '12 19:11

Rosa


1 Answers

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

Using try:

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

Using tryCatch:

myData <- lapply(urls, function(x) {
  tryCatch(read.table(x, header = TRUE, sep = '|'), error=function(e) NULL)
})
like image 62
GSee Avatar answered Nov 05 '22 22:11

GSee