问题
在一个函数中,当你对一个表达式求值不止一次时,你会得到警告restarting interrupted promise evaluation
.例如:
foo <- function() stop("Foo error")
bar <- function(x) {
try(x)
x
}
bar(foo())
yields
Error in foo() : Foo error
Error in foo() : Foo error
In addition: Warning message:
In bar(foo()) : restarting interrupted promise evaluation
如何避免这种警告并妥善处理?
出身背景
尤其是在执行诸如写入数据库之类的操作时,您可能会遇到需要重试几次操作的锁定错误.因此,我创建了一个大约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 <<- F
e
}
)
if (success) break
}
res
}
然而,我收到了restarting interrupted promise evaluation
条信息:
> tryAgain(foo())
[1] "Log error to file: Foo error"
[1] "Log error to file: Foo error"
[1] "Log error to file: Foo error"
<simpleError in foo(): Foo error>
Warning messages:
1: In doTryCatch(return(expr), name, parentenv, handler) :
restarting interrupted promise evaluation
2: In doTryCatch(return(expr), name, parentenv, handler) :
restarting interrupted promise evaluation
理想情况下,我希望完全避免这些信息,而不仅仅是掩盖它们,因为我可能还希望处理来自expr
个用户的真正警告.