Abhishek Nigam
Abhishek Nigam

Reputation: 43

Display all the option values using selectInput() in R Shiny

In my case, the options are states and instead of a group of check boxes, I have a selectInput drop down. I am triggering the logic by the means of a single check box in the UI.Now, what I want is, every time I click the box, all my states should be pre-selected in the drop down and the user input is required in case when the check box is not clicked. But unfortunately, irrespective of whether the user has clicked the check-box or not, the output is always what the user selects in the drop down i.e. the default "all states" are not populated as pre-selections.

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                                                          
                          ) 
  }}
}) 

Any help is much appreciated and many thanks in advance.

Upvotes: 3

Views: 3844

Answers (1)

user3652621
user3652621

Reputation: 3634

add a button in your ui.r

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

in your server.r let selectall modify your selector input (here i call it 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())

    }}
})

The mod 2 (%% 2) makes it work every second click to select all, otherwise defaults to second branch where you can preselect whatever you want (or nothing in my case).

Upvotes: 2

Related Questions