eval.with.error.trace: Evals the expression such that if an error is encountered a...

Description Usage Arguments Value Examples

Description

This function is mostly useful within a tryCatch clause Adapted from code in tools:::.try_quietly as suggested by Kurt Hornik in the following message https://stat.ethz.ch/pipermail/r-devel/2005-September/034546.html

Usage

1
2
eval.with.error.trace(expr, max.lines = 4, remove.early.calls = 0,
  error.string.fun = default.error.string.fun)

Arguments

expr

the expression to be evaluated

max.lines

as in traceback()

remove.early.calls

an integer specifying a number of calls that won't be shown in the trace.

error.string.fun

a function(e,tb) that takes as arguments an error e and a string vector tb of the stack trace resulting from a call to calls.to.trace() and returns a string with the extended error message

Value

If no error occurs the value of expr, otherwise an error is thrown with an error message that contains the stack trace of the error.

Examples

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
## Not run: 
  # A function that has an error
  f = function(x) {
    1:3[0]
  }
  g = function(x=4) {
    f(x)
  }
  # Usually no traceback is available for errors that are caught with tryCatch
  h = function() {
    tryCatch(
      g(25),
      error = identity
    )
  }
  h()
  
  # The function eval.with.error.trace adds trace information if an error is thrown
  h = function() {
    tryCatch(
      eval.with.error.trace(g(25)),
      error = identity
    )
  }
  h()

## End(Not run)

restorepoint documentation built on April 14, 2017, 4:47 p.m.