如何在发生警告时打印警告消息

14

我有以下代码:

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   
2个回答

16

调用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

5
为了显示所有警告,请运行此命令:
options(warn=1)

这是它的工作原理:

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

运行?options可获取进一步解释:

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


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接