[Deprecated]

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 26> (24) #> expr: identity(ctxt_stack()) #> env: [local 0x55ba49643a90] #> #> [[2]] #> <frame 25> (24) #> expr: identity(identity(ctxt_stack())) #> env: [local 0x55ba49643978] #> #> [[3]] #> <frame 24> (23) #> expr: eval(expr, envir, enclos) #> env: [local 0x55ba4966b898] #> #> [[4]] #> <frame 23> (13) #> expr: eval(expr, envir, enclos) #> env: [local 0x55ba496437b8] #> #> [[5]] #> <frame 22> (13) #> expr: withVisible(eval(expr, envir, enclos)) #> env: [local 0x55ba49643588] #> #> [[6]] #> <frame 21> (13) #> expr: withCallingHandlers(withVisible(eval(expr, envir, enclos)), warning = wHandler, <...> #> env: [local 0x55ba49646dd0] #> #> [[7]] #> <frame 20> (19) #> expr: doTryCatch(return(expr), name, parentenv, handler) #> env: [local 0x55ba49646820] #> #> [[8]] #> <frame 19> (18) #> expr: tryCatchOne(expr, names, parentenv, handlers[[1L]]) #> env: [local 0x55ba49646468] #> #> [[9]] #> <frame 18> (17) #> expr: tryCatchList(expr, classes, parentenv, handlers) #> env: [local 0x55ba49646120] #> #> [[10]] #> <frame 17> (16) #> expr: tryCatch(expr, error = function(e) { <...> #> env: [local 0x55ba496459e8] #> #> [[11]] #> <frame 16> (15) #> expr: try(f, silent = TRUE) #> env: [local 0x55ba49645780] #> #> [[12]] #> <frame 15> (13) #> expr: handle(ev <- withCallingHandlers(withVisible(eval(expr, envir, <...> #> env: [local 0x55ba496455f8] #> #> [[13]] #> <frame 14> (13) #> expr: timing_fn(handle(ev <- withCallingHandlers(withVisible(eval(expr, <...> #> env: [local 0x55ba49645518] #> #> [[14]] #> <frame 13> (12) #> expr: evaluate_call(expr, parsed$src[[i]], envir = envir, enclos = enclos, <...> #> env: [frame 0x55ba4965ae50] #> #> [[15]] #> <frame 12> (11) #> expr: evaluate::evaluate(code, child_env(env), new_device = TRUE) #> env: [frame 0x55ba498b6960] #> #> [[16]] #> <frame 11> (10) #> expr: downlit::evaluate_and_highlight(code, fig_save = fig_save_topic, <...> #> env: [frame 0x55ba498baf10] #> #> [[17]] #> <frame 10> (9) #> expr: highlight_examples(code, topic, env = env) #> env: [frame 0x55ba498c4998] #> #> [[18]] #> <frame 9> (8) #> expr: run_examples(tags$tag_examples[[1]], env = new.env(parent = globalenv()), <...> #> env: [frame 0x55ba498fdc78] #> #> [[19]] #> <frame 8> (6) #> expr: data_reference_topic(topic, pkg, examples = examples, run_dont_run = run_dont_run) #> env: [frame 0x55ba49b85938] #> #> [[20]] #> <frame 7> (6) #> expr: withCallingHandlers(data_reference_topic(topic, pkg, examples = examples, <...> #> env: [frame 0x55ba49b85200] #> #> [[21]] #> <frame 6> (5) #> expr: .f(.x[[i]], ...) #> env: [frame 0x55ba49ba3040] #> #> [[22]] #> <frame 5> (4) #> expr: purrr::map(topics, build_reference_topic, pkg = pkg, lazy = lazy, <...> #> env: [frame 0x55ba48927d98] #> #> [[23]] #> <frame 4> (3) #> expr: build_reference(pkg, lazy = lazy, examples = examples, run_dont_run = run_dont_run, <...> #> env: [frame 0x55ba48d0d4a0] #> #> [[24]] #> <frame 3> (2) #> expr: build_site_local(pkg = pkg, examples = examples, run_dont_run = run_dont_run, <...> #> env: [frame 0x55ba4836fa10] #> #> [[25]] #> <frame 2> (1) #> expr: build_site(pkg, devel = FALSE, preview = FALSE, install = FALSE, <...> #> env: [frame 0x55ba48368f20] #> #> [[26]] #> <frame 1> (0) #> expr: pkgdown::deploy_to_branch(new_process = FALSE) #> env: [frame 0x55ba45e8ffb0] #> #> [[27]] #> <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 24> (23) #> expr: eval(expr, envir, enclos) #> env: [local 0x55ba4966b898] #> #> [[2]] #> <frame 23> (13) #> expr: eval(expr, envir, enclos) #> env: [local 0x55ba4926d000] #> #> [[3]] #> <frame 22> (13) #> expr: withVisible(eval(expr, envir, enclos)) #> env: [local 0x55ba4926cdd0] #> #> [[4]] #> <frame 21> (13) #> expr: withCallingHandlers(withVisible(eval(expr, envir, enclos)), warning = wHandler, <...> #> env: [local 0x55ba49274420] #> #> [[5]] #> <frame 20> (19) #> expr: doTryCatch(return(expr), name, parentenv, handler) #> env: [local 0x55ba4927ddc8] #> #> [[6]] #> <frame 19> (18) #> expr: tryCatchOne(expr, names, parentenv, handlers[[1L]]) #> env: [local 0x55ba4927da80] #> #> [[7]] #> <frame 18> (17) #> expr: tryCatchList(expr, classes, parentenv, handlers) #> env: [local 0x55ba4927d700] #> #> [[8]] #> <frame 17> (16) #> expr: tryCatch(expr, error = function(e) { <...> #> env: [local 0x55ba4927d000] #> #> [[9]] #> <frame 16> (15) #> expr: try(f, silent = TRUE) #> env: [local 0x55ba4927cdd0] #> #> [[10]] #> <frame 15> (13) #> expr: handle(ev <- withCallingHandlers(withVisible(eval(expr, envir, <...> #> env: [local 0x55ba4927cc10] #> #> [[11]] #> <frame 14> (13) #> expr: timing_fn(handle(ev <- withCallingHandlers(withVisible(eval(expr, <...> #> env: [local 0x55ba4927cb30] #> #> [[12]] #> <frame 13> (12) #> expr: evaluate_call(expr, parsed$src[[i]], envir = envir, enclos = enclos, <...> #> env: [frame 0x55ba4928fef0] #> #> [[13]] #> <frame 12> (11) #> expr: evaluate::evaluate(code, child_env(env), new_device = TRUE) #> env: [frame 0x55ba498b6960] #> #> [[14]] #> <frame 11> (10) #> expr: downlit::evaluate_and_highlight(code, fig_save = fig_save_topic, <...> #> env: [frame 0x55ba498baf10] #> #> [[15]] #> <frame 10> (9) #> expr: highlight_examples(code, topic, env = env) #> env: [frame 0x55ba498c4998] #> #> [[16]] #> <frame 9> (8) #> expr: run_examples(tags$tag_examples[[1]], env = new.env(parent = globalenv()), <...> #> env: [frame 0x55ba498fdc78] #> #> [[17]] #> <frame 8> (6) #> expr: data_reference_topic(topic, pkg, examples = examples, run_dont_run = run_dont_run) #> env: [frame 0x55ba49b85938] #> #> [[18]] #> <frame 7> (6) #> expr: withCallingHandlers(data_reference_topic(topic, pkg, examples = examples, <...> #> env: [frame 0x55ba49b85200] #> #> [[19]] #> <frame 6> (5) #> expr: .f(.x[[i]], ...) #> env: [frame 0x55ba49ba3040] #> #> [[20]] #> <frame 5> (4) #> expr: purrr::map(topics, build_reference_topic, pkg = pkg, lazy = lazy, <...> #> env: [frame 0x55ba48927d98] #> #> [[21]] #> <frame 4> (3) #> expr: build_reference(pkg, lazy = lazy, examples = examples, run_dont_run = run_dont_run, <...> #> env: [frame 0x55ba48d0d4a0] #> #> [[22]] #> <frame 3> (2) #> expr: build_site_local(pkg = pkg, examples = examples, run_dont_run = run_dont_run, <...> #> env: [frame 0x55ba4836fa10] #> #> [[23]] #> <frame 2> (1) #> expr: build_site(pkg, devel = FALSE, preview = FALSE, install = FALSE, <...> #> env: [frame 0x55ba48368f20] #> #> [[24]] #> <frame 1> (0) #> expr: pkgdown::deploy_to_branch(new_process = FALSE) #> env: [frame 0x55ba45e8ffb0] #> #> [[25]] #> <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 24> (23) #> expr: eval(expr, envir, enclos) #> env: [local 0x55ba4966b898] #> #> [[2]] #> <frame 23> (13) #> expr: eval(expr, envir, enclos) #> env: [local 0x55ba48eae708] #> #> [[3]] #> <frame 22> (13) #> expr: withVisible(eval(expr, envir, enclos)) #> env: [local 0x55ba48eae4d8] #> #> [[4]] #> <frame 21> (13) #> expr: withCallingHandlers(withVisible(eval(expr, envir, enclos)), warning = wHandler, <...> #> env: [local 0x55ba48eb1cb0] #> #> [[5]] #> <frame 20> (19) #> expr: doTryCatch(return(expr), name, parentenv, handler) #> env: [local 0x55ba48eb14d0] #> #> [[6]] #> <frame 19> (18) #> expr: tryCatchOne(expr, names, parentenv, handlers[[1L]]) #> env: [local 0x55ba48eb10e0] #> #> [[7]] #> <frame 18> (17) #> expr: tryCatchList(expr, classes, parentenv, handlers) #> env: [local 0x55ba48eb0d60] #> #> [[8]] #> <frame 17> (16) #> expr: tryCatch(expr, error = function(e) { <...> #> env: [local 0x55ba48eb0200] #> #> [[9]] #> <frame 16> (15) #> expr: try(f, silent = TRUE) #> env: [local 0x55ba48eb3e00] #> #> [[10]] #> <frame 15> (13) #> expr: handle(ev <- withCallingHandlers(withVisible(eval(expr, envir, <...> #> env: [local 0x55ba48eb3c78] #> #> [[11]] #> <frame 14> (13) #> expr: timing_fn(handle(ev <- withCallingHandlers(withVisible(eval(expr, <...> #> env: [local 0x55ba48eb3a48] #> #> [[12]] #> <frame 13> (12) #> expr: evaluate_call(expr, parsed$src[[i]], envir = envir, enclos = enclos, <...> #> env: [frame 0x55ba48ee0af8] #> #> [[13]] #> <frame 12> (11) #> expr: evaluate::evaluate(code, child_env(env), new_device = TRUE) #> env: [frame 0x55ba498b6960] #> #> [[14]] #> <frame 11> (10) #> expr: downlit::evaluate_and_highlight(code, fig_save = fig_save_topic, <...> #> env: [frame 0x55ba498baf10] #> #> [[15]] #> <frame 10> (9) #> expr: highlight_examples(code, topic, env = env) #> env: [frame 0x55ba498c4998] #> #> [[16]] #> <frame 9> (8) #> expr: run_examples(tags$tag_examples[[1]], env = new.env(parent = globalenv()), <...> #> env: [frame 0x55ba498fdc78] #> #> [[17]] #> <frame 8> (6) #> expr: data_reference_topic(topic, pkg, examples = examples, run_dont_run = run_dont_run) #> env: [frame 0x55ba49b85938] #> #> [[18]] #> <frame 7> (6) #> expr: withCallingHandlers(data_reference_topic(topic, pkg, examples = examples, <...> #> env: [frame 0x55ba49b85200] #> #> [[19]] #> <frame 6> (5) #> expr: .f(.x[[i]], ...) #> env: [frame 0x55ba49ba3040] #> #> [[20]] #> <frame 5> (4) #> expr: purrr::map(topics, build_reference_topic, pkg = pkg, lazy = lazy, <...> #> env: [frame 0x55ba48927d98] #> #> [[21]] #> <frame 4> (3) #> expr: build_reference(pkg, lazy = lazy, examples = examples, run_dont_run = run_dont_run, <...> #> env: [frame 0x55ba48d0d4a0] #> #> [[22]] #> <frame 3> (2) #> expr: build_site_local(pkg = pkg, examples = examples, run_dont_run = run_dont_run, <...> #> env: [frame 0x55ba4836fa10] #> #> [[23]] #> <frame 2> (1) #> expr: build_site(pkg, devel = FALSE, preview = FALSE, install = FALSE, <...> #> env: [frame 0x55ba48368f20] #> #> [[24]] #> <frame 1> (0) #> expr: pkgdown::deploy_to_branch(new_process = FALSE) #> env: [frame 0x55ba45e8ffb0] #> #> [[25]] #> <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 26> (24) #> expr: user_fn() #> env: [local 0x55ba48a389a8] #> #> [[2]] #> <frame 25> (24) #> expr: identity(user_fn()) #> env: [local 0x55ba48a38938] #> #> [[3]] #> <frame 24> (23) #> expr: eval(expr, envir, enclos) #> env: [local 0x55ba4966b898] #> #> [[4]] #> <frame 23> (13) #> expr: eval(expr, envir, enclos) #> env: [local 0x55ba48a38708] #> #> [[5]] #> <frame 22> (13) #> expr: withVisible(eval(expr, envir, enclos)) #> env: [local 0x55ba48a384a0] #> #> [[6]] #> <frame 21> (13) #> expr: withCallingHandlers(withVisible(eval(expr, envir, enclos)), warning = wHandler, <...> #> env: [local 0x55ba48a37d30] #> #> [[7]] #> <frame 20> (19) #> expr: doTryCatch(return(expr), name, parentenv, handler) #> env: [local 0x55ba48a3b540] #> #> [[8]] #> <frame 19> (18) #> expr: tryCatchOne(expr, names, parentenv, handlers[[1L]]) #> env: [local 0x55ba48a3b1f8] #> #> [[9]] #> <frame 18> (17) #> expr: tryCatchList(expr, classes, parentenv, handlers) #> env: [local 0x55ba48a3aeb0] #> #> [[10]] #> <frame 17> (16) #> expr: tryCatch(expr, error = function(e) { <...> #> env: [local 0x55ba48a3a7e8] #> #> [[11]] #> <frame 16> (15) #> expr: try(f, silent = TRUE) #> env: [local 0x55ba48a3a580] #> #> [[12]] #> <frame 15> (13) #> expr: handle(ev <- withCallingHandlers(withVisible(eval(expr, envir, <...> #> env: [local 0x55ba48a3a3f8] #> #> [[13]] #> <frame 14> (13) #> expr: timing_fn(handle(ev <- withCallingHandlers(withVisible(eval(expr, <...> #> env: [local 0x55ba48a3a318] #> #> [[14]] #> <frame 13> (12) #> expr: evaluate_call(expr, parsed$src[[i]], envir = envir, enclos = enclos, <...> #> env: [frame 0x55ba48a50ef8] #> #> [[15]] #> <frame 12> (11) #> expr: evaluate::evaluate(code, child_env(env), new_device = TRUE) #> env: [frame 0x55ba498b6960] #> #> [[16]] #> <frame 11> (10) #> expr: downlit::evaluate_and_highlight(code, fig_save = fig_save_topic, <...> #> env: [frame 0x55ba498baf10] #> #> [[17]] #> <frame 10> (9) #> expr: highlight_examples(code, topic, env = env) #> env: [frame 0x55ba498c4998] #> #> [[18]] #> <frame 9> (8) #> expr: run_examples(tags$tag_examples[[1]], env = new.env(parent = globalenv()), <...> #> env: [frame 0x55ba498fdc78] #> #> [[19]] #> <frame 8> (6) #> expr: data_reference_topic(topic, pkg, examples = examples, run_dont_run = run_dont_run) #> env: [frame 0x55ba49b85938] #> #> [[20]] #> <frame 7> (6) #> expr: withCallingHandlers(data_reference_topic(topic, pkg, examples = examples, <...> #> env: [frame 0x55ba49b85200] #> #> [[21]] #> <frame 6> (5) #> expr: .f(.x[[i]], ...) #> env: [frame 0x55ba49ba3040] #> #> [[22]] #> <frame 5> (4) #> expr: purrr::map(topics, build_reference_topic, pkg = pkg, lazy = lazy, <...> #> env: [frame 0x55ba48927d98] #> #> [[23]] #> <frame 4> (3) #> expr: build_reference(pkg, lazy = lazy, examples = examples, run_dont_run = run_dont_run, <...> #> env: [frame 0x55ba48d0d4a0] #> #> [[24]] #> <frame 3> (2) #> expr: build_site_local(pkg = pkg, examples = examples, run_dont_run = run_dont_run, <...> #> env: [frame 0x55ba4836fa10] #> #> [[25]] #> <frame 2> (1) #> expr: build_site(pkg, devel = FALSE, preview = FALSE, install = FALSE, <...> #> env: [frame 0x55ba48368f20] #> #> [[26]] #> <frame 1> (0) #> expr: pkgdown::deploy_to_branch(new_process = FALSE) #> env: [frame 0x55ba45e8ffb0] #> #> [[27]] #> <frame 0> [global] #> expr: NULL #> env: [global] #> #> attr(,"class") #> [1] "ctxt_stack" "stack"