Skip to content

lambdas in alt text have unexpected and likely incorrect behavior #6725

Open

Description

@JacobBumgarner

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

Activity

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions