10

我有以下代码:

urls <- c(
    "xxxxx",
    "http://stat.ethz.ch/R-manual/R-devel/library/base/html/connections.html",
    "http://en.wikipedia.org/wiki/Xz"        
)
readUrl <- function(url) {
out <- tryCatch(
    readLines(con=url, warn=FALSE),
    error=function(e) {
        message(paste("URL does not seem to exist:", url))
        message(e)
        return(NA)
    },
    finally=message(paste("Processed URL:", url))
)    
return(out)
}
y <- lapply(urls, readUrl)

当我运行它时,我得到:

URL does not seem to exist: xxxxx  
cannot open the connectionProcessed URL: xxxxx    
Processed URL: http://stat.ethz.ch/R-manual/R-devel/library/base/html/connections.html  
Processed URL: http://en.wikipedia.org/wiki/Xz  
Warning message:  
In file(con, "r") : cannot open file 'xxxxx': No such file or directory  

但我期望:

URL does not seem to exist: xxxxx   
cannot open the connectionProcessed URL: xxxxx    
Warning message:    
In file(con, "r") : cannot open file 'xxxxx': No such file or directory  
Processed URL: http://stat.ethz.ch/R-manual/R-devel/library/base/html/connections.html  
Processed URL: http://en.wikipedia.org/wiki/Xz  

那么,为什么我会得到:

Warning message:    
In file(con, "r") : cannot open file 'xxxxx': No such file or directory   
4

2 回答 2

12

调用readLines发出警告。您可以使用 抑制警告suppressWarnings()。试试这个:

readUrl <- function(url) {
  out <- tryCatch(
    suppressWarnings(readLines(con=url, warn=FALSE)),
    error=function(e) {
      message(paste("URL does not seem to exist:", url))
      message(e)
      return(NA)
    },
    finally=message(paste("Processed URL:", url))
  )    
  return(out)
}
y <- lapply(urls, readUrl)

画面输出:

URL does not seem to exist: xxxxx
cannot open the connectionProcessed URL: xxxxx
Processed URL: http://stat.ethz.ch/R-manual/R-devel/library/base/html/connections.html
Processed URL: http://en.wikipedia.org/wiki/Xz

或者,您可以使用options(warn=1)在警告发生时显示警告。试试这个:

readUrl <- function(url) {
  op <- options("warn")
  on.exit(options(op))
  options(warn=1)
  out <- tryCatch(
    readLines(con=url, warn=FALSE),
    error=function(e) {
      message(paste("URL does not seem to exist:", url))
      message(e)
      return(NA)
    },
    finally=message(paste("Processed URL:", url))
  )    
  return(out)
}
y <- lapply(urls, readUrl)

输出:

Warning in file(con, "r") :
  cannot open file 'xxxxx': No such file or directory
URL does not seem to exist: xxxxx
cannot open the connectionProcessed URL: xxxxx
Processed URL: http://stat.ethz.ch/R-manual/R-devel/library/base/html/connections.html
Processed URL: http://en.wikipedia.org/wiki/Xz
于 2012-08-31T08:42:16.390 回答
1

要显示所有警告,只需运行以下命令:

options(warn=1)

以下是它的工作原理:

  • 设置warn = -1(或任何负整数),忽略所有警告
  • 设置warn = 0(默认),显示 10 个或更少的警告
  • 设置warn = 1(如上),显示所有警告
  • 设置warn = 2(或任何大于 2 的正整数)以将所有警告转换为错误

通过运行可获得进一步的解释?options

整数值设置警告消息的处理。如果警告为负,则忽略所有警告。如果 warn 为零(默认值),则存储警告,直到顶级函数返回。如果发出了 10 个或更少的警告,它们将被打印出来,否则会显示一条消息,说明发出了多少个警告。创建了一个名为 last.warning 的对象,并且可以通过函数警告打印。如果 warn 为 1,则在出现警告时打印警告。如果 warn 为 2(或更大,可强制转换为整数),则所有警告都将变为错误。

于 2021-10-06T06:40:36.307 回答