Created
March 27, 2022 09:10
-
-
Save hypebright/188e3f39ff80241ed6cc458a35a06627 to your computer and use it in GitHub Desktop.
Gist with an example demonstrating the usefulness of debouncing and throttling of a reactive expression in Shiny
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
library(shiny) | |
library(magrittr) | |
counts <- | |
reactiveValues(baseline = 0, | |
debounce = 0, | |
throttle = 0) | |
ui <- fluidPage( | |
titlePanel("Slowing down a chatty reactive expression 💬🦥 - demo"), | |
br(), | |
fluidRow( | |
column(5, | |
sliderInput('slider_baseline', 'Without debounce/throttle', min = 1, max = 10, value = 1)), | |
column(4, | |
textOutput('baseline_example')), | |
column(3, | |
textOutput('baseline_example_count')) | |
), | |
fluidRow( | |
column(5, | |
sliderInput('slider_debounce', 'With debounce', min = 1, max = 10, value = 1)), | |
column(4, | |
textOutput('debounce_example')), | |
column(3, | |
textOutput('debounce_example_count')) | |
), | |
fluidRow( | |
column(5, | |
sliderInput('slider_throttle', 'With throttle', min = 1, max = 10, value = 1)), | |
column(4, | |
textOutput('throttle_example')), | |
column(3, | |
textOutput('throttle_example_count')) | |
) | |
) | |
server <- function(input, output, session) { | |
## Baseline example | |
baseline <- reactive({ | |
input$slider_baseline | |
}) | |
observeEvent(baseline(), { | |
invalidated_value <- baseline() | |
counts$baseline <- counts$baseline + 1 | |
output$baseline_example <- renderText(paste0('Invalidated at ', Sys.time(), ' with value ', invalidated_value)) | |
output$baseline_example_count <- renderText(paste0('Times invalidated: ', counts$baseline)) | |
}) | |
# Debounce example | |
debounce_example <- reactive({ | |
input$slider_debounce | |
}) | |
debounce_example_d <- debounce_example %>% debounce(millis = 2000) | |
observeEvent(debounce_example_d(), { | |
invalidated_value <- debounce_example_d() | |
counts$debounce <- counts$debounce + 1 | |
output$debounce_example <- renderText(paste0('Invalidated at ', Sys.time(), ' with value ', invalidated_value)) | |
output$debounce_example_count <- renderText(paste0('Times invalidated: ', counts$debounce)) | |
}) | |
# Throttle example | |
throttle_example <- reactive({ | |
input$slider_throttle | |
}) | |
throttle_example_d <- throttle_example %>% throttle(millis = 2000) | |
observeEvent(throttle_example_d(), { | |
invalidated_value <- throttle_example_d() | |
counts$throttle <- counts$throttle + 1 | |
output$throttle_example <- renderText(paste0('Invalidated at ', Sys.time(), ' with value ', invalidated_value)) | |
output$throttle_example_count <- renderText(paste0('Times invalidated: ', counts$throttle)) | |
}) | |
} | |
shinyApp(ui = ui, server = server) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment