How to use debounce with reactiveValues in shiny

送分小仙女□ 提交于 2019-12-08 05:11:24

问题


I understand that I can use debounce with reactive() like this, and this is the sort of behaviour I need, but I want to use reactiveValues() instead.

ui <- fluidPage(
      textInput(inputId = "text",
                label = "To see how quickly..."),
      textOutput(outputId = "text")
)

server <- function(input, output, session) {
      text_input <- reactive({
            input$text
      })

      debounce(text_input, 2000)

      output$text <- renderText({
            text_input()
      })
}
shinyApp(ui, server)
}

But I would prefer to use reactiveValues() rather than reactive(). Is there any way to use debounce with reactiveValues()? This does not work:

ui <- fluidPage(
  textInput(inputId = "text",
            label = "To see how quickly..."),
  textOutput(outputId = "text")
)

server <- function(input, output, session) {

  values <- reactiveValues()


  observe({
    values$text= function(x)input$text

  values$t <-
    debounce(values$text(),2000)

  })


  output$text <- renderText({
    values$t()
  })
}
shinyApp(ui, server)

I get an error Warning: Error in r: could not find function "r", I guess because values is not a reactive expression?


回答1:


Try this. I removed the () after values$text because you want the function/expression, not the resolved values:

ui <- fluidPage(
  textInput(inputId = "text",
            label = "To see how quickly..."),
  textOutput(outputId = "text")
)

server <- function(input, output, session) {

  values <- reactiveValues()

  observe({
    values$text <- function(x){input$text}

    values$t <-
      debounce(values$text,2000)

  })

  output$text <- renderText({
    values$t()
  })
}

shinyApp(ui, server)


来源:https://stackoverflow.com/questions/56296342/how-to-use-debounce-with-reactivevalues-in-shiny

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!