EVOLUTION-MANAGER
Edit File: trace_back.html
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"><html xmlns="http://www.w3.org/1999/xhtml"><head><title>R: Capture a backtrace</title> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <link rel="stylesheet" type="text/css" href="R.css" /> </head><body> <table width="100%" summary="page for trace_back {rlang}"><tr><td>trace_back {rlang}</td><td style="text-align: right;">R Documentation</td></tr></table> <h2>Capture a backtrace</h2> <h3>Description</h3> <p>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 <code>print()</code> method for this object will reveal. </p> <p>Users rarely need to call <code>trace_back()</code> manually. Instead, signalling an error with <code><a href="abort.html">abort()</a></code> or setting up <code><a href="global_entrace.html">global_entrace()</a></code> is the most common way to create backtraces when an error is thrown. Inspect the backtrace created for the most recent error with <code><a href="last_error.html">last_error()</a></code>. </p> <p><code>trace_length()</code> returns the number of frames in a backtrace. </p> <h3>Usage</h3> <pre> trace_back(top = NULL, bottom = NULL) trace_length(trace) </pre> <h3>Arguments</h3> <table summary="R argblock"> <tr valign="top"><td><code>top</code></td> <td> <p>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. </p> <p>This is needed in particular when you call <code>trace_back()</code> 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. </p> <p>If not supplied, the <code>rlang_trace_top_env</code> global option is consulted. This makes it possible to trim the embedding context for all backtraces created while the option is set. If knitr is in progress, the default value for this option is <code>knitr::knit_global()</code> so that the knitr context is trimmed out of backtraces.</p> </td></tr> <tr valign="top"><td><code>bottom</code></td> <td> <p>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. </p> <p>Set this when you would like to capture a backtrace without the capture context. </p> <p>Can also be an integer that will be passed to <code><a href="stack.html">caller_env()</a></code>.</p> </td></tr> <tr valign="top"><td><code>trace</code></td> <td> <p>A backtrace created by <code>trace_back()</code>.</p> </td></tr> </table> <h3>Examples</h3> <pre> # 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() # Lazy evaluation introduces a tree like structure identity(identity(f())) identity(try(f())) try(identity(f())) # 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 print(x, simplify = "branch") # 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}) x # 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) 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) close(conn) </pre> <hr /><div style="text-align: center;">[Package <em>rlang</em> version 1.0.6 <a href="00Index.html">Index</a>]</div> </body></html>