lambdas in `alt` text have unexpected and likely incorrect behavior
Open
Nobody has claimed this yet.
- Dominant language
- R
- Stars
- 7k
- Forks
- 2.1k
- Avg merge
- 59m
- Merged PRs (30d)
- 2
Description
Problem
If I try to include a lambda in the alt text of labs, the plot will throw an error.
> ggplot2::ggplot(mtcars, ggplot2::aes(x = wt, y = mpg)) +
+ ggplot2::geom_point() +
+ ggplot2::labs(
+ alt = ~ sprintf(
+ "Scatter plot showing %s on x-axis and %s on y-axis",
+ .@labels$x,
+ .@labels$y
+ )
+ )
Error in `.@labels`:
! no applicable method for `@` applied to an object of class "character"
Hide Traceback
▆
1. ├─base (local) `<fn>`(x)
2. └─ggplot2 (local) `print.ggplot2::ggplot`(x)
3. ├─ggplot2::ggplot_build(x)
4. └─ggplot2 (local) `ggplot_build.ggplot2::ggplot`(x)
5. └─ggplot2:::setup_plot_labels(plot, layers, data)
6. └─base::lapply(...)
7. └─ggplot2 (local) FUN(X[[i]], ...)
8. └─global label(labels[[nm]] %||% "")
9. └─base::sprintf(...)
However, ggplot2::get_alt_text does work as expected.
> p <- ggplot2::ggplot(mtcars, ggplot2::aes(x = wt, y = mpg)) +
+ ggplot2::geom_point() +
+ ggplot2::labs(
+ alt = ~ sprintf(
+ "Scatter plot showing %s on x-axis and %s on y-axis",
+ .@labels$x,
+ .@labels$y
+ )
+ )
> ggplot2::get_alt_text(p)
[1] "Scatter plot showing wt on x-axis and mpg on y-axis"
Solution
I will open a small PR to fix this. I believe the solution is as simple as changing this lapply on line labels.R#L108 to match the call on labels.R#L336
Contributor guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
Research direction
Start in R/labels.R at the lapply on line 108 and compare it with the call around line 336. Reproduce the mtcars example with labs(alt = ~ sprintf(...)) and verify that printing the plot no longer errors while get_alt_text(p) still returns the expected axis-label text.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- r
- Domain
- data-visualization
- Issue type
- Bug
- Difficulty
- 1/5
- Estimated time
- Under an hour
- Activity status
- Stale
- Clarity
- Clearly specified
- Newbie friendliness
- 35/100