Jeet
Jeet

Reputation: 188

Checkboxgroup R Shiny categories split across columns instead of just 1 row

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

Upvotes: 1

Views: 2369

Answers (1)

Johan Rosa
Johan Rosa

Reputation: 3152

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

Upvotes: 2

Related Questions