Skip to content

Commit

Permalink
fix expr construction
Browse files Browse the repository at this point in the history
  • Loading branch information
yjunechoe committed Oct 18, 2024
1 parent 63366da commit 08a28e6
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 5 deletions.
9 changes: 5 additions & 4 deletions R/last-errorcontext.R
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,9 @@
#'
#' }
last_layer_errorcontext <- function(reprint_error = FALSE, ggtrace_notes = TRUE) { # nocov start
p <- eval.parent(rlang::call2(call("::", rlang::sym("ggplot2"), rlang::sym("last_plot"))))

last_plot_expr <- rlang::call2("last_plot", .ns = resolve_ns("ggplot2"))
p <- eval.parent(last_plot_expr)
tr <- rlang::last_trace()

layer_i <- gsub("Error occurred in the (\\d+).. layer.", "\\1", tr$body[1])
Expand All @@ -86,7 +88,7 @@ last_layer_errorcontext <- function(reprint_error = FALSE, ggtrace_notes = TRUE)
ggtrace_ns <- resolve_ns("ggtrace")
ggtrace_expr <- rlang::call2(
"inspect_args",
quote(p),
last_plot_expr,
method_expr,
rlang::call2("layer_is", layer_i, .ns = ggtrace_ns),
error = TRUE,
Expand All @@ -103,13 +105,12 @@ last_layer_errorcontext <- function(reprint_error = FALSE, ggtrace_notes = TRUE)
}

if (ggtrace_notes) {
ggtrace_expr$x <- call("last_plot")
if ("data" %in% names(out)) {
ggtrace_expr <- call("$", ggtrace_expr, quote(data))
out <- asNamespace("tibble")$as_tibble(out$data)
}
ggtrace_expr_fmt <- rlang::expr_deparse(ggtrace_expr, width = Inf)
cli::cli_alert_success("Executed {.code {ggtrace_expr_fmt}}")
cli::cli_alert_success("Ran {.code {ggtrace_expr_fmt}}")
}

out
Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -211,7 +211,7 @@ Note that you can more conveniently call `last_layer_errorcontext()` to
the same effect:

last_layer_errorcontext()
#> ✔ Executed `inspect_args(p, ggplot2:::Layer$compute_geom_1, layer_is(2L), error = TRUE, x = last_plot())$data`
#> ✔ Ran `inspect_args(last_plot(), ggplot2:::Layer$compute_geom_1, layer_is(2L), error = TRUE)$data`
#> # A tibble: 3 × 14
#> ymin lower middle upper ymax outliers notchupper notchlower x width
#> <dbl> <dbl> <dbl> <dbl> <dbl> <list> <dbl> <dbl> <dbl> <dbl>
Expand Down

0 comments on commit 08a28e6

Please sign in to comment.