Trying to understand how to use debounce in shiny

I have a shiny app (https://ajackson.shinyapps.io/DisplaySidewalkQuality/) which is rather slow, and could be greatly improved by using debounce, to reduce unneeded redraws. The problem is that I can't seem to grok how to actually use it, given how I have structured the code. I have a simplified reprex below that does not debounce but captures the basic code structure I have used.

#
# Reprex for understanding how to utilize debounce/throttle functionality
#
#   Uses Iris data
#

library(shiny)
library(tidyverse)

shinyApp(
  ui = basicPage(
    titlePanel("Test of debounce"),
    # Sidebar with selector for which species
    sidebarLayout(
      sidebarPanel(             
        checkboxGroupInput("species", label = "choose species",
                                                   choices = list("Setosa"="setosa", 
                                                                  "Versicolor"="versicolor", 
                                                                  "Virginica"="virginica" 
                                                   ),
                                                   selected = "")
      ),
      
      # Show a plot of the generated distribution
      mainPanel(
        plotOutput("plot1")
      )
    ) 
  ), # basicPage
  
  #####################################################
  # Define Server 
  #####################################################
  
  server <- function(input, output, session) {
    
    ##################################
    #           Make Plot
    ##################################
    draw_plot <- function() {
      print(paste("species",input$species))
      
      output$plot1 <- renderPlot({
        if (!is.null(input$species)){
          
        iris %>% 
            filter(Species %in% input$species) %>% 
            ggplot(aes(x=Petal.Length, y=Petal.Width)) + 
            geom_point(aes(color=Species))
        }
      })
    }
    
    ##################################
    #  CheckBoxes
    ##################################
    observeEvent(input$species, {
      draw_plot()
    }, ignoreNULL=FALSE, ignoreInit=TRUE)
    
  }
)

Hi @Ajackson. debounce need a reactive expression as the argument. First, construct the reactive expression with reactive function on the input value that you want to target. The debounce function will output a reactive expression like the first argument that pass to it with addition of debounce property. Then, you can use the reactive expression for the following plotting.

library(shiny)
library(tidyverse)

shinyApp(
  ui = basicPage(
    titlePanel("Test of debounce"),
    # Sidebar with selector for which species
    sidebarLayout(
      sidebarPanel(             
        checkboxGroupInput("species", label = "choose species",
                           choices = list("Setosa"="setosa", 
                                          "Versicolor"="versicolor", 
                                          "Virginica"="virginica" 
                           ),
                           selected = "")
      ),
      
      # Show a plot of the generated distribution
      mainPanel(
        plotOutput("plot1")
      )
    ) 
  ), # basicPage
  
  #####################################################
  # Define Server 
  #####################################################
  
  server <- function(input, output, session) {
    species <- reactive(input$species)
    
    species_d <- species %>%
      debounce(2000)
    
    ##################################
    #           Make Plot
    ##################################
    draw_plot <- function() {
      print(paste("species",species_d()))
      
      output$plot1 <- renderPlot({
        if (!is.null(species_d())){
          
          iris %>% 
            filter(Species %in% species_d()) %>% 
            ggplot(aes(x=Petal.Length, y=Petal.Width)) + 
            geom_point(aes(color=Species))
        }
      })
    }
    
    ##################################
    #  CheckBoxes
    ##################################
    observeEvent(species_d(), {
      draw_plot()
    }, ignoreNULL=FALSE, ignoreInit=TRUE)
    
  }
)

Thanks! That does it.

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.