我有以下代码:
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)
要显示所有警告,只需运行以下命令:
\noptions(warn=1)\n
Run Code Online (Sandbox Code Playgroud)\n它的工作原理如下:
\nwarn = -1
(或任何负整数),以忽略所有警告warn = 0
(默认),显示 10 个或更少的警告warn = 1
(如上),显示所有警告warn = 2
(或任何大于 2 的正整数)将所有警告转换为错误进一步的解释可以通过运行?options
:
\n\n用于设置警告消息处理的整数值。如果 warn 为负数,则所有警告都将被忽略。如果 warn 为零(默认),则警告将被存储,直到 top\xe2\x80\x93level 函数返回。如果发出了 10 个或更少的警告信号,则会打印它们,否则会显示一条消息,说明发出了多少个警告信号。创建了一个名为 last.warning 的对象,并可以通过函数 warnings 打印该对象。如果 warn 为 1,则在发生警告时打印警告。如果 warn 为 2(或更大,可强制为整数),则所有警告都会变成错误。
\n