persistently: Capture side effects.

Description Usage Arguments Value See Also Examples

View source: R/persistently.R

Description

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. See purrr::map()

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.

See Also

httr::RETRY() is a special case of persistently()

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)

ijlyttle/warrenr documentation built on March 22, 2018, 3:52 p.m.