rstudio / shiny

Easy interactive web applications with R
https://shiny.posit.co/
Other
5.37k stars 1.87k forks source link

Moving/resizing brush is not debounced if plot redraws #1642

Open wch opened 7 years ago

wch commented 7 years ago

I mentioned this in #1634, but it's actually a separate issue. To reproduce:

Drag to create a brush on the points. Move the brush, then wait long enough for it to send the values to the server, then, before the server finishes making the new plots and sending them back, move the brush, and keep moving it. The plot will keep redrawing while you drag. This is easier to do with options(shiny.trace=TRUE).

I think problem is that when the client receives the updated plot, it creates a new debounced brush function and does not delete the previous one; once the previous plot's debounced brush function reaches the debounce time, it sends the value, even though the plot has been replaced and a new debounced brush function has been created.

library(shiny)
library(ggplot2)
shinyApp(
  ui = fluidPage(
    plotOutput("p", brush = "brush", width = "400px"),
    verbatimTextOutput("showcount")
  ),
  server = function(input, output) {
    dataWithSelection <- reactive({
      brushedPoints(mtcars, input$brush, allRows = TRUE)
    })

    count <- reactiveVal(0)

    output$p <- renderPlot({
      isolate(count(count() + 1))

      ggplot(dataWithSelection(), aes(wt, mpg)) +
        geom_point(aes(color = selected_)) +
        scale_color_manual(values = c("black", "#66D65C"), guide = FALSE)
    })

    output$showcount <- renderText({
      paste("Number of redraws:", count())
    })
  }
)
dvg-p4 commented 2 years ago

I'm working on overhauling the root cause of this problem right now, amidst some other brush-related features and fixes, at https://github.com/dvg-p4/shiny