相关疑难解决方法(0)

R避免"重新启动中断的承诺评估"警告

问题

似乎在函数中,当您评估一个多次产生错误的表达式时,您会收到警告restarting interrupted promise evaluation.例如:

foo <- function() stop("Foo error")
bar <- function(x) {
    try(x)
    x
}
bar(foo())
Run Code Online (Sandbox Code Playgroud)

产量

Error in foo() : Foo error
Error in foo() : Foo error
In addition: Warning message:
In bar(foo()) : restarting interrupted promise evaluation
Run Code Online (Sandbox Code Playgroud)

如何避免此警告并妥善处理?

背景

特别是对于写入数据库等操作,您可能会遇到锁定错误,需要您重试几次操作.因此,我正在创建一个包装器tryCatch,重新计算表达式n直到成功为止:

tryAgain <- function(expr, n = 3) {
    success <- T
    for (i in 1:n) {
        res <- tryCatch(expr,
            error = function(e) {
                print(sprintf("Log error to file: %s", conditionMessage(e)))
                success …
Run Code Online (Sandbox Code Playgroud)

error-handling r try-catch

30
推荐指数
1
解决办法
3万
查看次数

标签 统计

error-handling ×1

r ×1

try-catch ×1