ggplot2 icon indicating copy to clipboard operation
ggplot2 copied to clipboard

Error: ! Failed to evaluate glue component

Open sirallen opened this issue 2 years ago • 6 comments

I started getting an error when using glue in ggplot as demonstrated below. The code runs successfully when the geom_hline() layer is commented out:

library(dplyr)
library(glue)
library(ggplot2)
library(tidyr)

data <-
  crossing(tier = factor(1:5),
           x = 1:5) %>%
  mutate(y = rnorm(n()))

data %>%
  ggplot(aes(x, y)) +
  geom_hline(yintercept = 0) +
  geom_point() +
  facet_wrap(~ glue('T{tier}'))

gives Error: ! Failed to evaluate glue component {tier}.

The stack trace:

Backtrace:
     ▆
  1. ├─base (local) `<fn>`(x)
  2. ├─ggplot2:::print.ggplot(x)
  3. │ ├─ggplot2::ggplot_build(x)
  4. │ └─ggplot2:::ggplot_build.ggplot(x)
  5. │   └─layout$setup(data, plot$data, plot$plot_env)
  6. │     └─ggplot2 (local) setup(..., self = self)
  7. │       └─self$facet$compute_layout(data, self$facet_params)
  8. │         └─ggplot2 (local) compute_layout(..., self = self)
  9. │           └─ggplot2::combine_vars(data, params$plot_env, vars, drop = params$drop)
 10. │             ├─ggplot2:::compact(lapply(data, eval_facets, facets = vars, possible_columns = possible_columns))
 11. │             │ └─base::vapply(x, is.null, logical(1))
 12. │             └─base::lapply(data, eval_facets, facets = vars, possible_columns = possible_columns)
 13. │               └─ggplot2 (local) FUN(X[[i]], ...)
 14. │                 ├─ggplot2:::compact(lapply(facets, eval_facet, data, possible_columns = possible_columns))
 15. │                 │ └─base::vapply(x, is.null, logical(1))
 16. │                 └─base::lapply(facets, eval_facet, data, possible_columns = possible_columns)
 17. │                   └─ggplot2 (local) FUN(X[[i]], ...)
 18. │                     ├─base::tryCatch(eval_tidy(facet, mask), ggplot2_missing_facet_var = function(e) NULL)
 19. │                     │ └─base (local) tryCatchList(expr, classes, parentenv, handlers)
 20. │                     │   └─base (local) tryCatchOne(expr, names, parentenv, handlers[[1L]])
 21. │                     │     └─base (local) doTryCatch(return(expr), name, parentenv, handler)
 22. │                     └─rlang::eval_tidy(facet, mask)
 23. ├─glue::glue("T{tier}")
 24. │ └─glue::glue_data(...)
 25. ├─glue (local) `<fn>`("tier")
 26. │ ├─.transformer(expr, env) %||% .null
 27. │ └─glue (local) .transformer(expr, env)
 28. │   ├─glue:::with_glue_error(...)
 29. │   │ └─base::withCallingHandlers(...)
 30. │   └─base::eval(expr, envir)
 31. │     └─base::eval(expr, envir)
 32. └─ggplot2 (local) `<fn>`()

sirallen avatar Jan 30 '24 18:01 sirallen

Thanks for the report! Did this code used to work (also with geom_hline()) and no longer does?

teunbrand avatar Jan 30 '24 19:01 teunbrand

Yes, it worked previously -- I auto-updated some packages (not sure which ones) and now it no longer works.

sirallen avatar Jan 30 '24 19:01 sirallen

Thanks that is helpful to know. I can reproduce this on my end, but can I ask you about the versions of R, ggplot2, scales and glue you're using anyway?

teunbrand avatar Jan 30 '24 19:01 teunbrand

R version 4.3.2 (2023-10-31)
Platform: aarch64-apple-darwin20 (64-bit)
Running under: macOS Big Sur 11.4

... 

other attached packages:
[1] ... glue_1.7.0    ggplot2_3.4.4 ...

loaded via a namespace (and not attached):
 [1]  ... scales_1.3.0    ...

sirallen avatar Jan 30 '24 19:01 sirallen

I've tested a little bit and it all seems to work fine with glue 1.6.2 but not 1.7.0.

I think the issue is as follows. ggplot2 throws a special error when facetting columns are absent, like for the geom_hline() case. This error is then caught and is given special treatment (i.e. it applies to all facets). However, glue now wraps errors (see https://github.com/tidyverse/glue/pull/294) that hide the special ggplot2 error. The special error is still there, but it doesn't get caught as such.

teunbrand avatar Jan 31 '24 08:01 teunbrand

@teunbrand Thank you for investigating!

sirallen avatar Jan 31 '24 22:01 sirallen