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 0xa7b6310] #> #> [[2]] #> <frame 34> (33) #> expr: identity(identity(ctxt_stack())) #> env: [local 0xa7b61f8] #> #> [[3]] #> <frame 33> (32) #> expr: eval(expr, envir, enclos) #> env: [local 0x8b16b60] #> #> [[4]] #> <frame 32> (22) #> expr: eval(expr, envir, enclos) #> env: [local 0xa7b6038] #> #> [[5]] #> <frame 31> (22) #> expr: withVisible(eval(expr, envir, enclos)) #> env: [local 0xa7b5e08] #> #> [[6]] #> <frame 30> (22) #> expr: withCallingHandlers(withVisible(eval(expr, envir, enclos)), warning = wHandler, <...> #> env: [local 0xa7b5778] #> #> [[7]] #> <frame 29> (28) #> expr: doTryCatch(return(expr), name, parentenv, handler) #> env: [local 0xa7b5190] #> #> [[8]] #> <frame 28> (27) #> expr: tryCatchOne(expr, names, parentenv, handlers[[1L]]) #> env: [local 0xa7b4e48] #> #> [[9]] #> <frame 27> (26) #> expr: tryCatchList(expr, classes, parentenv, handlers) #> env: [local 0xa7b4b00] #> #> [[10]] #> <frame 26> (25) #> expr: tryCatch(expr, error = function(e) { <...> #> env: [local 0xa7c3d28] #> #> [[11]] #> <frame 25> (24) #> expr: try(f, silent = TRUE) #> env: [local 0xa7c3af8] #> #> [[12]] #> <frame 24> (22) #> expr: handle(ev <- withCallingHandlers(withVisible(eval(expr, envir, <...> #> env: [local 0xa7c3970] #> #> [[13]] #> <frame 23> (22) #> expr: timing_fn(handle(ev <- withCallingHandlers(withVisible(eval(expr, <...> #> env: [local 0xa7c3890] #> #> [[14]] #> <frame 22> (21) #> expr: evaluate_call(expr, parsed$src[[i]], envir = envir, enclos = enclos, <...> #> env: [frame 0xa815920] #> #> [[15]] #> <frame 21> (20) #> expr: evaluate::evaluate(x, child_env(env), new_device = TRUE) #> env: [frame 0xaa76b30] #> #> [[16]] #> <frame 20> (19) #> expr: highlight_examples(code, topic, env = env) #> env: [frame 0xaa6ee40] #> #> [[17]] #> <frame 19> (18) #> expr: run_examples(tags$tag_examples[[1]], env = new.env(parent = globalenv()), <...> #> env: [frame 0xaa4ffc0] #> #> [[18]] #> <frame 18> (17) #> expr: data_reference_topic(topic, pkg, examples = examples, run_dont_run = run_dont_run) #> env: [frame 0xa7a6e10] #> #> [[19]] #> <frame 17> (16) #> expr: .f(.x[[i]], ...) #> env: [frame 0xa866260] #> #> [[20]] #> <frame 16> (15) #> expr: purrr::map(topics, build_reference_topic, pkg = pkg, lazy = lazy, <...> #> env: [frame 0x4609728] #> #> [[21]] #> <frame 15> (14) #> expr: build_reference(pkg, lazy = lazy, examples = examples, run_dont_run = run_dont_run, <...> #> env: [frame 0x47728c0] #> #> [[22]] #> <frame 14> (13) #> expr: build_site_local(pkg = pkg, examples = examples, run_dont_run = run_dont_run, <...> #> env: [frame 0x47bbb28] #> #> [[23]] #> <frame 13> (12) #> expr: pkgdown::build_site(...) #> env: [frame 0x47b2060] #> #> [[24]] #> <frame 12> (0) #> expr: (function (..., crayon_enabled, crayon_colors, pkgdown_internet) <...> #> env: [frame 0x446b1c8] #> #> [[25]] #> <frame 11> (0) #> expr: (function (what, args, quote = FALSE, envir = parent.frame()) <...> #> env: [frame 0x4f05758] #> #> [[26]] #> <frame 10> (0) #> expr: do.call(do.call, c(readRDS("/tmp/RtmpDApnXm/file4ee732ebd9b8"), <...> #> env: [frame 0x4466470] #> #> [[27]] #> <frame 9> (0) #> expr: saveRDS(do.call(do.call, c(readRDS("/tmp/RtmpDApnXm/file4ee732ebd9b8"), <...> #> env: [frame 0x44667f0] #> #> [[28]] #> <frame 8> (0) #> expr: withCallingHandlers({ <...> #> env: [frame 0x4461538] #> #> [[29]] #> <frame 7> (6) #> expr: doTryCatch(return(expr), name, parentenv, handler) #> env: [frame 0x445dd98] #> #> [[30]] #> <frame 6> (5) #> expr: tryCatchOne(expr, names, parentenv, handlers[[1L]]) #> env: [frame 0x445e0e0] #> #> [[31]] #> <frame 5> (2) #> expr: tryCatchList(expr, names[-nh], parentenv, handlers[-nh]) #> env: [frame 0x445e498] #> #> [[32]] #> <frame 4> (3) #> expr: doTryCatch(return(expr), name, parentenv, handler) #> env: [frame 0x445ea48] #> #> [[33]] #> <frame 3> (2) #> expr: tryCatchOne(tryCatchList(expr, names[-nh], parentenv, handlers[-nh]), <...> #> env: [frame 0x445ed90] #> #> [[34]] #> <frame 2> (1) #> expr: tryCatchList(expr, classes, parentenv, handlers) #> env: [frame 0x445f0d8] #> #> [[35]] #> <frame 1> (0) #> expr: tryCatch(withCallingHandlers({ <...> #> env: [frame 0x445f810] #> #> [[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 0x8b16b60] #> #> [[2]] #> <frame 32> (22) #> expr: eval(expr, envir, enclos) #> env: [local 0x7767eb0] #> #> [[3]] #> <frame 31> (22) #> expr: withVisible(eval(expr, envir, enclos)) #> env: [local 0x7767c80] #> #> [[4]] #> <frame 30> (22) #> expr: withCallingHandlers(withVisible(eval(expr, envir, enclos)), warning = wHandler, <...> #> env: [local 0x776b3e8] #> #> [[5]] #> <frame 29> (28) #> expr: doTryCatch(return(expr), name, parentenv, handler) #> env: [local 0x776ae70] #> #> [[6]] #> <frame 28> (27) #> expr: tryCatchOne(expr, names, parentenv, handlers[[1L]]) #> env: [local 0x776ab28] #> #> [[7]] #> <frame 27> (26) #> expr: tryCatchList(expr, classes, parentenv, handlers) #> env: [local 0x776a690] #> #> [[8]] #> <frame 26> (25) #> expr: tryCatch(expr, error = function(e) { <...> #> env: [local 0x776cd20] #> #> [[9]] #> <frame 25> (24) #> expr: try(f, silent = TRUE) #> env: [local 0x776caf0] #> #> [[10]] #> <frame 24> (22) #> expr: handle(ev <- withCallingHandlers(withVisible(eval(expr, envir, <...> #> env: [local 0x776c968] #> #> [[11]] #> <frame 23> (22) #> expr: timing_fn(handle(ev <- withCallingHandlers(withVisible(eval(expr, <...> #> env: [local 0x776c888] #> #> [[12]] #> <frame 22> (21) #> expr: evaluate_call(expr, parsed$src[[i]], envir = envir, enclos = enclos, <...> #> env: [frame 0x77b3740] #> #> [[13]] #> <frame 21> (20) #> expr: evaluate::evaluate(x, child_env(env), new_device = TRUE) #> env: [frame 0xaa76b30] #> #> [[14]] #> <frame 20> (19) #> expr: highlight_examples(code, topic, env = env) #> env: [frame 0xaa6ee40] #> #> [[15]] #> <frame 19> (18) #> expr: run_examples(tags$tag_examples[[1]], env = new.env(parent = globalenv()), <...> #> env: [frame 0xaa4ffc0] #> #> [[16]] #> <frame 18> (17) #> expr: data_reference_topic(topic, pkg, examples = examples, run_dont_run = run_dont_run) #> env: [frame 0xa7a6e10] #> #> [[17]] #> <frame 17> (16) #> expr: .f(.x[[i]], ...) #> env: [frame 0xa866260] #> #> [[18]] #> <frame 16> (15) #> expr: purrr::map(topics, build_reference_topic, pkg = pkg, lazy = lazy, <...> #> env: [frame 0x4609728] #> #> [[19]] #> <frame 15> (14) #> expr: build_reference(pkg, lazy = lazy, examples = examples, run_dont_run = run_dont_run, <...> #> env: [frame 0x47728c0] #> #> [[20]] #> <frame 14> (13) #> expr: build_site_local(pkg = pkg, examples = examples, run_dont_run = run_dont_run, <...> #> env: [frame 0x47bbb28] #> #> [[21]] #> <frame 13> (12) #> expr: pkgdown::build_site(...) #> env: [frame 0x47b2060] #> #> [[22]] #> <frame 12> (0) #> expr: (function (..., crayon_enabled, crayon_colors, pkgdown_internet) <...> #> env: [frame 0x446b1c8] #> #> [[23]] #> <frame 11> (0) #> expr: (function (what, args, quote = FALSE, envir = parent.frame()) <...> #> env: [frame 0x4f05758] #> #> [[24]] #> <frame 10> (0) #> expr: do.call(do.call, c(readRDS("/tmp/RtmpDApnXm/file4ee732ebd9b8"), <...> #> env: [frame 0x4466470] #> #> [[25]] #> <frame 9> (0) #> expr: saveRDS(do.call(do.call, c(readRDS("/tmp/RtmpDApnXm/file4ee732ebd9b8"), <...> #> env: [frame 0x44667f0] #> #> [[26]] #> <frame 8> (0) #> expr: withCallingHandlers({ <...> #> env: [frame 0x4461538] #> #> [[27]] #> <frame 7> (6) #> expr: doTryCatch(return(expr), name, parentenv, handler) #> env: [frame 0x445dd98] #> #> [[28]] #> <frame 6> (5) #> expr: tryCatchOne(expr, names, parentenv, handlers[[1L]]) #> env: [frame 0x445e0e0] #> #> [[29]] #> <frame 5> (2) #> expr: tryCatchList(expr, names[-nh], parentenv, handlers[-nh]) #> env: [frame 0x445e498] #> #> [[30]] #> <frame 4> (3) #> expr: doTryCatch(return(expr), name, parentenv, handler) #> env: [frame 0x445ea48] #> #> [[31]] #> <frame 3> (2) #> expr: tryCatchOne(tryCatchList(expr, names[-nh], parentenv, handlers[-nh]), <...> #> env: [frame 0x445ed90] #> #> [[32]] #> <frame 2> (1) #> expr: tryCatchList(expr, classes, parentenv, handlers) #> env: [frame 0x445f0d8] #> #> [[33]] #> <frame 1> (0) #> expr: tryCatch(withCallingHandlers({ <...> #> env: [frame 0x445f810] #> #> [[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 0x8b16b60] #> #> [[2]] #> <frame 32> (22) #> expr: eval(expr, envir, enclos) #> env: [local 0x6c581d8] #> #> [[3]] #> <frame 31> (22) #> expr: withVisible(eval(expr, envir, enclos)) #> env: [local 0x6c57d78] #> #> [[4]] #> <frame 30> (22) #> expr: withCallingHandlers(withVisible(eval(expr, envir, enclos)), warning = wHandler, <...> #> env: [local 0x6c57330] #> #> [[5]] #> <frame 29> (28) #> expr: doTryCatch(return(expr), name, parentenv, handler) #> env: [local 0x6c56920] #> #> [[6]] #> <frame 28> (27) #> expr: tryCatchOne(expr, names, parentenv, handlers[[1L]]) #> env: [local 0x6c565a0] #> #> [[7]] #> <frame 27> (26) #> expr: tryCatchList(expr, classes, parentenv, handlers) #> env: [local 0x6c5ca28] #> #> [[8]] #> <frame 26> (25) #> expr: tryCatch(expr, error = function(e) { <...> #> env: [local 0x6c5bc98] #> #> [[9]] #> <frame 25> (24) #> expr: try(f, silent = TRUE) #> env: [local 0x6c61ce8] #> #> [[10]] #> <frame 24> (22) #> expr: handle(ev <- withCallingHandlers(withVisible(eval(expr, envir, <...> #> env: [local 0x6c61230] #> #> [[11]] #> <frame 23> (22) #> expr: timing_fn(handle(ev <- withCallingHandlers(withVisible(eval(expr, <...> #> env: [local 0x6c61150] #> #> [[12]] #> <frame 22> (21) #> expr: evaluate_call(expr, parsed$src[[i]], envir = envir, enclos = enclos, <...> #> env: [frame 0x6c94ea0] #> #> [[13]] #> <frame 21> (20) #> expr: evaluate::evaluate(x, child_env(env), new_device = TRUE) #> env: [frame 0xaa76b30] #> #> [[14]] #> <frame 20> (19) #> expr: highlight_examples(code, topic, env = env) #> env: [frame 0xaa6ee40] #> #> [[15]] #> <frame 19> (18) #> expr: run_examples(tags$tag_examples[[1]], env = new.env(parent = globalenv()), <...> #> env: [frame 0xaa4ffc0] #> #> [[16]] #> <frame 18> (17) #> expr: data_reference_topic(topic, pkg, examples = examples, run_dont_run = run_dont_run) #> env: [frame 0xa7a6e10] #> #> [[17]] #> <frame 17> (16) #> expr: .f(.x[[i]], ...) #> env: [frame 0xa866260] #> #> [[18]] #> <frame 16> (15) #> expr: purrr::map(topics, build_reference_topic, pkg = pkg, lazy = lazy, <...> #> env: [frame 0x4609728] #> #> [[19]] #> <frame 15> (14) #> expr: build_reference(pkg, lazy = lazy, examples = examples, run_dont_run = run_dont_run, <...> #> env: [frame 0x47728c0] #> #> [[20]] #> <frame 14> (13) #> expr: build_site_local(pkg = pkg, examples = examples, run_dont_run = run_dont_run, <...> #> env: [frame 0x47bbb28] #> #> [[21]] #> <frame 13> (12) #> expr: pkgdown::build_site(...) #> env: [frame 0x47b2060] #> #> [[22]] #> <frame 12> (0) #> expr: (function (..., crayon_enabled, crayon_colors, pkgdown_internet) <...> #> env: [frame 0x446b1c8] #> #> [[23]] #> <frame 11> (0) #> expr: (function (what, args, quote = FALSE, envir = parent.frame()) <...> #> env: [frame 0x4f05758] #> #> [[24]] #> <frame 10> (0) #> expr: do.call(do.call, c(readRDS("/tmp/RtmpDApnXm/file4ee732ebd9b8"), <...> #> env: [frame 0x4466470] #> #> [[25]] #> <frame 9> (0) #> expr: saveRDS(do.call(do.call, c(readRDS("/tmp/RtmpDApnXm/file4ee732ebd9b8"), <...> #> env: [frame 0x44667f0] #> #> [[26]] #> <frame 8> (0) #> expr: withCallingHandlers({ <...> #> env: [frame 0x4461538] #> #> [[27]] #> <frame 7> (6) #> expr: doTryCatch(return(expr), name, parentenv, handler) #> env: [frame 0x445dd98] #> #> [[28]] #> <frame 6> (5) #> expr: tryCatchOne(expr, names, parentenv, handlers[[1L]]) #> env: [frame 0x445e0e0] #> #> [[29]] #> <frame 5> (2) #> expr: tryCatchList(expr, names[-nh], parentenv, handlers[-nh]) #> env: [frame 0x445e498] #> #> [[30]] #> <frame 4> (3) #> expr: doTryCatch(return(expr), name, parentenv, handler) #> env: [frame 0x445ea48] #> #> [[31]] #> <frame 3> (2) #> expr: tryCatchOne(tryCatchList(expr, names[-nh], parentenv, handlers[-nh]), <...> #> env: [frame 0x445ed90] #> #> [[32]] #> <frame 2> (1) #> expr: tryCatchList(expr, classes, parentenv, handlers) #> env: [frame 0x445f0d8] #> #> [[33]] #> <frame 1> (0) #> expr: tryCatch(withCallingHandlers({ <...> #> env: [frame 0x445f810] #> #> [[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 0x5f8d168] #> #> [[2]] #> <frame 34> (33) #> expr: identity(user_fn()) #> env: [local 0x5f8d0f8] #> #> [[3]] #> <frame 33> (32) #> expr: eval(expr, envir, enclos) #> env: [local 0x8b16b60] #> #> [[4]] #> <frame 32> (22) #> expr: eval(expr, envir, enclos) #> env: [local 0x5f8cf38] #> #> [[5]] #> <frame 31> (22) #> expr: withVisible(eval(expr, envir, enclos)) #> env: [local 0x5f8cd08] #> #> [[6]] #> <frame 30> (22) #> expr: withCallingHandlers(withVisible(eval(expr, envir, enclos)), warning = wHandler, <...> #> env: [local 0x5f8c678] #> #> [[7]] #> <frame 29> (28) #> expr: doTryCatch(return(expr), name, parentenv, handler) #> env: [local 0x5f8e3a0] #> #> [[8]] #> <frame 28> (27) #> expr: tryCatchOne(expr, names, parentenv, handlers[[1L]]) #> env: [local 0x5f93a60] #> #> [[9]] #> <frame 27> (26) #> expr: tryCatchList(expr, classes, parentenv, handlers) #> env: [local 0x5f933d0] #> #> [[10]] #> <frame 26> (25) #> expr: tryCatch(expr, error = function(e) { <...> #> env: [local 0x5f92640] #> #> [[11]] #> <frame 25> (24) #> expr: try(f, silent = TRUE) #> env: [local 0x5f921e0] #> #> [[12]] #> <frame 24> (22) #> expr: handle(ev <- withCallingHandlers(withVisible(eval(expr, envir, <...> #> env: [local 0x5f97c20] #> #> [[13]] #> <frame 23> (22) #> expr: timing_fn(handle(ev <- withCallingHandlers(withVisible(eval(expr, <...> #> env: [local 0x5f97a60] #> #> [[14]] #> <frame 22> (21) #> expr: evaluate_call(expr, parsed$src[[i]], envir = envir, enclos = enclos, <...> #> env: [frame 0x5fdf3c0] #> #> [[15]] #> <frame 21> (20) #> expr: evaluate::evaluate(x, child_env(env), new_device = TRUE) #> env: [frame 0xaa76b30] #> #> [[16]] #> <frame 20> (19) #> expr: highlight_examples(code, topic, env = env) #> env: [frame 0xaa6ee40] #> #> [[17]] #> <frame 19> (18) #> expr: run_examples(tags$tag_examples[[1]], env = new.env(parent = globalenv()), <...> #> env: [frame 0xaa4ffc0] #> #> [[18]] #> <frame 18> (17) #> expr: data_reference_topic(topic, pkg, examples = examples, run_dont_run = run_dont_run) #> env: [frame 0xa7a6e10] #> #> [[19]] #> <frame 17> (16) #> expr: .f(.x[[i]], ...) #> env: [frame 0xa866260] #> #> [[20]] #> <frame 16> (15) #> expr: purrr::map(topics, build_reference_topic, pkg = pkg, lazy = lazy, <...> #> env: [frame 0x4609728] #> #> [[21]] #> <frame 15> (14) #> expr: build_reference(pkg, lazy = lazy, examples = examples, run_dont_run = run_dont_run, <...> #> env: [frame 0x47728c0] #> #> [[22]] #> <frame 14> (13) #> expr: build_site_local(pkg = pkg, examples = examples, run_dont_run = run_dont_run, <...> #> env: [frame 0x47bbb28] #> #> [[23]] #> <frame 13> (12) #> expr: pkgdown::build_site(...) #> env: [frame 0x47b2060] #> #> [[24]] #> <frame 12> (0) #> expr: (function (..., crayon_enabled, crayon_colors, pkgdown_internet) <...> #> env: [frame 0x446b1c8] #> #> [[25]] #> <frame 11> (0) #> expr: (function (what, args, quote = FALSE, envir = parent.frame()) <...> #> env: [frame 0x4f05758] #> #> [[26]] #> <frame 10> (0) #> expr: do.call(do.call, c(readRDS("/tmp/RtmpDApnXm/file4ee732ebd9b8"), <...> #> env: [frame 0x4466470] #> #> [[27]] #> <frame 9> (0) #> expr: saveRDS(do.call(do.call, c(readRDS("/tmp/RtmpDApnXm/file4ee732ebd9b8"), <...> #> env: [frame 0x44667f0] #> #> [[28]] #> <frame 8> (0) #> expr: withCallingHandlers({ <...> #> env: [frame 0x4461538] #> #> [[29]] #> <frame 7> (6) #> expr: doTryCatch(return(expr), name, parentenv, handler) #> env: [frame 0x445dd98] #> #> [[30]] #> <frame 6> (5) #> expr: tryCatchOne(expr, names, parentenv, handlers[[1L]]) #> env: [frame 0x445e0e0] #> #> [[31]] #> <frame 5> (2) #> expr: tryCatchList(expr, names[-nh], parentenv, handlers[-nh]) #> env: [frame 0x445e498] #> #> [[32]] #> <frame 4> (3) #> expr: doTryCatch(return(expr), name, parentenv, handler) #> env: [frame 0x445ea48] #> #> [[33]] #> <frame 3> (2) #> expr: tryCatchOne(tryCatchList(expr, names[-nh], parentenv, handlers[-nh]), <...> #> env: [frame 0x445ed90] #> #> [[34]] #> <frame 2> (1) #> expr: tryCatchList(expr, classes, parentenv, handlers) #> env: [frame 0x445f0d8] #> #> [[35]] #> <frame 1> (0) #> expr: tryCatch(withCallingHandlers({ <...> #> env: [frame 0x445f810] #> #> [[36]] #> <frame 0> [global] #> expr: NULL #> env: [global] #> #> attr(,"class") #> [1] "ctxt_stack" "stack"