Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can I let Shiny wait for a longer time for numericInput before updating?

Tags:

r

shiny

In my Shiny App, there are a few numericInput and selectInput.

Shiny updates outputs during typing, especially when users type is slower in the numericInput.

sumbitButton could you be used to stop automatically updading. But I prefer to not to use it.

How could I let Shiny waits for a longer time for numericInput?

Thanks for any suggestion. Let me know if my question is not clear.

like image 794
Bangyou Avatar asked Jan 22 '16 01:01

Bangyou


1 Answers

You can use debounce on the reactive function that uses your Inputs. Setting it to 2000 milliseconds felt OK to me. If you use the input directly in a render function you might need to create the data to use in your render function in a reactive function.

An example is here: https://shiny.rstudio.com/reference/shiny/latest/debounce.html

## Only run examples in interactive R sessions
if (interactive()) {
options(device.ask.default = FALSE)

library(shiny)
library(magrittr)

ui <- fluidPage(
  plotOutput("plot", click = clickOpts("hover")),
  helpText("Quickly click on the plot above, while watching the result table below:"),
  tableOutput("result")
)

server <- function(input, output, session) {
  hover <- reactive({
    if (is.null(input$hover))
      list(x = NA, y = NA)
    else
      input$hover
  })
  hover_d <- hover %>% debounce(1000)
  hover_t <- hover %>% throttle(1000)

  output$plot <- renderPlot({
    plot(cars)
  })

  output$result <- renderTable({
    data.frame(
      mode = c("raw", "throttle", "debounce"),
      x = c(hover()$x, hover_t()$x, hover_d()$x),
      y = c(hover()$y, hover_t()$y, hover_d()$y)
    )
  })
}

shinyApp(ui, server)
}
like image 196
Jan Stanstrup Avatar answered Sep 28 '22 15:09

Jan Stanstrup