Alex_
Alex_

Reputation: 187

R Shiny Debounce with reactiveValues and observeEvent / R input debounce

I am currently trying in vain to use the debounce function in Shiny to delay my input a bit. The goal is to have the renderText not fire every few milliseconds, but only after 2 second intervals.

I tried to implement the following solution. Thereby I absolutely need the reactiveValues and observeEvent functions. Other solutions here never take this combination into account and I am currently stuck. My example code is shortened. In reality the variable name1$data is still used by different functions and the RenderText accesses different variables.

if (interactive()) {
  
  ui <- fluidPage(
    textInput("IText1", "Input i want to slow down"),
    
    textOutput("OName")
  )
  
  server <- function(input, output, session) {

    Name1 <- reactiveValues()
    observeEvent(input$IText1, {Name1$data <- input$IText1})
    
    #Solutions on stackoverflow
    #Just causes errors for me
    #Name1$t <- debounce(Name1$data, 2000)
    
    output$OName <- renderText({
      Name1$data
    })
    
    
  }
  
  shinyApp(ui, server)
}

Thank you very much for any hint!

Upvotes: 2

Views: 1224

Answers (1)

St&#233;phane Laurent
St&#233;phane Laurent

Reputation: 84709

Normally we debounce reactive conductors (reactive({......})):

ui <- fluidPage(
  textInput("IText1", "Input i want to slow down"),
  
  textOutput("OName")
)

server <- function(input, output, session) {
  
  Name1 <- reactive({
    input$IText1
  })
  
  Name1_d <- debounce(Name1, 2000)
  
  output$OName <- renderText({
    Name1_d()
  })
  
  
}

shinyApp(ui, server)

EDIT

Or you need

server <- function(input, output, session) {
  
  Name1 <- reactiveValues()
  observe({
    invalidateLater(2000, session)
    Name1$data <- isolate(input$IText1)
  })
  
  output$OName <- renderText({
    Name1$data
  })
  
}

Upvotes: 1

Related Questions