JohnCoene / g2r

📈 Grammar of graphics for interactive visualisations
Other
148 stars 10 forks source link

Shiny reactivity cause new plot to be generated at each change #3

Closed DivadNojnarg closed 5 years ago

DivadNojnarg commented 5 years ago

As discussed before:

library(shiny)
library(g2r)

shinyApp(
  ui = fluidPage(
    sliderInput(
      inputId = "obs",
      label = "Number of rows:",
      min = 1,
      max = nrow(cars),
      value = 10
    ),
    g2Output("chart")
  ),
  server = function(input, output) {
    output$chart <- renderG2({
      g2(cars[1:input$obs, ], asp(speed, dist)) %>%
        fig_point() %>%
        fig_smooth(method = "loess") %>% 
        motif_dark()
    })
  }
)
JohnCoene commented 5 years ago

Thanks for reporting that David! I just pushed a fix. Note that to do what you are after there is a proxy (below) which changes the underlying data which is faster as it doesn't redraw everything (i.e.: axes) and makes the animation much better.

library(shiny)
library(g2r)

shinyApp(
  ui = fluidPage(
    sliderInput(
      inputId = "obs",
      label = "Number of rows:",
      min = 1,
      max = nrow(cars),
      value = 10,
      step = 1
    ),
    g2Output("chart")
  ),
  server = function(input, output) {
    output$chart <- renderG2({
      g2(cars, asp(speed, dist)) %>%
        fig_point() %>%
        fig_smooth(method = "loess") %>% 
        motif_dark()
    })

    observeEvent(input$obs, {
      g2Proxy("chart") %>% 
        change_data(cars[1:input$obs, ], speed, dist)
    })
  }
)
DivadNojnarg commented 5 years ago

Cool!