Debounce and throttle are used to slow down a reactive expression. For example, suppose we are using the invalidation check for a reactive expression and error indications are prompted unnecessarily. We can use debounce and throttle to make expressions such as these slow down and wait for intermediate expressions to complete their calculations. The syntaxes of both of these are as follows:
debounce(r, millis, priority = 100, domain = getDefaultReactiveDomain()) throttle(r, millis, priority = 100, domain = getDefaultReactiveDomain())
Here, r is the reactive expression that invalidates too often. millis is the time window used by debounce/throttle, and priority sets the observer's priority. For example, if we want to add debounce to an expression, we can do it as follows:
plot_iris<- plot(iris$Sepal.Length,iris$Sepal.Width) ) %>% debounce(1000)
For more detail visite https://shiny.rstudio.com/reference/shiny/1.0.0/debounce.html. Lets have an example
## 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(iris)
})
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)
}
You will get the following output:
Result table