I came across a situation where a reactive expression is evaluated multiple times when it calls some inputs within renderUI widgets.
Please check the following simple code.
library(shiny)
server <- function(input, output) {
output$INPUT_1 = renderUI({
selectInput("input_1","Input 1",choices = letters)
})
output$INPUT_2 = renderUI({
selectInput("input_2","Input 2",choices = letters)
})
output$INPUT_3 = renderUI({
selectInput("input_3","Input 3",choices = letters)
})
output$INPUT_4 = renderUI({
selectInput("input_4","Input 4",choices = letters)
})
output$text = renderText({
print("1")
paste(input$input_1,input$input_2,input$input_3,input$input_4)
})
}
ui <- fluidPage(
uiOutput("INPUT_1"),
uiOutput("INPUT_2"),
uiOutput("INPUT_3"),
uiOutput("INPUT_4"),
textOutput("text")
)
shinyApp(ui = ui, server = server)
If you take a look at your console, you'll see
Listening on http://127.0.0.1:4939
[1] "1"
[1] "1"
The "1" came twice.
My own app has the "1" coming more than 3 times. Since each of my reactive deals with big data, this kind of scenario really embarrasses my users and myself.
I'm almost sure that this has something to do with the renderUI facility. But I still cannot find a way to fix it. I considered using updateXXXXX features but my input UIs contain very complex calculations. So using updateXXXXX is something I am trying to avoid.
How can the my reactive expression be evaluated only once?