在Shiny中,我只想在UI加载期间一次updateSelectInput()

时间:2018-07-29 06:20:08

标签: r shiny-server

我有一个下拉列表(SelectInput),我只想更新一次,并在上传UI时以编程方式将其加载到项目列表中。我把它放在Render函数中,但问题是它一次又一次地重置。

1 个答案:

答案 0 :(得分:0)

selectInput具有允许您设置初始状态的参数。在这些参数中,您可以使用choices提供选项,并使用selected提供默认值。请运行?shiny::selectInput了解更多详细信息。

如果要在反应性上下文中用户交互时对其进行更新,则在server端或最好使用updateSelectInput进行渲染会有所帮助。

这是一个最小的示例:

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)