Learn R Programming

statnet.common (version 4.10.0)

persistEval: Evaluate an expression, restarting on error

Description

A pair of functions paralleling eval() and evalq() that make multiple attempts at evaluating an expression, retrying on error up to a specified number of attempts, and optionally evaluating another expression before restarting.

Usage

persistEval(
  expr,
  retries = NVL(getOption("eval.retries"), 5),
  beforeRetry,
  envir = parent.frame(),
  enclos = if (is.list(envir) || is.pairlist(envir)) parent.frame() else baseenv(),
  verbose = FALSE
)

persistEvalQ( expr, retries = NVL(getOption("eval.retries"), 5), beforeRetry, envir = parent.frame(), enclos = if (is.list(envir) || is.pairlist(envir)) parent.frame() else baseenv(), verbose = FALSE )

Value

Results of evaluating expr, including side-effects such as variable assignments, if successful in retries retries.

Arguments

expr

an expression to be retried; note the difference between eval() and evalq().

retries

number of retries to make; defaults to "eval.retries" option, or 5.

beforeRetry

if given, an expression that will be evaluated before each retry if the initial attempt fails; it is evaluated in the same environment and with the same quoting semantics as expr, but its errors are not handled.

envir, enclos

see eval().

verbose

Whether to output retries.

Examples

Run this code
x <- 0
persistEvalQ({if((x<-x+1)<3) stop("x < 3") else x},
             beforeRetry = {cat("Will try incrementing...\n")})

x <- 0
e <- quote(if((x<-x+1)<3) stop("x < 3") else x)
persistEval(e,
            beforeRetry = quote(cat("Will try incrementing...\n")))

Run the code above in your browser using DataLab