rlang_backtrace_on_error {rlang}R Documentation

Display backtrace on error

Description

rlang errors carry a backtrace that can be inspected by calling last_error(). You can also control the default display of the backtrace by setting the option rlang_backtrace_on_error to one of the following values:

rlang errors are normally thrown with abort(). If you promote base errors to rlang errors with global_entrace(), rlang_backtrace_on_error applies to all errors.

Promote base errors to rlang errors

You can use options(error = rlang::entrace) to promote base errors to rlang errors. This does two things:

Warnings and errors in RMarkdown

The display of errors depends on whether they're expected (i.e. chunk option error = TRUE) or unexpected:

When knitr is running (as determined by the knitr.in.progress global option), the default top environment for backtraces is set to the chunk environment knitr::knit_global(). This ensures that the part of the call stack belonging to knitr does not end up in backtraces. If needed, you can override this by setting the rlang_trace_top_env global option.

Similarly to rlang_backtrace_on_error_report, you can set rlang_backtrace_on_warning_report inside RMarkdown documents to tweak the display of warnings. This is useful in conjunction with global_entrace(). Because of technical limitations, there is currently no corresponding rlang_backtrace_on_warning option for normal R sessions.

To get full entracing in an Rmd document, include this in a setup chunk before the first error or warning is signalled.

```{r setup}
rlang::global_entrace()
options(rlang_backtrace_on_warning_report = "full")
options(rlang_backtrace_on_error_report = "full")
```

See Also

rlang_backtrace_on_warning

Examples

# Display a simplified backtrace on error for both base and rlang
# errors:

# options(
#   rlang_backtrace_on_error = "branch",
#   error = rlang::entrace
# )
# stop("foo")

[Package rlang version 1.1.3 Index]