When an input value is created using renderUI, the initial reactive graph completes as if the input value is NULL even if a default value is supplied. This results in all outputs dependent on the input value calculating twice at startup. Example below:
library(dplyr)
library(shiny)
ui <- fluidPage(
uiOutput("iris_filter"),
dataTableOutput('table')
)
server <- function(input, output, session) {
output$iris_filter <- renderUI({
selectInput(inputId = "species_filter",
label = "Species: ",
choices = iris %>% distinct(as.character(Species)) %>% pull(),
multiple = T,
selected = 'setosa')
})
output$table <- renderDataTable({
iris %>%
filter(Species %in% input$species_filter)
})
}
options(shiny.reactlog = T)
shinyApp(ui, server)
Tracing through the reactive graph in this example shows the following sequence:
- renderUI completes
- input$species_filter evalutes to NULL
- output$table renders
- input$species_filter updates to 'setosa'
- output$table renders
Our goal is to prevent output$table from rendering twice at startup, what is the best way to do this? Is there a way to force renderUI to set the right default value during init?