Skip to content

Instantly share code, notes, and snippets.

@krlmlr
Last active January 7, 2021 01:59
Show Gist options
  • Star 10 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save krlmlr/33ec72d196b1542b9c4f9497d981de49 to your computer and use it in GitHub Desktop.
Save krlmlr/33ec72d196b1542b9c4f9497d981de49 to your computer and use it in GitHub Desktop.
Pretty stack traces in R
# Add this to your .Rprofile
options(
error = quote(rlang::entrace()),
rlang__backtrace_on_error = "collapse" # or "branch" or "full"
)
@stufield
Copy link

Hi Kirill, this is fantastic!
I'd like to demo this to my team, but I'm having trouble having the trace show in a reprex ... likely the errorHandlers are intercepted somewhere within reprex differently than in the console. Any ideas?

reprex::reprex({
library(rlang)

f <- function(x) {
  x + g(x)
}

g <- function(y) {
  foo <- y * 2
  bar <- paste0(foo, h())
  bar - 100
}

h <- function() {
  stop("error out here", call. = FALSE)
}

f(3.14)
traceback()

#' Much more convenient to set the `errorHandler` intercept in `.Rprofile`
options(error = quote(rlang::entrace()),
        rlang__backtrace_on_error = "collapse")   # or 'full'

f(3.14)
})

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment