R/DebugFn.R
DebugFnWE.Rd
Wrap fn, so it will save arguments and environment on failure.
Please see: vignette("DebugFnW", package="wrapr")
.
DebugFnWE(saveDest, fn, ...)
where to write captured state (determined by type): NULL random temp file, character temp file, name globalenv() variable, and function triggers callback.
function to call
arguments for fn
wrapped function that captures state on error.
dump.frames
, DebugFn
, DebugFnW
, DebugFnWE
, DebugPrintFn
, DebugFnE
, DebugPrintFnE
Idea from: https://gist.github.com/nassimhaddad/c9c327d10a91dcf9a3370d30dff8ac3d
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//RtmpZ4IuL6/debug11ef459c295f1.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//RtmpZ4IuL6/debug11ef459c295f1.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: 0x7fba0b3810e8>
#> $ args :List of 1
#> ..$ : num 12
#> $ namedargs: language df(12)
#> $ fn_name : chr "f"
#> $ env :<environment: 0x7fba0b384c78>
# 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