insistently()
接受一个函数并修改它,以便在出错时在给定的时间后重试。
参数
- f
-
要修改的函数,通过以下方式之一指定:
-
命名函数,例如
mean
。 -
匿名函数,例如
\(x) x + 1
或function(x) x + 1
。 -
一个公式,例如
~ .x + 1
。仅当您需要向后兼容旧版本的 R 时才推荐。
-
- rate
-
一个rate 对象。默认为抖动指数退避。
- quiet
-
隐藏错误(
TRUE
,默认值),还是在错误发生时显示它们?
副词
该函数称为副词,因为它修饰函数(动词)的效果。如果您想在包中包含创建副词的函数,请务必阅读faq-adverbs-export。
也可以看看
httr::RETRY()
是 HTTP 动词 insistently()
的特例。
其他副词:auto_browse()
, compose()
, negate()
, partial()
, possibly()
, quietly()
, safely()
, slowly()
例子
# For the purpose of this example, we first create a custom rate
# object with a low waiting time between attempts:
rate <- rate_delay(0.1)
# insistently() makes a function repeatedly try to work
risky_runif <- function(lo = 0, hi = 1) {
y <- runif(1, lo, hi)
if(y < 0.9) {
stop(y, " is too small")
}
y
}
# Let's now create an exponential backoff rate with a low waiting
# time between attempts:
rate <- rate_backoff(pause_base = 0.1, pause_min = 0.005, max_times = 4)
# Modify your function to run insistently.
insistent_risky_runif <- insistently(risky_runif, rate, quiet = FALSE)
set.seed(6) # Succeeding seed
insistent_risky_runif()
#> Error: 0.606268297648057 is too small
#> Retrying in 0.19 seconds.
#> Error: 0.264352067606524 is too small
#> Retrying in 0.15 seconds.
#> Error: 0.807483389042318 is too small
#> Retrying in 0.78 seconds.
#> [1] 0.9579337
set.seed(3) # Failing seed
try(insistent_risky_runif())
#> Error: 0.168041526339948 is too small
#> Retrying in 0.16 seconds.
#> Error: 0.384942351374775 is too small
#> Retrying in 0.13 seconds.
#> Error: 0.602100674761459 is too small
#> Retrying in 0.48 seconds.
#> Error: 0.124633444240317 is too small
#> Error in rate_sleep(rate, quiet = quiet) :
#> Request failed after 4 attempts.
# You can also use other types of rate settings, like a delay rate
# that waits for a fixed amount of time. Be aware that a delay rate
# has an infinite amount of attempts by default:
rate <- rate_delay(0.2, max_times = 3)
insistent_risky_runif <- insistently(risky_runif, rate = rate, quiet = FALSE)
try(insistent_risky_runif())
#> Error: 0.294600924244151 is too small
#> Retrying in 0.2 seconds.
#> Error: 0.577609919011593 is too small
#> Retrying in 0.2 seconds.
#> Error: 0.630979274399579 is too small
#> Error in rate_sleep(rate, quiet = quiet) :
#> Request failed after 3 attempts.
# insistently() and possibly() are a useful combination
rate <- rate_backoff(pause_base = 0.1, pause_min = 0.005)
possibly_insistent_risky_runif <- possibly(insistent_risky_runif, otherwise = -99)
set.seed(6)
possibly_insistent_risky_runif()
#> Error: 0.606268297648057 is too small
#> Retrying in 0.2 seconds.
#> [1] 0.937642
set.seed(3)
possibly_insistent_risky_runif()
#> Error: 0.168041526339948 is too small
#> Retrying in 0.2 seconds.
#> Error: 0.807516399072483 is too small
#> Retrying in 0.2 seconds.
#> Error: 0.384942351374775 is too small
#> [1] -99
相关用法
- R purrr imap 将函数应用于向量的每个元素及其索引
- R purrr accumulate 累积向量缩减的中间结果
- R purrr list_transpose 转置列表
- R purrr as_vector 将列表强制转换为向量
- R purrr map_if 有条件地将函数应用于向量的每个元素
- R purrr map2 映射两个输入
- R purrr array-coercion 强制数组列出
- R purrr auto_browse 包装一个函数,以便在出错时自动 browser()
- R purrr pluck 安全地获取或设置嵌套数据结构深处的元素
- R purrr map_depth 在给定深度映射/修改元素
- R purrr list_simplify 将列表简化为原子或 S3 向量
- R purrr rerun 多次重新运行表达式
- R purrr quietly 包装一个函数来捕获副作用
- R purrr list_flatten 压平列表
- R purrr pmap 同时映射多个输入(“并行”)
- R purrr possibly 包装函数以返回值而不是错误
- R purrr head_while 查找全部满足谓词的头/尾。
- R purrr rbernoulli 从伯努利分布生成随机样本
- R purrr rate-helpers 创建延迟率设置
- R purrr keep_at 根据元素的名称/位置保留/丢弃元素
- R purrr keep 根据元素的值保留/丢弃元素
- R purrr transpose 转置列表。
- R purrr flatten 将列表的列表展平为简单的向量
- R purrr every 列表中的每个元素、部分元素还是没有一个元素满足谓词?
- R purrr detect 查找第一个匹配的值或位置
注:本文由纯净天空筛选整理自Hadley Wickham等大神的英文原创作品 Transform a function to wait then retry after an error。非经特殊声明,原始代码版权归原作者所有,本译文未经允许或授权,请勿转载或复制。