Display all parameter values ​​using selectInput () in R Shiny

In my case, the options are states, and instead of a group of checkboxes, I have a selectInput. I am running the logic with a single checkbox in the UI. Now I want that every time I click the button all my states should be preselected in the dropdown and user input is required if the checkbox is not checked. But unfortunately, regardless of whether the user has clicked the checkbox or not, the output is always user-selectable in the dropdown, that is, "all states" are not populated as preselects by default.

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 greatly appreciated and thanks a lot in advance.

+3


source to share


1 answer


add button to your ui.r

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

      

on your server. r let's selectall

change 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())

    }}
})

      

Mod 2 ( %% 2

) makes it work every second click to select everything, otherwise it defaults to the second branch where you can select whatever you want (or nothing in my case).

+2


source







All Articles