作者热门文章
- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我想编写trycatch
代码来处理从网络下载时出现的错误。
url <- c(
"http://stat.ethz.ch/R-manual/R-devel/library/base/html/connections.html",
"http://en.wikipedia.org/wiki/Xz")
y <- mapply(readLines, con=url)
这两条语句运行成功。下面,我创建了一个不存在的网址:
url <- c("xxxxx", "http://en.wikipedia.org/wiki/Xz")
url[1]
不存在。如何编写一个 trycatch
循环(函数)以便:
最佳答案
那么:欢迎来到 R 世界 ;-)
给你
urls <- c(
"http://stat.ethz.ch/R-manual/R-devel/library/base/html/connections.html",
"http://en.wikipedia.org/wiki/Xz",
"xxxxx"
)
readUrl <- function(url) {
out <- tryCatch(
{
# Just to highlight: if you want to use more than one
# R expression in the "try" part then you'll have to
# use curly brackets.
# 'tryCatch()' will return the last evaluated expression
# in case the "try" part was completed successfully
message("This is the 'try' part")
readLines(con=url, warn=FALSE)
# The return value of `readLines()` is the actual value
# that will be returned in case there is no condition
# (e.g. warning or error).
# You don't need to state the return value via `return()` as code
# in the "try" part is not wrapped inside a function (unlike that
# for the condition handlers for warnings and error below)
},
error=function(cond) {
message(paste("URL does not seem to exist:", url))
message("Here's the original error message:")
message(cond)
# Choose a return value in case of error
return(NA)
},
warning=function(cond) {
message(paste("URL caused a warning:", url))
message("Here's the original warning message:")
message(cond)
# Choose a return value in case of warning
return(NULL)
},
finally={
# NOTE:
# Here goes everything that should be executed at the end,
# regardless of success or error.
# If you want more than one expression to be executed, then you
# need to wrap them in curly brackets ({...}); otherwise you could
# just have written 'finally=<expression>'
message(paste("Processed URL:", url))
message("Some other message at the end")
}
)
return(out)
}
> y <- lapply(urls, readUrl)
Processed URL: http://stat.ethz.ch/R-manual/R-devel/library/base/html/connections.html
Some other message at the end
Processed URL: http://en.wikipedia.org/wiki/Xz
Some other message at the end
URL does not seem to exist: xxxxx
Here's the original error message:
cannot open the connection
Processed URL: xxxxx
Some other message at the end
Warning message:
In file(con, "r") : cannot open file 'xxxxx': No such file or directory
> head(y[[1]])
[1] "<!DOCTYPE html PUBLIC \"-//W3C//DTD HTML 4.01 Transitional//EN\">"
[2] "<html><head><title>R: Functions to Manipulate Connections</title>"
[3] "<meta http-equiv=\"Content-Type\" content=\"text/html; charset=utf-8\">"
[4] "<link rel=\"stylesheet\" type=\"text/css\" href=\"R.css\">"
[5] "</head><body>"
[6] ""
> length(y)
[1] 3
> y[[3]]
[1] NA
tryCatch
tryCatch
返回与执行 expr
相关的值,除非出现错误或警告。在这种情况下,可以通过提供相应的处理函数来指定特定的返回值(请参阅上面的 return(NA)
)(请参阅参数 error
和 warning
> 在 ?tryCatch
中)。这些可以是已经存在的函数,但您也可以在 tryCatch()
中定义它们(就像我上面所做的那样)。
选择处理函数的特定返回值的含义
由于我们已指定在发生错误时应返回 NA
,因此 y
中的第三个元素是 NA
。如果我们选择 NULL
作为返回值,则 y
的长度将是 2
而不是 3
as lapply()
将简单地“忽略”返回值为 NULL
的值。另请注意,如果您未通过 return()
指定显式返回值,则处理函数将返回 NULL
(即,如果错误或警告条件)。
“不需要的”警告消息
由于 warn=FALSE
似乎没有任何效果,抑制警告的另一种方法(在本例中并不是真正感兴趣)是使用
suppressWarnings(readLines(con=url))
而不是
readLines(con=url, warn=FALSE)
多个表达式
请注意,如果将多个表达式括在大括号中(就像我在finally
部分中进行了说明)。
关于r - 如何在R中编写trycatch,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12193779/
我是一名优秀的程序员,十分优秀!