如何在R中编写trycatch

如何在R中编写trycatch

本文介绍了如何在R中编写trycatch的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想编写 trycatch 代码来处理从网络下载时出现的错误。

I want to write trycatch code to deal with error in downloading from the web.

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] 不存在。如何编写 trycatch 循环(函数),以便:

url[1] does not exist. How does one write a trycatch loop (function) so that:


  1. 何时URL错误,输出将为: Web URL错误,无法获取。

  2. 如果URL错误,代码不会停止,而是继续下载直到URL列表的末尾?


推荐答案

然后:欢迎来到R世界;-)

Well then: welcome to the R world ;-)

在这里走

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 insided 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)
}



应用代码



Applying the code

> 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



调查输出



Investigating the output

> 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 警告 tryCatch 中)。这些可以是已经存在的函数,但是您也可以在 tryCatch()中定义它们(如我上面所做的那样)。

tryCatch returns the value associated to executing expr unless there's an error or a warning. 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). These can be functions that already exist, but you can also define them within tryCatch() (as I did above).

选择处理程序函数的特定返回值的含义

我们已指定 NA 会在出现错误的情况下返回, y 中的第三个元素是 NA 。如果我们选择 NULL 作为返回值,则 y 的长度就是 2 而不是 3 ,因为 lapply()只会忽略返回 NULL 的值。还要注意,如果您没有通过 return()指定显式返回值,则处理函数将返回 NULL (即出现错误或警告情况)。

As we've specified that NA should be returned in case of error, the third element in y is 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. Also note that if you don't specify an explicit return value via return(), the handler functions will return NULL (i.e. in case of an error or a warning condition).

不需要的警告消息

由于 warn = FALSE 似乎没有任何效果,这是抑制警告的另一种方法(在这种情况下,不是真的很有趣)是使用

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

suppressWarnings(readLines(con=url))

而不是

readLines(con=url, warn=FALSE)

多个表达式

请注意,您也可以在实际表达式部分中放置多个表达式(参数 tryCatch的参数 expr ())(如果将它们用大括号括起来(就像我在最后部分中所说明的那样)。

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).

这篇关于如何在R中编写trycatch的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-31 01:50