R: Evaluate an expression, restarting on error

persistEval {statnet.common} | R Documentation

## 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
    )

### 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.

### Value

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

### Note

If `expr` returns a `"try-error"` object (returned by `try()`), it will be treated as an error. This behavior may change in the future.

### Examples

    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")))

[Package statnet.common version 4.4.1 Index]