Richard J. Acton
Richard J. Acton

Reputation: 915

How to access values from dynamically generated UI elements that are not initially visible

If you run this app 'a' the default selected value does not appear until the UI tab is selected and the UI element which populates 'input$select' is generated. How can I force this element to be created when the app is loaded without the need to click on the panel to initialize it in order to get access to its default value.

library(shiny)
library(shinydashboard)

ui <- fluidPage(
    tabsetPanel(
        tabPanel(
            title = "landing",
            "Stuff"
        ),
        tabPanel(
            title = "UI",
            uiOutput("select")
        )
    ),
    textOutput("out")
)

server <- function(input, output, session) {
    output$select <- renderUI(
        selectInput(
            "select", "Selector:", choices = c("a", "b"), selected = "a"
        )
    )
    
    output$out <- renderText(input$select)
}

shinyApp(ui, server)

Upvotes: 1

Views: 74

Answers (1)

starja
starja

Reputation: 10365

You can use the argument suspendWhenHidden = FALSE from outputOptions. I had to play a bit where to place outputOptions (it doesn't work at the beginning of the server function). However, it still needs a little bit of time to load, so maybe one could optimise it further.

library(shiny)
library(shinydashboard)

ui <- fluidPage(
  tabsetPanel(
    tabPanel(
      title = "landing",
      "Stuff"
    ),
    tabPanel(
      title = "UI",
      uiOutput("select")
    )
  ),
  textOutput("out")
)

server <- function(input, output, session) {
  
  output$select <- renderUI({
    selectInput(
      "select", "Selector:", choices = c("a", "b"), selected = "a"
    )
  })
  
  output$out <- renderText(input$select)
  outputOptions(output, "select", suspendWhenHidden = FALSE)
}

shinyApp(ui, server)

Upvotes: 1

Related Questions