Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to use debounce with reactiveValues in shiny

Tags:

r

reactive

shiny

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?

like image 287
Steve Powell Avatar asked Oct 28 '25 14:10

Steve Powell


1 Answers

observe isn't needed:

library(shiny)

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

server <- function(input, output, session) {
  values <- reactiveValues()
  values$t <- debounce(reactive({input$text}), 2000)
  output$text <- renderText({
    values$t()
  })
}

shinyApp(ui, server)

Or without reactiveValues:

library(shiny)

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

server <- function(input, output, session) {
  debouncedText <- debounce(reactive({input$text}), 2000)
  output$text <- renderText({
    debouncedText()
  })
}

shinyApp(ui, server)
like image 81
ismirsehregal Avatar answered Oct 31 '25 05:10

ismirsehregal



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!