Function to create "Errorized" version of existing R functions.

Share:

Description

This function accepts an existing R function as its first argument and returns a new R function with enhanced error and warning logging capabilities.

Usage

1
errorize(FUN, fileSuffix = NULL, stopOnError = TRUE, onErrorReturn = NULL, ...)

Arguments

FUN

The function to "errorize". If you are making a drop-in replacement, include the namespace (see Details).

fileSuffix

A vector of length one. The suffix to append to the .Rds filename if there is an error or warning. If NULL (the default) the suffix will be the current POSIX time. If NA, no suffix will be added to the file name and subsequent errors or warnings from the same function will be overwritten. Anything else will be coerced to character and appended.

stopOnError

Logical. If the function errors out, do you want to stop the script (the default) or issue a warning and proceed.

onErrorReturn

If stopOnError is FALSE, what value should the function return if it errors out? Default is NULL.

...

Additional arguments to saveRDS.

Details

If you encounter an error or warning, an "errorized" function will save the error or warning, the time at which the error or warning occurred, the function that threw the error, and all the objects entering the "errorized" function in a list.

If you want to create a drop-in replacement of an existing R function (i.e. create a new function of the same name as an existing function in the global environment), you'll need to include the namespace in the call to errorize in the form namespace::function.

Value

A function identical to the function passed to errorize but with enhanced error and warning logging capabilities.

Author(s)

Ian K. Kyle

See Also

saveRDS, readRDS for reading the saved error or warning information back in

Examples

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
## Not run: 
lm <- errorize(stats::lm)
data("cars")
lm(speed ~ dist, data = cars)             # works
lm(speed ~ missing_variable, data = cars) # this errors out and saves the data to a .Rda file

# read the saved data back in (timestamp on filename will change based on when the above is run)
er <- readRDS('./lm_error_1478361734.Rds')

# use do.call to recreate the error
do.call(er$fxn, er$arglst)

## End(Not run)

Want to suggest features or report bugs for rdrr.io? Use the GitHub issue tracker.