try_catch: Try Catch

Description Usage Arguments Details Examples

View source: R/try_catch.R

Description

Friendlier try catch functions

Usage

1
2
3
4
5
6
7
try_catch(expr, .e = NULL, .w = NULL, .f = NULL)

try_catch_df(expr)

map_try_catch(l, fun, .e = NULL, .w = NULL, .f = NULL)

map_try_catch_df(l, fun)

Arguments

expr

for simple try catch, the expression to be evaluated

.e

a one side formula or a function evaluated when an error occurs

.w

a one side formula or a function evaluated when a warning occurs

.f

a one side formula or an expression evaluated before returning or exiting

l

for map_* function, a list of arguments

fun

for map_* function, a function to try with the list l

Details

try_catch handles errors and warnings the way you specify. try_catch_df returns a tibble with the call, the error message if any, the warning message if any, and the value of the evaluated expression.

Examples

1
2
3
4
5
6
## Not run: 
try_catch(log("a"), .e = ~ paste0("There was an error: ", .x))
try_catch(log(1), .f = ~ print("finally"))
try_catch(log(1), .f = function() print("finally"))

## End(Not run)

Example output

[1] "There was an error: Error in log(\"a\"): non-numeric argument to mathematical function\n"
[1] "finally"
[1] 0
[1] "finally"
[1] 0

attempt documentation built on May 4, 2020, 1:05 a.m.