提问人:Dd Pp 提问时间:8/30/2012 最后编辑:Konrad RudolphDd Pp 更新时间:11/14/2023 访问量:536236
如何使用 tryCatch() 函数?
How to use the tryCatch() function?
问:
我想编写代码来处理从网络下载数据的错误。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
- 当 URL 错误时,输出将是:“Web URL 错误,无法获取”。
- 当 URL 错误时,代码不会停止,而是继续下载,直到 URL 列表的末尾?
答:
R 使用函数来实现 try-catch 块:
语法有点像这样:
result = tryCatch({
expr
}, warning = function(warning_condition) {
warning-handler-code
}, error = function(error_condition) {
error-handler-code
}, finally={
cleanup-code
})
在 tryCatch() 中,有两个可以处理的“条件”:“警告”和“错误”。编写每个代码块时要了解的重要事项是执行状态和范围。@source
评论
error-handler-code
cat("web url is wrong, can't get")
设置代码
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) {
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")
suppressWarnings(readLines(url))
# The return value of `readLines()` is the actual value
# that will be returned in case there is no condition
# (e.g. warning or error).
},
error = function(cond) {
message(paste("URL does not seem to exist:", url))
message("Here's the original error message:")
message(conditionMessage(cond))
# Choose a return value in case of error
NA
},
warning = function(cond) {
message(paste("URL caused a warning:", url))
message("Here's the original warning message:")
message(conditionMessage(cond))
# Choose a return value in case of warning
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")
}
)
}
使用代码
> y <- lapply(urls, readUrl)
This is the 'try' part
Processed URL: http://stat.ethz.ch/R-manual/R-devel/library/base/html/connections.html
Some other message at the end
This is the 'try' part
Processed URL: http://en.wikipedia.org/wiki/Xz
Some other message at the end
This is the 'try' part
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
调查输出
> head(y[[1]])
[1] "<!DOCTYPE html><html><head><title>R: Functions to Manipulate Connections (Files, URLs, ...)</title>"
[2] "<meta http-equiv=\"Content-Type\" content=\"text/html; charset=utf-8\" />"
[3] "<meta name=\"viewport\" content=\"width=device-width, initial-scale=1.0, user-scalable=yes\" />"
[4] "<link rel=\"stylesheet\" href=\"https://cdn.jsdelivr.net/npm/[email protected]/dist/katex.min.css\">"
[5] "<script type=\"text/javascript\">"
[6] "const macros = { \"\\\\R\": \"\\\\textsf{R}\", \"\\\\code\": \"\\\\texttt\"};"
> length(y)
[1] 3
> y[[3]]
[1] NA
补充说明
tryCatch
tryCatch
返回与执行关联的值,除非出现错误或警告。在这种情况下,可以通过提供相应的处理函数来指定特定的返回值(见上文)。这些可以是已经存在的函数,但您也可以在其中定义它们(就像我上面所做的那样)。expr
NA
error
warning
?tryCatch
tryCatch()
选择处理程序函数的特定返回值的含义
正如我们指定的那样,在出现错误时应返回,因此 中的第三个元素是 .NA
y
NA
评论
paste0
paste0()
在基地。在内部,两者都调用了 paste.c。唯一的区别是不传递参数。paste()
paste0()
do_paste
paste0()
sep
paste
readLines(con=url, warn=FALSE)
out <- readLines(con=url, warn=FALSE)
message("Everything worked")
out
paste
paste0
由于我刚刚浪费了两天的时间试图解决 irr 函数的 tryCatch,我想我应该分享我的智慧(以及缺少什么)。仅供参考 - irr 是 FinCal 的一个实际函数,在这种情况下,在大型数据集上出现错误。
将 tryCatch 设置为函数的一部分。例如:
irr2 <- function (x) { out <- tryCatch(irr(x), error = function(e) NULL) return(out) }
要使错误(或警告)起作用,您实际上需要创建一个函数。我最初只是写了错误部分,所有值都返回 null。
error = return(NULL)
请记住创建一个子输出(例如我的“out”)和 .
return(out)
评论
下面是一个简单的例子:
# Do something, or tell me why it failed
my_update_function <- function(x){
tryCatch(
# This is what I want to do...
{
y = x * 2
return(y)
},
# ... but if an error occurs, tell me what happened:
error=function(error_message) {
message("This is my custom message.")
message("And below is the error message from R:")
message(error_message)
return(NA)
}
)
}
如果您还想捕获“警告”,只需添加与该部分相似的内容即可。warning=
error=
评论
expr
Error: unexpected ')' in " )"
Error: unexpected ')' in " )"
tryCatch
具有略微复杂的语法结构。但是,一旦我们理解了构成完整 tryCatch 调用的 4 个部分,如下所示,就很容易记住:
expr:[必需] 要评估的 R 代码
error : [可选] 如果在 expr 中评估代码时发生错误,应该运行什么
warning : [可选] 如果在 expr 中评估代码时出现警告,应运行什么
finally : [可选] 在退出 tryCatch 调用之前应该运行什么,无论 expr 是否成功运行、出现错误或出现警告
tryCatch(
expr = {
# Your code...
# goes here...
# ...
},
error = function(e){
# (Optional)
# Do this if an error is caught...
},
warning = function(w){
# (Optional)
# Do this if a warning is caught...
},
finally = {
# (Optional)
# Do this at the end before quitting the tryCatch structure...
}
)
因此,一个玩具示例,用于计算值的对数可能如下所示:
log_calculator <- function(x){
tryCatch(
expr = {
message(log(x))
message("Successfully executed the log(x) call.")
},
error = function(e){
message('Caught an error!')
print(e)
},
warning = function(w){
message('Caught an warning!')
print(w)
},
finally = {
message('All done, quitting.')
}
)
}
现在,运行三个案例:
有效案例
log_calculator(10)
# 2.30258509299405
# Successfully executed the log(x) call.
# All done, quitting.
“警告”案例
log_calculator(-10)
# Caught an warning!
# <simpleWarning in log(x): NaNs produced>
# All done, quitting.
“错误”案例
log_calculator("log_me")
# Caught an error!
# <simpleError in log(x): non-numeric argument to mathematical function>
# All done, quitting.
我写过一些我经常使用的有用用例。在此处查找更多详细信息:将 tryCatch 用于可靠的 R 脚本
希望这对您有所帮助。
评论
log_calculator
expr
该软件包提供了比 更易于设置的替代功能。从文档中,它们被描述为:purrr
tryCatch
?safely
safely
:wrapped 函数返回一个带有组件和 .如果发生错误,则为对象并具有默认值 ()。否则错误是 。list
result
error
error
error
result
otherwise
NULL
quietly
:包装函数返回一个带有组件、 和 的函数。list
result
output
messages
warnings
possibly
:每当发生错误时,wrapped 函数使用默认值 ()。otherwise
请注意,与 不同,这些函数应包装函数,而不是表达式,并且它们返回修改后的函数。
对于如前所述的 OP 问题,我们可能会直接使用 and wrap 来修改它。tryCatch()
possibly
readLines
url <- c(
"http://stat.ethz.ch/R-manual/R-devel/library/base/html/connections.html",
"http://en.wikipedia.org/wiki/Xz",
"xxx")
library(purrr)
lapply(url, possibly(readLines, otherwise = "web URL is wrong, can't get"))
## with possibly, the error prints as a warning
## and the final value is the `otherwise` string
但也要注意,我们可以创建一个修改后的版本 ,例如可以在我们代码中的多个地方使用。readLines
my_readLines <- possibly(readLines, otherwise = "web URL is wrong, can't get")
我在上面进行了说明,但我们可以很容易地想象出我们想要使用的情况(之后我们可以从每个列表项中提取组件,可能跳过或以其他方式处理具有非空组件的项目,甚至可能根据错误以不同的方式处理它们),或者还单独捕获警告和消息。possibly
safely()
result
error
quietly
评论