1

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)

1 Answer 1

1

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)
Sign up to request clarification or add additional context in comments.

1 Comment

I did not know about the outputOptions() function that seems to solve the problem in my actual use case as well as this toy example thanks!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.