recover: Recover the scope of a function that dies.

Description Usage Arguments Details Value Examples

View source: R/recover.R

Description

Allows error recovery with specific lines and functions.

Usage

1
recover(obj, args, return_all = TRUE)

Arguments

obj

A function to test, or a result of previous call to recovery. For functions, please supply as either an unquoted name, or as a character string.

args

A list of optional function arguments to evaluate the function with.

return_all

Whether to return all of the objects in the environment, or to only print the last function called (which failed).

Details

This function exists due to difficulties in dealing with composed functions. A composed function routinely returns an error message without listing the function within that caused the error, leading to uninformative error messages. Rather than try to rewrite everything that already exists (and potentially works), this function allows you to evaluate everything that happened until the function crashed and recover all of the objects in the environment when it crashed. You can thus reconstruct the whole scenario and find the error message faster.

Value

A list with the line caused the function to crash, and optionally the list of all objects in scope when the crash happened. Alternatively return the failing line if **return_all** is set to **FALSE**. Return a success message if the function ran successfully. succesfully (and print a message).

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
28
29
30
 dummy_fun <- function( x = 2,
                        y = "this_crashes",
                        z = 2 )
 {
 # these will run
 x = x + 2
 z = x + 3
 # this will crash due to y being type character
 x = y + z
 return(x)
 }

 recover(obj = dummy_fun, args = list(x = 5))
 # this also works with arguments unspecified (but they must have set defaults)
 recover(obj = dummy_fun)
 # and it will try to be helpful, though it is recommended to specify args where possible

 # finally, recover can also return functions

dummy_fun <- function( x = 2 )
 {
 x = x + 2
 z = x + 3
 lister <- function() ls()
 stop()
 print("Hello, github!")
 return(x)
 }

 recover(obj = dummy_fun)

JSzitas/recovery documentation built on April 3, 2021, 3:16 p.m.