我想写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世界;-)

给你

设置代码

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返回与执行expr相关的值,除非出现错误或警告。在这种情况下,可以通过提供相应的处理函数来指定特定的返回值(参见上面的return(NA))(请参阅?tryCatch中的参数error和warning)。这些函数可以是已经存在的函数,但也可以在tryCatch()中定义它们(如上所述)。

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

正如我们已经指定的,在错误的情况下应该返回NA, y中的第三个元素是NA。如果我们选择NULL作为返回值,y的长度将只是2而不是3,因为lapply()将简单地“忽略”返回值为NULL。还要注意,如果你没有通过return()指定显式的返回值,处理函数将返回NULL(即在错误或警告条件的情况下)。

“不受欢迎”警告信息

由于warn=FALSE似乎没有任何效果,另一种抑制警告的方法(在本例中并不真正感兴趣)是使用

suppressWarnings(readLines(con=url))

而不是

readLines(con=url, warn=FALSE)

多个表达式

注意,如果将多个表达式用大括号括起来(就像我在最后一部分中演示的那样),还可以在“实际表达式部分”(tryCatch()的参数expr)中放置多个表达式。

其他回答

这里有一个简单的例子:

# 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=部分。

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

那么好吧:欢迎来到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返回与执行expr相关的值,除非出现错误或警告。在这种情况下,可以通过提供相应的处理函数来指定特定的返回值(参见上面的return(NA))(请参阅?tryCatch中的参数error和warning)。这些函数可以是已经存在的函数,但也可以在tryCatch()中定义它们(如上所述)。

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

正如我们已经指定的,在错误的情况下应该返回NA, y中的第三个元素是NA。如果我们选择NULL作为返回值,y的长度将只是2而不是3,因为lapply()将简单地“忽略”返回值为NULL。还要注意,如果你没有通过return()指定显式的返回值,处理函数将返回NULL(即在错误或警告条件的情况下)。

“不受欢迎”警告信息

由于warn=FALSE似乎没有任何效果,另一种抑制警告的方法(在本例中并不真正感兴趣)是使用

suppressWarnings(readLines(con=url))

而不是

readLines(con=url, warn=FALSE)

多个表达式

注意,如果将多个表达式用大括号括起来(就像我在最后一部分中演示的那样),还可以在“实际表达式部分”(tryCatch()的参数expr)中放置多个表达式。

由于我刚刚花了两天时间试图为一个irr函数求解tryCatch,我认为我应该分享我的智慧(以及所缺少的东西)。供您参考- irr是来自FinCal的一个实际函数,在这种情况下,在大数据集上的一些情况下会出现错误。

将tryCatch设置为函数的一部分。例如: ir2 <- function (x) { out <- tryCatch(irr(x),错误=函数(e) NULL) 返回() } 要使错误(或警告)生效,实际上需要创建一个函数。我原来的错误部分只是写了错误=返回(NULL)和所有值返回NULL。 记住创建一个子输出(如我的“out”)并返回(out)。

tryCatch的语法结构略复杂。然而,一旦我们理解了组成一个完整的tryCatch调用的4个部分,就很容易记住了:

expr:[必需的]要计算的R个代码

error:[可选]如果在expr中计算代码时发生错误,应该运行什么

警告:[可选]如果在expr中计算代码时发生警告,应该运行什么

[可选]在退出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...
    }
)

因此,一个简单的例子,计算一个值的对数可能是这样的:

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.

我已经写了一些我经常使用的有用的用例。更多详情请访问:https://rsangole.netlify.com/post/try-catch/

希望这对你有帮助。