[英]How to write trycatch in R
I want to write trycatch
code to deal with error in downloading from the web.我想编写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)
These two statements run successfully.这两个语句运行成功。 Below, I create a non-exist web address:下面,我创建了一个不存在的网址:
url <- c("xxxxx", "http://en.wikipedia.org/wiki/Xz")
url[1]
does not exist. url[1]
不存在。 How does one write a trycatch
loop (function) so that:如何编写一个trycatch
循环(函数),以便:
Well then: welcome to the R world ;-)那么:欢迎来到 R 世界 ;-)
Here you go干得好
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
returns the value associated to executing expr
unless there's an error or a warning. tryCatch
返回与执行expr
关联的值,除非出现错误或警告。 In this case, specific return values (see return(NA)
above) can be specified by supplying a respective handler function (see arguments error
and warning
in ?tryCatch
).在这种情况下,可以通过提供相应的处理程序函数(参见?tryCatch
参数error
和warning
)来指定特定的返回值(参见上面的return(NA)
)。 These can be functions that already exist, but you can also define them within tryCatch()
(as I did above).这些可以是已经存在的函数,但您也可以在tryCatch()
定义它们(就像我上面所做的那样)。
The implications of choosing specific return values of the handler functions选择处理程序函数的特定返回值的含义
As we've specified that NA
should be returned in case of error, the third element in y
is NA
.由于我们已经指定在出现错误时应返回NA
,因此y
的第三个元素是NA
。 If we'd have chosen NULL
to be the return value, the length of y
would just have been 2
instead of 3
as lapply()
will simply "ignore" return values that are NULL
.如果我们选择NULL
作为返回值,则y
的长度将仅为2
而不是3
因为lapply()
将简单地“忽略”为NULL
返回值。 Also note that if you don't specify an explicit return value via return()
, the handler functions will return NULL
(ie in case of an error or a warning condition).另请注意,如果您未通过return()
指定显式返回值,则处理程序函数将返回NULL
(即在出现错误或警告条件的情况下)。
"Undesired" warning message “不需要的”警告消息
As warn=FALSE
doesn't seem to have any effect, an alternative way to suppress the warning (which in this case isn't really of interest) is to use由于warn=FALSE
似乎没有任何效果,因此抑制警告(在这种情况下并不是真正感兴趣的)的另一种方法是使用
suppressWarnings(readLines(con=url))
instead of代替
readLines(con=url, warn=FALSE)
Multiple expressions多种表达方式
Note that you can also place multiple expressions in the "actual expressions part" (argument expr
of tryCatch()
) if you wrap them in curly brackets (just like I illustrated in the finally
part).请注意,如果将多个表达式括在大括号中(就像我在finally
部分中说明的那样),您还可以在“实际表达式部分”( tryCatch()
参数expr
tryCatch()
放置多个表达式。
tryCatch
has a slightly complex syntax structure. tryCatch
有一个稍微复杂的语法结构。 However, once we understand the 4 parts which constitute a complete tryCatch call as shown below, it becomes easy to remember:但是,一旦我们理解了构成完整 tryCatch 调用的 4 个部分,如下所示,就很容易记住了:
expr : [ Required ] R code(s) to be evaluated expr : [必需] 要评估的 R 代码
error : [ Optional ] What should run if an error occured while evaluating the codes in expr错误:[可选] 如果在评估 expr 中的代码时发生错误,应该运行什么
warning : [ Optional ] What should run if a warning occured while evaluating the codes in expr警告:[可选] 如果在评估 expr 中的代码时出现警告,应该运行什么
finally : [ Optional ] What should run just before quitting the tryCatch call, irrespective of if expr ran successfully, with an error, or with a warning 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 an warning is caught...
},
finally = {
# (Optional)
# Do this at the end before quitting the tryCatch structure...
}
)
Thus, a toy example, to calculate the log of a value might look like:因此,一个计算值的对数的玩具示例可能如下所示:
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.')
}
)
}
Now, running three cases:现在,运行三种情况:
A valid case一个有效的案例
log_calculator(10)
# 2.30258509299405
# Successfully executed the log(x) call.
# All done, quitting.
A "warning" case一个“警告”案例
log_calculator(-10)
# Caught an warning!
# <simpleWarning in log(x): NaNs produced>
# All done, quitting.
An "error" case一个“错误”案例
log_calculator("log_me")
# Caught an error!
# <simpleError in log(x): non-numeric argument to mathematical function>
# All done, quitting.
I've written about some useful use-cases which I use regularly.我写了一些我经常使用的有用用例。 Find more details here: https://rsangole.netlify.com/post/try-catch/在此处查找更多详细信息: https : //rsangole.netlify.com/post/try-catch/
Hope this is helpful.希望这是有帮助的。
R uses functions for implementing try-catch block: R 使用函数来实现 try-catch 块:
The syntax somewhat looks like this:语法有点像这样:
result = tryCatch({
expr
}, warning = function(warning_condition) {
warning-handler-code
}, error = function(error_condition) {
error-handler-code
}, finally={
cleanup-code
})
In tryCatch() there are two 'conditions' that can be handled: 'warnings' and 'errors'.在 tryCatch() 中有两个可以处理的“条件”:“警告”和“错误”。 The important thing to understand when writing each block of code is the state of execution and the scope.编写每个代码块时要了解的重要一点是执行状态和范围。 @source @来源
Here goes a straightforward example :这是一个简单的例子:
# 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)
}
)
}
If you also want to capture a "warning", just add warning=
similar to the error=
part.如果您还想捕获“警告”,只需添加类似于error=
部分的warning=
。
Since I just lost two days of my life trying to solve for tryCatch for an irr function, I thought I should share my wisdom (and what is missing).由于我刚刚花了两天时间试图解决 irr 函数的 tryCatch,我想我应该分享我的智慧(以及缺少的东西)。 FYI - irr is an actual function from FinCal in this case where got errors in a few cases on a large data set.仅供参考 - irr 是 FinCal 的一个实际函数,在这种情况下,在少数情况下在大型数据集上出现错误。
Set up tryCatch as part of a function.将 tryCatch 设置为函数的一部分。 For example:例如:
irr2 <- function (x) { out <- tryCatch(irr(x), error = function(e) NULL) return(out) }
For the error (or warning) to work, you actually need to create a function.为了使错误(或警告)起作用,您实际上需要创建一个函数。 I originally for error part just wrote error = return(NULL)
and ALL values came back null.我最初为错误部分只写了error = return(NULL)
并且所有值都返回空值。
Remember to create a sub-output (like my "out") and to return(out)
.请记住创建一个子输出(如我的“输出”)并return(out)
。
Thanks for the hints, helped a lot, especially the error-handling-part with 感谢您的提示,对您有所帮助,尤其是错误处理部分
error = function(e){ return(NA) } 错误=函数(e){return(NA)}
This was exactly what i needed. 这正是我所需要的。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.