似乎在函数中,当您评估一个多次产生错误的表达式时,您会收到警告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)