首页 > 解决方案 > 数字输入在 Shiny 中响应太快

问题描述

我希望 Shiny 稍等一下,让用户输入他们的组大小(不使用按钮)。这是我的代码的一个更简单的版本,但在我的实际代码中,我有更多的用户输入(所以我只希望 Shiny 只等待 2 秒等待这个输入)。我一直在试图弄清楚我将如何使用debounce这段代码,但我不确定。

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()
   })
})

标签: rshinyshiny-servershinyappsshiny-reactivity

解决方案


这适用于debounce

  1. 创建一个反应输入函数groupsize
  2. 将此函数传递给以debounce创建一个新函数groupsize_d
  3. 使用这个新功能进行渲染
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()
  })
})

推荐阅读