A backtrace captures the sequence of calls that lead to the current function, sometimes called the call stack. Because of lazy evaluation, the call stack in R is actually a tree, which the summary() method of this object will reveal.

trace_back(top = NULL, bottom = NULL)

Arguments

top

The first frame environment to be included in the backtrace. This becomes the top of the backtrace tree and represents the oldest call in the backtrace.

This is needed in particular when you call trace_back() indirectly or from a larger context, for example in tests or inside an RMarkdown document where you don't want all of the knitr evaluation mechanisms to appear in the backtrace.

bottom

The last frame environment to be included in the backtrace. This becomes the rightmost leaf of the backtrace tree and represents the youngest call in the backtrace.

Set this when you would like to capture a backtrace without the capture context.

Can also be an integer that will be passed to caller_env().

Examples

# Trim backtraces automatically (this improves the generated # documentation for the rlang website and the same trick can be # useful within knitr documents): options(rlang_trace_top_env = current_env()) f <- function() g() g <- function() h() h <- function() trace_back() # When no lazy evaluation is involved the backtrace is linear # (i.e. every call has only one child) f()
#> █ #> └─global::f() #> └─global::g() #> └─global::h()
# Lazy evaluation introduces a tree like structure identity(identity(f()))
#> █ #> ├─[ base::identity(...) ] #> ├─[ base::identity(...) ] #> └─global::f() #> └─global::g() #> └─global::h()
#> █ #> ├─[ base::identity(...) ] #> ├─[ base::try(...) ] with 4 more calls #> └─global::f() #> └─global::g() #> └─global::h()
#> █ #> ├─[ base::try(...) ] with 4 more calls #> ├─[ base::identity(...) ] #> └─global::f() #> └─global::g() #> └─global::h()
# When printing, you can request to simplify this tree to only show # the direct sequence of calls that lead to `trace_back()` x <- try(identity(f())) x
#> █ #> ├─[ base::try(...) ] with 4 more calls #> ├─[ base::identity(...) ] #> └─global::f() #> └─global::g() #> └─global::h()
print(x, simplify = "branch")
#> ─base::try(identity(f())) #> ─global::f() #> ─global::g() #> ─global::h()
# With a little cunning you can also use it to capture the # tree from within a base NSE function x <- NULL with(mtcars, {x <<- f(); 10})
#> [1] 10
x
#> █ #> ├─[ base::with(...) ] #> └─base::with.default(...) #> └─[ base::eval(...) ] with 1 more call #> └─global::f() #> └─global::g() #> └─global::h()
# Restore default top env for next example options(rlang_trace_top_env = NULL) # When code is executed indirectly, i.e. via source or within an # RMarkdown document, you'll tend to get a lot of guff at the beginning # related to the execution environment: conn <- textConnection("summary(f())") source(conn, echo = TRUE, local = TRUE)
#> #> > summary(f()) #> █ #> ├─base::tryCatch(...) #> │ └─base:::tryCatchList(expr, classes, parentenv, handlers) #> │ ├─base:::tryCatchOne(...) #> │ │ └─base:::doTryCatch(return(expr), name, parentenv, handler) #> │ └─base:::tryCatchList(expr, names[-nh], parentenv, handlers[-nh]) #> │ └─base:::tryCatchOne(expr, names, parentenv, handlers[[1L]]) #> │ └─base:::doTryCatch(return(expr), name, parentenv, handler) #> ├─base::withCallingHandlers(...) #> ├─base::saveRDS(...) #> ├─base::do.call(...) #> ├─(function (what, args, quote = FALSE, envir = parent.frame()) ... #> └─(function (..., crayon_enabled, crayon_colors, pkgdown_internet) ... #> └─pkgdown::build_site(...) #> └─pkgdown:::build_site_local(...) #> └─pkgdown::build_reference(...) #> └─purrr::map(...) #> └─pkgdown:::.f(.x[[i]], ...) #> └─pkgdown:::data_reference_topic(...) #> ├─pkgdown:::as_data(...) #> └─pkgdown:::as_data.tag_examples(...) #> └─purrr::pmap_chr(...) #> └─pkgdown:::.f(...) #> ├─withr::with_options(...) #> │ └─base::force(code) #> └─evaluate::evaluate(code, env, new_device = TRUE) #> └─evaluate:::evaluate_call(...) #> ├─evaluate:::timing_fn(...) #> ├─evaluate:::handle(...) #> │ └─base::try(f, silent = TRUE) #> │ └─base::tryCatch(...) #> │ └─base:::tryCatchList(expr, classes, parentenv, handlers) #> │ └─base:::tryCatchOne(expr, names, parentenv, handlers[[1L]]) #> │ └─base:::doTryCatch(return(expr), name, parentenv, handler) #> ├─base::withCallingHandlers(...) #> ├─base::withVisible(eval(expr, envir, enclos)) #> └─base::eval(expr, envir, enclos) #> └─base::eval(expr, envir, enclos) #> ├─base::source(conn, echo = TRUE, local = TRUE) #> │ ├─base::withVisible(eval(ei, envir)) #> │ └─base::eval(ei, envir) #> │ └─base::eval(ei, envir) #> ├─base::summary(f()) #> └─global::f() #> └─global::g() #> └─global::h()
close(conn) # To automatically strip this off, specify which frame should be # the top of the backtrace. This will automatically trim off calls # prior to that frame: top <- current_env() h <- function() trace_back(top) conn <- textConnection("summary(f())") source(conn, echo = TRUE, local = TRUE)
#> #> > summary(f()) #> █ #> ├─base::summary(f()) #> └─global::f() #> └─global::g() #> └─global::h()
close(conn)