Numeric Input responds too quickly in Shiny

402 views Asked by At

I want Shiny to wait a little bit for the user to input their group size (without using a button). This is a simpler version of my code, but in my actual code, I have more user inputs (so I only want Shiny to wait 2 seconds for this input only). I've been trying to figure out how I would use debounce for this code, but I'm not sure.

library(shiny)

shinyApp(ui <- fluidPage(sidebarPanel(
  "",
  numericInput("groupSize", label =
                 "How many people will be with you?", value = ""), 
  textOutput("output")
)) ,

server <- function(input, output, session) {
  getNumber <- reactive({
    
  req(input$groupSize>=0)
  groupSize <- input$groupSize
  })
  
  output$output <- renderText({ 
    getNumber()
   })
})

1

There are 1 answers

0
Waldi On BEST ANSWER

This works with debounce:

  1. create a reactive input function groupsize
  2. pass this function to debounce to create a new function groupsize_d
  3. use this new function for rendering
library(shiny)

shinyApp(ui <- fluidPage(sidebarPanel(
  "",
  numericInput("groupSize", label =
                 "How many people will be with you?", value = ""), 
  textOutput("output")
)) ,

server <- function(input, output, session) {
  groupsize <- reactive(input$groupSize)
  groupsize_d <- debounce(groupsize,2000)
  
  getNumber <- reactive({
    
    req(groupsize_d()>=0)
    groupsize_d()
  })
  
  output$output <- renderText({ 
    getNumber()
  })
})