Skip to contents
  • last_layer_errorcontext() returns the error context at the level of the Layer ggproto.

  • last_sublayer_errorcontext() (EXPERIMENTAL) returns the error context at the sub-Layer level (e.g., Stat or Geom).

Usage

last_layer_errorcontext(reprint_error = FALSE, ggtrace_notes = TRUE)

last_sublayer_errorcontext(reprint_error = FALSE, ggtrace_notes = TRUE)

Arguments

reprint_error

Re-prints the original error message to the console. Defaults to FALSE.

ggtrace_notes

Prints the ggtrace_inspect_args() call used to inspect the error context. Defaults to TRUE.

Value

An dynamically constructed and evaluated call to ggtrace_inspect_args(). Prioritizes showing the state of layer data whenever possible (by extracting the data argument).

Scope

These functions can only retrieve information from errors propagating from Layer ggproto methods. In non-technical terms, they only work for errors with a "Error occured in the Nth layer" message (as of {ggplot2} >= 3.4.0).

The scope of last_sublayer_errorcontext() is narrower, since not all Layer methods call a sub-Layer method. This function is intended for developers - in most cases users can get all the information necessary to debug layer code from last_layer_errorcontext() (there are only so many ways to break a ggplot from user-facing code).

Examples

if (FALSE) {
library(ggplot2)
erroring_barplot1 <- ggplot(mtcars, aes(mpg, hp)) +
  stat_summary(fun.data = "mean_se") +
  geom_bar()

# Render to trigger error
erroring_barplot1

# Both return the same snapshot of layer data
# but at different levels of specificity
last_layer_errorcontext()
last_sublayer_errorcontext()

erroring_barplot2 <- ggplot(mtcars, aes(mpg, hp)) +
  stat_summary() +
  geom_bar(aes(y = c(1, 2)))
erroring_barplot2

# This works:
last_layer_errorcontext()
# This doesn't: there's no sub-layer ggproto involved in this error
last_sublayer_errorcontext()

library(ggforce)
erroring_sina <- ggplot(mtcars, aes(mpg)) +
  geom_bar() +
  geom_sina()
erroring_barplot1

# The two return different snapshots of layer data here -
# see `ggplot2:::Layer$compute_statistic` for why.
last_layer_errorcontext()
last_sublayer_errorcontext()

}