persistently: Capture side effects.

Description Usage Arguments Value Examples

View source: R/persistently.R

Description

Imported from https://github.com/ijlyttle and his warrenr package. These functions wrap functions so that instead of generating side effects through printed output, messages, warnings, and errors, they return enhanced output. They are all adverbs because they modify the action of a verb (a function).

Usage

1
persistently(.f, quiet = TRUE, max_attempts = 5, wait_seconds = 0.1)

Arguments

.f

A function, formula, or atomic vector.

quiet

Hide errors (TRUE, the default), or display them as they occur?

max_attempts

Positive integer. persistent functions will try to run this many times before giving up.

wait_seconds

Positive number. Base multiplier for time in seconds to wait between attempts. The time increases exponentially, with a wait time randomly chosen from a uniform distribution between 0 and wait_seconds * 2 ^ (i - 1) seconds, between the ith and i + 1th attempts.

Value

wrapped function uses a default value (otherwise) whenever an error occurs max_attempts times.

Examples

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
# persistently() makes a function repeatedly try to work

risky_runif <- function(lo = 0, hi = 1) {
  y <- stats::runif(1, lo, hi)
  if(y < 0.9) {
    stop(y, " is too small")
  }
  y
}

persistent_risky_runif <- persistently(
  risky_runif, quiet = FALSE, wait_seconds = 0.01)

## Not run: 
  set.seed(1)
  persistent_risky_runif()
  set.seed(3)
  persistent_risky_runif()

## End(Not run)

Dooms31/nojhl documentation built on Oct. 9, 2020, 2:27 a.m.