16.4 Create infinite loops
- Pausing animations
In this example we use running reactive value and invalidateLater() function. This is the case when we can’t use observeEvent() but we need to used observe() and isolate() otherwise an infinite loop is created.
ui <- fluidPage(
actionButton("start", "start"),
actionButton("stop", "stop"),
textOutput("n")
)
server <- function(input, output, session) {
r <- reactiveValues(running = FALSE, n = 0)
observeEvent(input$start, {
r$running <- TRUE
})
observeEvent(input$stop, {
r$running <- FALSE
})
# we cannot use observeEvent() but observe() and isolate()
observe({
if (r$running) {
r$n <- isolate(r$n) + 1
invalidateLater(250)
}
})
output$n <- renderText(r$n)
}
# shinyApp(ui = ui, server = server)