如何在出现警告消息时打印它们

Dd *_* Pp 5 warnings r

我有以下代码:

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)
Run Code Online (Sandbox Code Playgroud)

当我运行它时,我得到:

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  
Run Code Online (Sandbox Code Playgroud)

但我期待:

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  
Run Code Online (Sandbox Code Playgroud)

那么,为什么我得到:

Warning message:    
In file(con, "r") : cannot open file 'xxxxx': No such file or directory   
Run Code Online (Sandbox Code Playgroud)

And*_*rie 10

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)
Run Code Online (Sandbox Code Playgroud)

屏幕输出:

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
Run Code Online (Sandbox Code Playgroud)

或者,您可以使用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)
Run Code Online (Sandbox Code Playgroud)

输出:

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
Run Code Online (Sandbox Code Playgroud)


use*_*745 5

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

\n
options(warn=1)\n
Run Code Online (Sandbox Code Playgroud)\n
\n

它的工作原理如下:

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

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

\n
\n

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

\n
\n