我可以通过使用一个副词来解决这个问题,这个副词曾与另一个地理编码函数一起使用,该函数在无法提供结果时尝试运行该函数5次。考虑到这一点,这似乎只是一个暂时的错误,而不是一个系统性的问题。
我用的副词是:
safely <- function(fn, ..., max_attempts = 5) {
function(...) {
this_env <- environment()
for(i in seq_len(max_attempts)) {
ok <- tryCatch({
assign("result", fn(...), envir = this_env)
TRUE
},
error = function(e) {
FALSE
}
)
if(ok) {
return(this_env$result)
}
}
msg <- sprintf(
"%s failed after %d tries; returning NULL.",
deparse(match.call()),
max_attempts
)
warning(msg)
NULL
}
}
摘自
Repeating values in loop until error disappears
.