Wrap fn, so it will save arguments and environment on failure. Please see: vignette("DebugFnW", package="wrapr").

DebugFnWE(saveDest, fn, ...)

Arguments

saveDest

where to write captured state (determined by type): NULL random temp file, character temp file, name globalenv() variable, and function triggers callback.

fn

function to call

...

arguments for fn

Value

wrapped function that captures state on error.

See also

dump.frames, DebugFn, DebugFnW, DebugFnWE, DebugPrintFn, DebugFnE, DebugPrintFnE

Idea from: https://gist.github.com/nassimhaddad/c9c327d10a91dcf9a3370d30dff8ac3d

Examples

saveDest <- paste0(tempfile('debug'),'.RDS') f <- function(i) { (1:10)[[i]] } df <- DebugFnWE(saveDest, f) # correct run df(5)
#> [1] 5
# now re-run # capture error on incorrect run tryCatch( df(12), error = function(e) { print(e) })
#> <simpleError in value[[3L]](cond): wrapr::DebugFnWE: wrote '/var/folders/7f/sdjycp_d08n8wwytsbgwqgsw0000gn/T//RtmpupYvzx/debug13dd07cc0c16f.RDS' on catching 'Error in (1:10)[[i]]: subscript out of bounds' #> You can reproduce the error with: #> 'p <- readRDS('/var/folders/7f/sdjycp_d08n8wwytsbgwqgsw0000gn/T//RtmpupYvzx/debug13dd07cc0c16f.RDS'); do.call(p$fn, p$args, envir= p$env)'>
# examine details situation <- readRDS(saveDest) str(situation)
#> List of 5 #> $ fn :function (i) #> ..- attr(*, "srcref")= 'srcref' int [1:8] 3 6 3 32 6 32 3 3 #> .. ..- attr(*, "srcfile")=Classes 'srcfilecopy', 'srcfile' <environment: 0x7fc757c14598> #> $ args :List of 1 #> ..$ : num 12 #> $ namedargs: language df(12) #> $ fn_name : chr "f" #> $ env :<environment: 0x7fc757c14218>
# fix and re-run situation$args[[1]] <- 6 do.call(situation$fn, situation$args, envir=situation$env)
#> [1] 6
# clean up file.remove(saveDest)
#> [1] TRUE