I have a shiny app where changes to selectInputs trigger both updates to other selects AND trigger a plot update. There are situations, unfortunately, where changing a select causes a plot to re-draw, then it updates another select and the plot draws a second time. So I end up with a plot re-drawing multiple times. My shiny app is more complex than the one below but I've distilled the issue.
I want a change to the plot whenever the user changes a country, year or a title. But when the user changes a country it can also end up updating the year automatically and this can result in an update to the plot associated with country and then a re-draw associated with year.
Is there a way to allow for a brief delay, perhaps, for shiny to "catch up" and not have both reactives trigger the plot? Or perhaps there are other options?
library(shiny)
server <- function(input, output, session) {
output$plot <- renderPlot({
Sys.sleep(0.2)
plot(1:10, main=input$title)
rect(par("usr")[1], par("usr")[3], par("usr")[2], par("usr")[4], col = sample(colors() ,1))
})
observeEvent(input$country, {
vals <- switch(input$country,
US = 2001:2003,
UK = 2001:2005,
FR = 2002:2006)
updateSelectInput(session, "year", choices = vals, selected = vals[1])
})
observeEvent(c(input$country, input$year), {
updateNumericInput(session, "title",
value = paste(input$country, input$year))
})
}
ui <- fluidPage(
tags$div(class="panel-body",
selectInput("country", "country", choices = c("US", "UK", "FR"), selected = "US"),
textInput("title", "Give a title", "This is my initital title"),
selectInput("year", "year", choices = NULL, selected = NULL)
),
plotOutput("plot")
)
shinyApp(ui = ui, server = server)