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

前端 未结 2 1634
失恋的感觉
失恋的感觉 2021-01-11 09:42

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(\         


        
2条回答
  •  情书的邮戳
    2021-01-11 10:43

    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)
    })
    

提交回复
热议问题