1

I have created a checkBoxgroup of all the states in United States for my R shiny dashboard. I have a categorical column State in my database. Is there a way I can pass the entire column in choices as I want all the states in the column as choices in my checkbox. Currently, I am manually putting arguments like this :

checkboxGroupInput("checkGroup", label = h3("Checkbox group"), 
                                   choices = list("AL" ="AL",
                                                  "AR"="AR",
                                                  "AZ"="AZ",
                                                  "CO"="CO",
                                                  "CT"="CT",
                                                  "DC"="DC",
                                                  "DE"="DE",
                                                  "FL"="FL",
                                                  "GA"="GA",
                                                  "HI"="HI",
                                                  "IA"="IA",
                                                  "ID"="ID",
                                                  "IL"="IL",
                                                  "IN"="IN",
                                                  "KS"="KS",
                                                  "KY"="KY",
                                                  "LA"="LA",
                                                  "MA"="MA",
                                                  "MD"="MD",
                                                   .
                                                   .
                                                   .
                                                   .
))

I am looking for a better way to put these choices in the choices argument instead of manually typing each category.

Also when I put this in my UI function, This creates a big Checklist with all the states in one column which results in my app having more than one page. I want to fit it in the space I have maybe with multiple columns.

Attaching a screenshot below for reference as to how it looks right now :

enter image description here

Jeet
  • 188
  • 12
  • in the choices argument you can put the distinct options. `choices = unique(df$state)` – Johan Rosa Oct 07 '19 at 19:40
  • @JohanRosa what about the values? What values will it take? – Jeet Oct 07 '19 at 19:42
  • Consider the answer provide for [this question](https://stackoverflow.com/questions/34530142/drop-down-checkbox-input-in-shiny), with this you can solve your problem. Go to the **EDIT Mar 22 '16** part. – Johan Rosa Oct 07 '19 at 20:02
  • @JohanRosa I used the DropDownbutton function but still every time you open the dropdown, it is big enough so it goes outside the page – Jeet Oct 07 '19 at 21:26
  • In the link there was an specific part that handle this problem. Anyway, I wrote an answer for you. Let me know if it worked for you! – Johan Rosa Oct 08 '19 at 16:09

1 Answers1

2

Well @Jeet, I think that this is a solution for your problem, and is based on the answer that I suggest to you earlier.

In this exmple, lest say I want a shiny app that gives you a table with the name an the abbreviation of the name of the states that you select.

Data

df <- structure(
  list(
    state = c("Alabama", "Alaska", "Arizona", "Arkansas",
              "California", "Colorado", "Connecticut", "Delaware", "Florida",
              "Georgia", "Hawaii", "Idaho", "Illinois", "Indiana", "Iowa",
              "Kansas", "Kentucky", "Louisiana", "Maine", "Maryland", "Massachusetts",
              "Michigan", "Minnesota", "Mississippi", "Missouri", "Montana",
              "Nebraska", "Nevada", "New Hampshire", "New Jersey", "New Mexico",
              "New York", "North Carolina", "North Dakota", "Ohio", "Oklahoma",
              "Oregon", "Pennsylvania", "Rhode Island", "South Carolina", "South Dakota",
              "Tennessee", "Texas", "Utah", "Vermont", "Virginia", "Washington",
              "West Virginia", "Wisconsin", "Wyoming"),
    abr = c("AL", "AK",
            "AZ", "AR", "CA", "CO", "CT", "DE", "FL", "GA", "HI", "ID", "IL",
            "IN", "IA", "KS", "KY", "LA", "ME", "MD", "MA", "MI", "MN", "MS",
            "MO", "MT", "NE", "NV", "NH", "NJ", "NM", "NY", "NC", "ND", "OH",
            "OK", "OR", "PA", "RI", "SC", "SD", "TN", "TX", "UT", "VT", "VA",
            "WA", "WV", "WI", "WY")),
  class = "data.frame",
  row.names = c(NA, -50L)
  )

The approach that I use to end up with a widget that shows all states names abbreviations in three columns and not in a long one

  1. In the UI, I put three checkboxGroupInput inside a dropdownButton
  2. In the server I combined the values selected in these three inputs into one vector, with the reactive function
  3. Then I used the this object to filter the data frame with the states selected

App

library(shiny)
library(shinyWidgets)
library(dplyr)

ui <- fluidPage(
dropdownButton(
  label = "Select states",
  status = "default", width = 450,
  tags$label("Choose :"),
  fluidRow(
    column(
      width = 4,
      checkboxGroupInput(
        inputId = "checka",
        label = NULL,
        choices = df$abr[1:17]
        )
      ),

    column(
      width = 4,
      checkboxGroupInput(
        inputId = "checkb",
        label = NULL,
        choices = df$abr[18:34]
      )
    ),

    column(
      width = 4,
      checkboxGroupInput(
        inputId = "checkc",
        label = NULL,
        choices = df$abr[35:50]
      )
    )

  )
),

tableOutput("table")
)



server <- function(input, output, session) {



abr_selected <- reactive({
  x <- c(input$checka, input$checkb, input$checkc)
})  


  output$table <- renderTable({
    df %>%
      filter(abr %in% abr_selected()) 
  })


}

shinyApp(ui, server)

output

Johan Rosa
  • 2,797
  • 10
  • 18