简体   繁体   中英

In Shiny I want to updateSelectInput() only once during UI load

I have a drop-down (SelectInput) that I want to update only once and load it up with a list of items programmatically during the upload of UI. I was putting it in a Render function but the problem is it resets again and again.

selectInput has parameters that allows you to set the initial state. Among these parameters, you can use choices for providing options and selected for having defaults. Please run ?shiny::selectInput for additional details.

Rendering it in server side or preferably using updateSelectInput would be of help if you want to update it upon user interaction in a reactive context.

Here is a minimal example:

library(shiny)

ui <- fluidPage(
  selectInput(
    inputId = "digits_input", 
    label = "Digits:", 
    choices = 0:9
    ## other arguments with default values:
    # selected = NULL,
    # multiple = FALSE,
    # selectize = TRUE, 
    # width = NULL, 
    # size = NULL
  ),

  selectInput(
    inputId = "letters_input", 
    label = "Lower case letters:", 
    choices = letters,
    selected = c("a", "b", "c"), # initially selected items 
    multiple = T # to be able to select multiple items
  ),

  actionButton(
    inputId = "update",
    label = "Capitalize"
  )

)

server <- function(session, input, output) {
  observeEvent(input$update, {
    updateSelectInput(
      session,
      inputId = "letters_input",
      label = "Upper case letters:",
      choices = LETTERS,
      selected = c("A", "B", "C")
    )
  })
}

shinyApp(ui = ui, server = server)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM