Skip to content

fix(renderPlot): get interactive plotting working with ggplot2 v4.0 #4228

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Jun 12, 2025

Conversation

cpsievert
Copy link
Collaborator

@cpsievert cpsievert commented Jun 10, 2025

This update gets renderPlot() interactive ggplot2 feature working with ggplot2's upcoming transition from S3 to S7 (tidyverse/ggplot2#6364).

Note that #4226 fixed a similar issue, but only for our unit tests. The same problem exists in renderPlot()'s interactive feature, which we don't have any automated tests for.

For a minimal example, with this app, no brushed points are returned when brushing the plot.

library(shiny)
library(ggplot2)
library(bslib)

ui <- page_fixed(
    card(
    card_header("Interactive Brushing Plot"),
    plotOutput("plot", brush = "plot_brush"),
    card_footer("Drag to select points on the plot")
  ),
  card(
    card_header("Selected Data Points"),
    tableOutput("brushed_data")
  )
)

server <- function(input, output, session) {
  
  output$plot <- renderPlot({
    ggplot(iris, aes(x = Sepal.Length, y = Sepal.Width, color = Species)) +
      geom_point(size = 3) +
      theme_minimal()
  })
  
  output$brushed_data <- renderTable({
    req(input$plot_brush)
    
    brushed_data <- brushedPoints(
      iris, input$plot_brush, 
      xvar = "Sepal.Length", yvar = "Sepal.Width"
    )
    
    if(nrow(brushed_data) == 0) {
      return(data.frame(Message = "No points selected"))
    }
    
    brushed_data
  })
}

shinyApp(ui = ui, server = server)
Screenshot 2025-06-10 at 10 01 20 AM

With this change, you'll get the expected behavior:

Screenshot 2025-06-10 at 10 04 23 AM

Testing notes

  • First, verify the example above is broken by installing the new ggplot2 remotes::install_github("tidyverse/ggplot2#6364")
  • Next, install this change, and verify the problem is fixed: remotes::install_github("rstudio/shiny#4228")

More generally, we should do more testing of shiny's interactive ggplot2 plot feature to make sure there isn't more changes that we're missing. We still have a handful of apps that we designed for manually testing, but never got automated tests:

093-plot-interaction-basic
094-image-interaction-basic
095-plot-interaction-advanced
096-plot-interaction-article-1
097-plot-interaction-article-2
098-plot-interaction-article-3
099-plot-interaction-article-4
100-plot-interaction-article-5
101-plot-interaction-article-6
102-plot-interaction-article-7
103-plot-interaction-article-8
104-plot-interaction-select
105-plot-interaction-zoom
106-plot-interaction-exclude

It'd also be great to verify that these apps are working as intended with the latest ggplot2 (plus this change). Testing across different environments isn't that important (we're just looking for changes in ggplot2's R code that we're not accounting for).

@cpsievert cpsievert requested a review from wch June 10, 2025 15:15
@karangattu
Copy link

The tests across all listed apps showed no issues with the ggplot2 plotting functionality in this branch of Shiny.

@cpsievert cpsievert force-pushed the fix/ggplot2-4.0-compat branch from 24ecd0d to 522fe88 Compare June 12, 2025 21:12
@cpsievert cpsievert merged commit 57bb3a1 into main Jun 12, 2025
12 checks passed
@cpsievert cpsievert deleted the fix/ggplot2-4.0-compat branch June 12, 2025 21:23
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants