使用R Shiny中的selectInput()显示所有选项值

时间:2015-06-19 13:29:30

标签: r shiny

在我的情况下,选项是状态,而不是一组复选框,我有一个selectInput下拉列表。我通过UI中的单个复选框触发逻辑。现在,我想要的是,每次单击该框时,我都应该在下拉列表中预先选择所有状态,并且需要用户输入未单击复选框的情况。 但不幸的是,无论用户是否点击了复选框,输出始终是用户在下拉列表中选择的内容,即默认的“所有状态”不会作为预选择填充。

Server.R -  
    observe({
    if(input$national>0)
    {if (input$national %% 2 == 0){
        updateSelectInput(session,
                        "State",label = h4("Select any state"),                               
                        choices = list("NSW" = "NSW","Victoria" = "Victoria","SA" = "SA","Tasmania" = "Tasmania"),                                                                                                                                 
                        selected = c("NSW","Victoria","SA","Tasmania"),multiple = TRUE                                                          
                        )}
      else 
      {updateSelectInput(session,
                        "State",                                
                        label = h4("Select any state"),                               
                        choices = list("NSW" = "NSW","Victoria" = "Victoria","SA" = "SA","Tasmania" = "Tasmania"),                                                                                                                                 
                        selected = c(),multiple = TRUE                                                          
                          ) 
  }}
}) 

非常感谢任何帮助,并提前多多感谢。

1 个答案:

答案 0 :(得分:2)

在你的ui.r中添加一个按钮

actionButton("selectall", label="Select/Deselect all")

在你的服务器中。让selectall修改你的选择器输入(这里我称之为show_vars)。

 observe({
  if (input$selectall > 0) {
    if (input$selectall %% 2 == 0){
      updateCheckboxGroupInput(session=session, 
                               inputId="show_vars",
                               choices = list("carat" = "carat",
                                              "cut" = "cut",
                                              "color" = "color",
                                              "clarity"= "clarity",
                                              "depth" = "depth",
                                              "table" = "table",
                                              "price" = "price",
                                              "x" = "x",
                                              "y" = "y",
                                              "z" = "z"),
                               selected = c(names(diamonds)))

    } else {
      updateCheckboxGroupInput(session=session, 
                               inputId="show_vars",
                               choices = list("carat" = "carat",
                                              "cut" = "cut",
                                              "color" = "color",
                                              "clarity"= "clarity",
                                              "depth" = "depth",
                                              "table" = "table",
                                              "price" = "price",
                                              "x" = "x",
                                              "y" = "y",
                                              "z" = "z"),
                               selected = c())

    }}
})

mod 2(%% 2)使每次第二次点击都可以选择全部,否则默认为第二个分支,你可以预先选择你想要的任何东西(或者在我的情况下没有任何内容)。