Deprecated lifecycle

ctxt_stack() can be tricky to use in real code because all intervening frames are returned with the stack, including those at ctxt_stack() own call site. stack_trim() makes it easy to remove layers of intervening calls.

stack_trim(stack, n = 1)

Arguments

stack

An evaluation stack.

n

The number of call frames (not eval frames) to trim off the top of the stack. In other words, the number of layers of intervening frames to trim.

Life cycle

These functions are deprecated and replaced by trace_back().

Examples

# Intervening frames appear on the evaluation stack: identity(identity(ctxt_stack()))
#> [[1]] #> <frame 35> (33) #> expr: identity(ctxt_stack()) #> env: [local 0x3bc7230] #> #> [[2]] #> <frame 34> (33) #> expr: identity(identity(ctxt_stack())) #> env: [local 0x3bc7118] #> #> [[3]] #> <frame 33> (32) #> expr: eval(expr, envir, enclos) #> env: [local 0x3cf8bb0] #> #> [[4]] #> <frame 32> (22) #> expr: eval(expr, envir, enclos) #> env: [local 0x3bc6f58] #> #> [[5]] #> <frame 31> (22) #> expr: withVisible(eval(expr, envir, enclos)) #> env: [local 0x3bc6d28] #> #> [[6]] #> <frame 30> (22) #> expr: withCallingHandlers(withVisible(eval(expr, envir, enclos)), warning = wHandler, <...> #> env: [local 0x3bc5f28] #> #> [[7]] #> <frame 29> (28) #> expr: doTryCatch(return(expr), name, parentenv, handler) #> env: [local 0x3bc9738] #> #> [[8]] #> <frame 28> (27) #> expr: tryCatchOne(expr, names, parentenv, handlers[[1L]]) #> env: [local 0x3bc93f0] #> #> [[9]] #> <frame 27> (26) #> expr: tryCatchList(expr, classes, parentenv, handlers) #> env: [local 0x3bc9070] #> #> [[10]] #> <frame 26> (25) #> expr: tryCatch(expr, error = function(e) { <...> #> env: [local 0x3bc8938] #> #> [[11]] #> <frame 25> (24) #> expr: try(f, silent = TRUE) #> env: [local 0x3bc86d0] #> #> [[12]] #> <frame 24> (22) #> expr: handle(ev <- withCallingHandlers(withVisible(eval(expr, envir, <...> #> env: [local 0x3bc84a0] #> #> [[13]] #> <frame 23> (22) #> expr: timing_fn(handle(ev <- withCallingHandlers(withVisible(eval(expr, <...> #> env: [local 0x3bc83c0] #> #> [[14]] #> <frame 22> (21) #> expr: evaluate_call(expr, parsed$src[[i]], envir = envir, enclos = enclos, <...> #> env: [frame 0x3beaff8] #> #> [[15]] #> <frame 21> (20) #> expr: evaluate::evaluate(x, child_env(env), new_device = TRUE) #> env: [frame 0x49c7910] #> #> [[16]] #> <frame 20> (19) #> expr: highlight_examples(code, topic, env = env) #> env: [frame 0x49d3398] #> #> [[17]] #> <frame 19> (18) #> expr: run_examples(tags$tag_examples[[1]], env = new.env(parent = globalenv()), <...> #> env: [frame 0x4a015f8] #> #> [[18]] #> <frame 18> (17) #> expr: data_reference_topic(topic, pkg, examples = examples, run_dont_run = run_dont_run) #> env: [frame 0x5997148] #> #> [[19]] #> <frame 17> (16) #> expr: .f(.x[[i]], ...) #> env: [frame 0x5bc5e18] #> #> [[20]] #> <frame 16> (15) #> expr: purrr::map(topics, build_reference_topic, pkg = pkg, lazy = lazy, <...> #> env: [frame 0x5945038] #> #> [[21]] #> <frame 15> (14) #> expr: build_reference(pkg, lazy = lazy, examples = examples, run_dont_run = run_dont_run, <...> #> env: [frame 0x5d2f518] #> #> [[22]] #> <frame 14> (13) #> expr: build_site_local(pkg = pkg, examples = examples, run_dont_run = run_dont_run, <...> #> env: [frame 0x3349128] #> #> [[23]] #> <frame 13> (12) #> expr: pkgdown::build_site(...) #> env: [frame 0x333e558] #> #> [[24]] #> <frame 12> (0) #> expr: (function (..., crayon_enabled, crayon_colors, pkgdown_internet) <...> #> env: [frame 0x3085518] #> #> [[25]] #> <frame 11> (0) #> expr: (function (what, args, quote = FALSE, envir = parent.frame()) <...> #> env: [frame 0x3b0f358] #> #> [[26]] #> <frame 10> (0) #> expr: do.call(do.call, c(readRDS("/tmp/Rtmp4B68tY/file4f26784478dc"), <...> #> env: [frame 0x3081470] #> #> [[27]] #> <frame 9> (0) #> expr: saveRDS(do.call(do.call, c(readRDS("/tmp/Rtmp4B68tY/file4f26784478dc"), <...> #> env: [frame 0x30817f0] #> #> [[28]] #> <frame 8> (0) #> expr: withCallingHandlers({ <...> #> env: [frame 0x307c538] #> #> [[29]] #> <frame 7> (6) #> expr: doTryCatch(return(expr), name, parentenv, handler) #> env: [frame 0x3078d98] #> #> [[30]] #> <frame 6> (5) #> expr: tryCatchOne(expr, names, parentenv, handlers[[1L]]) #> env: [frame 0x30790e0] #> #> [[31]] #> <frame 5> (2) #> expr: tryCatchList(expr, names[-nh], parentenv, handlers[-nh]) #> env: [frame 0x3079498] #> #> [[32]] #> <frame 4> (3) #> expr: doTryCatch(return(expr), name, parentenv, handler) #> env: [frame 0x3079a48] #> #> [[33]] #> <frame 3> (2) #> expr: tryCatchOne(tryCatchList(expr, names[-nh], parentenv, handlers[-nh]), <...> #> env: [frame 0x3079d90] #> #> [[34]] #> <frame 2> (1) #> expr: tryCatchList(expr, classes, parentenv, handlers) #> env: [frame 0x307a0d8] #> #> [[35]] #> <frame 1> (0) #> expr: tryCatch(withCallingHandlers({ <...> #> env: [frame 0x307a810] #> #> [[36]] #> <frame 0> [global] #> expr: NULL #> env: [global] #> #> attr(,"class") #> [1] "ctxt_stack" "stack"
# stack_trim() will trim the first n layers of calls: stack_trim(identity(identity(ctxt_stack())))
#> [[1]] #> <frame 33> (32) #> expr: eval(expr, envir, enclos) #> env: [local 0x3cf8bb0] #> #> [[2]] #> <frame 32> (22) #> expr: eval(expr, envir, enclos) #> env: [local 0x1a38fd0] #> #> [[3]] #> <frame 31> (22) #> expr: withVisible(eval(expr, envir, enclos)) #> env: [local 0x1a38d68] #> #> [[4]] #> <frame 30> (22) #> expr: withCallingHandlers(withVisible(eval(expr, envir, enclos)), warning = wHandler, <...> #> env: [local 0x1a38080] #> #> [[5]] #> <frame 29> (28) #> expr: doTryCatch(return(expr), name, parentenv, handler) #> env: [local 0x1a37a98] #> #> [[6]] #> <frame 28> (27) #> expr: tryCatchOne(expr, names, parentenv, handlers[[1L]]) #> env: [local 0x1a3b200] #> #> [[7]] #> <frame 27> (26) #> expr: tryCatchList(expr, classes, parentenv, handlers) #> env: [local 0x2456fc8] #> #> [[8]] #> <frame 26> (25) #> expr: tryCatch(expr, error = function(e) { <...> #> env: [local 0x245a500] #> #> [[9]] #> <frame 25> (24) #> expr: try(f, silent = TRUE) #> env: [local 0x245a260] #> #> [[10]] #> <frame 24> (22) #> expr: handle(ev <- withCallingHandlers(withVisible(eval(expr, envir, <...> #> env: [local 0x245a0a0] #> #> [[11]] #> <frame 23> (22) #> expr: timing_fn(handle(ev <- withCallingHandlers(withVisible(eval(expr, <...> #> env: [local 0x2459fc0] #> #> [[12]] #> <frame 22> (21) #> expr: evaluate_call(expr, parsed$src[[i]], envir = envir, enclos = enclos, <...> #> env: [frame 0x2910608] #> #> [[13]] #> <frame 21> (20) #> expr: evaluate::evaluate(x, child_env(env), new_device = TRUE) #> env: [frame 0x49c7910] #> #> [[14]] #> <frame 20> (19) #> expr: highlight_examples(code, topic, env = env) #> env: [frame 0x49d3398] #> #> [[15]] #> <frame 19> (18) #> expr: run_examples(tags$tag_examples[[1]], env = new.env(parent = globalenv()), <...> #> env: [frame 0x4a015f8] #> #> [[16]] #> <frame 18> (17) #> expr: data_reference_topic(topic, pkg, examples = examples, run_dont_run = run_dont_run) #> env: [frame 0x5997148] #> #> [[17]] #> <frame 17> (16) #> expr: .f(.x[[i]], ...) #> env: [frame 0x5bc5e18] #> #> [[18]] #> <frame 16> (15) #> expr: purrr::map(topics, build_reference_topic, pkg = pkg, lazy = lazy, <...> #> env: [frame 0x5945038] #> #> [[19]] #> <frame 15> (14) #> expr: build_reference(pkg, lazy = lazy, examples = examples, run_dont_run = run_dont_run, <...> #> env: [frame 0x5d2f518] #> #> [[20]] #> <frame 14> (13) #> expr: build_site_local(pkg = pkg, examples = examples, run_dont_run = run_dont_run, <...> #> env: [frame 0x3349128] #> #> [[21]] #> <frame 13> (12) #> expr: pkgdown::build_site(...) #> env: [frame 0x333e558] #> #> [[22]] #> <frame 12> (0) #> expr: (function (..., crayon_enabled, crayon_colors, pkgdown_internet) <...> #> env: [frame 0x3085518] #> #> [[23]] #> <frame 11> (0) #> expr: (function (what, args, quote = FALSE, envir = parent.frame()) <...> #> env: [frame 0x3b0f358] #> #> [[24]] #> <frame 10> (0) #> expr: do.call(do.call, c(readRDS("/tmp/Rtmp4B68tY/file4f26784478dc"), <...> #> env: [frame 0x3081470] #> #> [[25]] #> <frame 9> (0) #> expr: saveRDS(do.call(do.call, c(readRDS("/tmp/Rtmp4B68tY/file4f26784478dc"), <...> #> env: [frame 0x30817f0] #> #> [[26]] #> <frame 8> (0) #> expr: withCallingHandlers({ <...> #> env: [frame 0x307c538] #> #> [[27]] #> <frame 7> (6) #> expr: doTryCatch(return(expr), name, parentenv, handler) #> env: [frame 0x3078d98] #> #> [[28]] #> <frame 6> (5) #> expr: tryCatchOne(expr, names, parentenv, handlers[[1L]]) #> env: [frame 0x30790e0] #> #> [[29]] #> <frame 5> (2) #> expr: tryCatchList(expr, names[-nh], parentenv, handlers[-nh]) #> env: [frame 0x3079498] #> #> [[30]] #> <frame 4> (3) #> expr: doTryCatch(return(expr), name, parentenv, handler) #> env: [frame 0x3079a48] #> #> [[31]] #> <frame 3> (2) #> expr: tryCatchOne(tryCatchList(expr, names[-nh], parentenv, handlers[-nh]), <...> #> env: [frame 0x3079d90] #> #> [[32]] #> <frame 2> (1) #> expr: tryCatchList(expr, classes, parentenv, handlers) #> env: [frame 0x307a0d8] #> #> [[33]] #> <frame 1> (0) #> expr: tryCatch(withCallingHandlers({ <...> #> env: [frame 0x307a810] #> #> [[34]] #> <frame 0> [global] #> expr: NULL #> env: [global] #> #> attr(,"class") #> [1] "ctxt_stack" "stack"
# Note that it also takes care of calls intervening at its own call # site: identity(identity( stack_trim(identity(identity(ctxt_stack()))) ))
#> [[1]] #> <frame 33> (32) #> expr: eval(expr, envir, enclos) #> env: [local 0x3cf8bb0] #> #> [[2]] #> <frame 32> (22) #> expr: eval(expr, envir, enclos) #> env: [local 0x64dde60] #> #> [[3]] #> <frame 31> (22) #> expr: withVisible(eval(expr, envir, enclos)) #> env: [local 0x64de090] #> #> [[4]] #> <frame 30> (22) #> expr: withCallingHandlers(withVisible(eval(expr, envir, enclos)), warning = wHandler, <...> #> env: [local 0x64de640] #> #> [[5]] #> <frame 29> (28) #> expr: doTryCatch(return(expr), name, parentenv, handler) #> env: [local 0x64debb8] #> #> [[6]] #> <frame 28> (27) #> expr: tryCatchOne(expr, names, parentenv, handlers[[1L]]) #> env: [local 0x64def00] #> #> [[7]] #> <frame 27> (26) #> expr: tryCatchList(expr, classes, parentenv, handlers) #> env: [local 0x64df248] #> #> [[8]] #> <frame 26> (25) #> expr: tryCatch(expr, error = function(e) { <...> #> env: [local 0x5ff0a60] #> #> [[9]] #> <frame 25> (24) #> expr: try(f, silent = TRUE) #> env: [local 0x5ff0c90] #> #> [[10]] #> <frame 24> (22) #> expr: handle(ev <- withCallingHandlers(withVisible(eval(expr, envir, <...> #> env: [local 0x5ff0e18] #> #> [[11]] #> <frame 23> (22) #> expr: timing_fn(handle(ev <- withCallingHandlers(withVisible(eval(expr, <...> #> env: [local 0x5ff0ef8] #> #> [[12]] #> <frame 22> (21) #> expr: evaluate_call(expr, parsed$src[[i]], envir = envir, enclos = enclos, <...> #> env: [frame 0x4aab380] #> #> [[13]] #> <frame 21> (20) #> expr: evaluate::evaluate(x, child_env(env), new_device = TRUE) #> env: [frame 0x49c7910] #> #> [[14]] #> <frame 20> (19) #> expr: highlight_examples(code, topic, env = env) #> env: [frame 0x49d3398] #> #> [[15]] #> <frame 19> (18) #> expr: run_examples(tags$tag_examples[[1]], env = new.env(parent = globalenv()), <...> #> env: [frame 0x4a015f8] #> #> [[16]] #> <frame 18> (17) #> expr: data_reference_topic(topic, pkg, examples = examples, run_dont_run = run_dont_run) #> env: [frame 0x5997148] #> #> [[17]] #> <frame 17> (16) #> expr: .f(.x[[i]], ...) #> env: [frame 0x5bc5e18] #> #> [[18]] #> <frame 16> (15) #> expr: purrr::map(topics, build_reference_topic, pkg = pkg, lazy = lazy, <...> #> env: [frame 0x5945038] #> #> [[19]] #> <frame 15> (14) #> expr: build_reference(pkg, lazy = lazy, examples = examples, run_dont_run = run_dont_run, <...> #> env: [frame 0x5d2f518] #> #> [[20]] #> <frame 14> (13) #> expr: build_site_local(pkg = pkg, examples = examples, run_dont_run = run_dont_run, <...> #> env: [frame 0x3349128] #> #> [[21]] #> <frame 13> (12) #> expr: pkgdown::build_site(...) #> env: [frame 0x333e558] #> #> [[22]] #> <frame 12> (0) #> expr: (function (..., crayon_enabled, crayon_colors, pkgdown_internet) <...> #> env: [frame 0x3085518] #> #> [[23]] #> <frame 11> (0) #> expr: (function (what, args, quote = FALSE, envir = parent.frame()) <...> #> env: [frame 0x3b0f358] #> #> [[24]] #> <frame 10> (0) #> expr: do.call(do.call, c(readRDS("/tmp/Rtmp4B68tY/file4f26784478dc"), <...> #> env: [frame 0x3081470] #> #> [[25]] #> <frame 9> (0) #> expr: saveRDS(do.call(do.call, c(readRDS("/tmp/Rtmp4B68tY/file4f26784478dc"), <...> #> env: [frame 0x30817f0] #> #> [[26]] #> <frame 8> (0) #> expr: withCallingHandlers({ <...> #> env: [frame 0x307c538] #> #> [[27]] #> <frame 7> (6) #> expr: doTryCatch(return(expr), name, parentenv, handler) #> env: [frame 0x3078d98] #> #> [[28]] #> <frame 6> (5) #> expr: tryCatchOne(expr, names, parentenv, handlers[[1L]]) #> env: [frame 0x30790e0] #> #> [[29]] #> <frame 5> (2) #> expr: tryCatchList(expr, names[-nh], parentenv, handlers[-nh]) #> env: [frame 0x3079498] #> #> [[30]] #> <frame 4> (3) #> expr: doTryCatch(return(expr), name, parentenv, handler) #> env: [frame 0x3079a48] #> #> [[31]] #> <frame 3> (2) #> expr: tryCatchOne(tryCatchList(expr, names[-nh], parentenv, handlers[-nh]), <...> #> env: [frame 0x3079d90] #> #> [[32]] #> <frame 2> (1) #> expr: tryCatchList(expr, classes, parentenv, handlers) #> env: [frame 0x307a0d8] #> #> [[33]] #> <frame 1> (0) #> expr: tryCatch(withCallingHandlers({ <...> #> env: [frame 0x307a810] #> #> [[34]] #> <frame 0> [global] #> expr: NULL #> env: [global] #> #> attr(,"class") #> [1] "ctxt_stack" "stack"
# It is especially useful when used within a function that needs to # inspect the evaluation stack but should nonetheless be callable # within nested calls without side effects: stack_util <- function() { # n = 2 means that two layers of intervening calls should be # removed: The layer at ctxt_stack()'s call site (including the # stack_trim() call), and the layer at stack_util()'s call. stack <- stack_trim(ctxt_stack(), n = 2) stack } user_fn <- function() { # A user calls your stack utility with intervening frames: identity(identity(stack_util())) } # These intervening frames won't appear in the evaluation stack identity(user_fn())
#> [[1]] #> <frame 35> (33) #> expr: user_fn() #> env: [local 0x5f119e8] #> #> [[2]] #> <frame 34> (33) #> expr: identity(user_fn()) #> env: [local 0x5f11a58] #> #> [[3]] #> <frame 33> (32) #> expr: eval(expr, envir, enclos) #> env: [local 0x3cf8bb0] #> #> [[4]] #> <frame 32> (22) #> expr: eval(expr, envir, enclos) #> env: [local 0x5f11c18] #> #> [[5]] #> <frame 31> (22) #> expr: withVisible(eval(expr, envir, enclos)) #> env: [local 0x5f11e48] #> #> [[6]] #> <frame 30> (22) #> expr: withCallingHandlers(withVisible(eval(expr, envir, enclos)), warning = wHandler, <...> #> env: [local 0x5f123f8] #> #> [[7]] #> <frame 29> (28) #> expr: doTryCatch(return(expr), name, parentenv, handler) #> env: [local 0x5f12970] #> #> [[8]] #> <frame 28> (27) #> expr: tryCatchOne(expr, names, parentenv, handlers[[1L]]) #> env: [local 0x5f0ee88] #> #> [[9]] #> <frame 27> (26) #> expr: tryCatchList(expr, classes, parentenv, handlers) #> env: [local 0x5f0f1d0] #> #> [[10]] #> <frame 26> (25) #> expr: tryCatch(expr, error = function(e) { <...> #> env: [local 0x5f0f898] #> #> [[11]] #> <frame 25> (24) #> expr: try(f, silent = TRUE) #> env: [local 0x5f0fac8] #> #> [[12]] #> <frame 24> (22) #> expr: handle(ev <- withCallingHandlers(withVisible(eval(expr, envir, <...> #> env: [local 0x5f0fc50] #> #> [[13]] #> <frame 23> (22) #> expr: timing_fn(handle(ev <- withCallingHandlers(withVisible(eval(expr, <...> #> env: [local 0x5f0fd30] #> #> [[14]] #> <frame 22> (21) #> expr: evaluate_call(expr, parsed$src[[i]], envir = envir, enclos = enclos, <...> #> env: [frame 0x6079d10] #> #> [[15]] #> <frame 21> (20) #> expr: evaluate::evaluate(x, child_env(env), new_device = TRUE) #> env: [frame 0x49c7910] #> #> [[16]] #> <frame 20> (19) #> expr: highlight_examples(code, topic, env = env) #> env: [frame 0x49d3398] #> #> [[17]] #> <frame 19> (18) #> expr: run_examples(tags$tag_examples[[1]], env = new.env(parent = globalenv()), <...> #> env: [frame 0x4a015f8] #> #> [[18]] #> <frame 18> (17) #> expr: data_reference_topic(topic, pkg, examples = examples, run_dont_run = run_dont_run) #> env: [frame 0x5997148] #> #> [[19]] #> <frame 17> (16) #> expr: .f(.x[[i]], ...) #> env: [frame 0x5bc5e18] #> #> [[20]] #> <frame 16> (15) #> expr: purrr::map(topics, build_reference_topic, pkg = pkg, lazy = lazy, <...> #> env: [frame 0x5945038] #> #> [[21]] #> <frame 15> (14) #> expr: build_reference(pkg, lazy = lazy, examples = examples, run_dont_run = run_dont_run, <...> #> env: [frame 0x5d2f518] #> #> [[22]] #> <frame 14> (13) #> expr: build_site_local(pkg = pkg, examples = examples, run_dont_run = run_dont_run, <...> #> env: [frame 0x3349128] #> #> [[23]] #> <frame 13> (12) #> expr: pkgdown::build_site(...) #> env: [frame 0x333e558] #> #> [[24]] #> <frame 12> (0) #> expr: (function (..., crayon_enabled, crayon_colors, pkgdown_internet) <...> #> env: [frame 0x3085518] #> #> [[25]] #> <frame 11> (0) #> expr: (function (what, args, quote = FALSE, envir = parent.frame()) <...> #> env: [frame 0x3b0f358] #> #> [[26]] #> <frame 10> (0) #> expr: do.call(do.call, c(readRDS("/tmp/Rtmp4B68tY/file4f26784478dc"), <...> #> env: [frame 0x3081470] #> #> [[27]] #> <frame 9> (0) #> expr: saveRDS(do.call(do.call, c(readRDS("/tmp/Rtmp4B68tY/file4f26784478dc"), <...> #> env: [frame 0x30817f0] #> #> [[28]] #> <frame 8> (0) #> expr: withCallingHandlers({ <...> #> env: [frame 0x307c538] #> #> [[29]] #> <frame 7> (6) #> expr: doTryCatch(return(expr), name, parentenv, handler) #> env: [frame 0x3078d98] #> #> [[30]] #> <frame 6> (5) #> expr: tryCatchOne(expr, names, parentenv, handlers[[1L]]) #> env: [frame 0x30790e0] #> #> [[31]] #> <frame 5> (2) #> expr: tryCatchList(expr, names[-nh], parentenv, handlers[-nh]) #> env: [frame 0x3079498] #> #> [[32]] #> <frame 4> (3) #> expr: doTryCatch(return(expr), name, parentenv, handler) #> env: [frame 0x3079a48] #> #> [[33]] #> <frame 3> (2) #> expr: tryCatchOne(tryCatchList(expr, names[-nh], parentenv, handlers[-nh]), <...> #> env: [frame 0x3079d90] #> #> [[34]] #> <frame 2> (1) #> expr: tryCatchList(expr, classes, parentenv, handlers) #> env: [frame 0x307a0d8] #> #> [[35]] #> <frame 1> (0) #> expr: tryCatch(withCallingHandlers({ <...> #> env: [frame 0x307a810] #> #> [[36]] #> <frame 0> [global] #> expr: NULL #> env: [global] #> #> attr(,"class") #> [1] "ctxt_stack" "stack"