问题描述
我希望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()
})
})
解决方法
这与debounce
一起使用:
- 创建反应式输入函数
groupsize
- 将此函数传递给
debounce
以创建新函数groupsize_d
- 使用此新功能进行渲染
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()
})
})