R/DebugFn.R
buildNameCallback.Rd
Build a custom writeback function that writes state into a user named variable.
buildNameCallback(varName)
character where to write captured state
writeback function for use with functions such as DebugFnW
# user function
f <- function(i) { (1:10)[[i]] }
# capture last error in variable called "lastError"
writeBack <- buildNameCallback('lastError')
# wrap function with writeBack
df <- DebugFnW(writeBack,f)
# capture error (Note: tryCatch not needed for user code!)
tryCatch(
df(12),
error = function(e) { print(e) })
#> <simpleError in value[[3L]](cond): wrapr::DebugFnW: wrote error to user function: 'writing to variable: "lastError"' on catching 'Error in (1:10)[[i]]: subscript out of bounds'
#> You can reproduce the error with:
#> 'do.call(p$fn, p$args)' (replace 'p' with actual variable name)>
# examine error
str(lastError)
#> List of 4
#> $ fn :function (i)
#> ..- attr(*, "srcref")= 'srcref' int [1:8] 3 6 3 32 6 32 3 3
#> .. ..- attr(*, "srcfile")=Classes 'srcfilecopy', 'srcfile' <environment: 0x7fba0a331cc0>
#> $ args :List of 1
#> ..$ : num 12
#> $ namedargs: language df(12)
#> $ fn_name : chr "f"
# redo call, perhaps debugging
tryCatch(
do.call(lastError$fn_name, lastError$args),
error = function(e) { print(e) })
#> <subscriptOutOfBoundsError in (1:10)[[i]]: subscript out of bounds>