How to Create Cascading Filters in R Shiny Dashboard by TopBullets.com

Topbullets.comIn Data Science visualization is a powerful tool. You can’t sell a concept but a product. Data analytics is science but presenting them through different graphs and charts is an art. We have many ways to present the numbers and a lot of software which make our life easy. Few of them which I have tried are Tableau, R Shiny, D3, QlikView and VBA Dashboard. Every tool has its own advantages and properties. Currently, I am working on R Shiny dashboard which is so beautiful and easy to learn. I would really encourage you to learn R Shiny if you do coding in R. It has a lot of in-built packages which can easily be deployed. Today let’s work on cascading filters one of the most important kinds of filters. Every other filter is connected to the predecessor filter.
I am using this mock data. Please download and have a look. We have 3 filters here: Continent, Country, and State. If I select Asia only Asian countries should come in the second filter and if I select India as country only Indian states should appear on the third filter.
If you want a shortcut you can directly use shinyFilters. Detail documentation is provided here.
But here you don’t have control to present the data. You get the filtered data in few clicks but which columns you want to show or if you want to process the data further you have very fewer options. So I have written my own code to make cascading filters after spending few hours playing around shinyFilters and DT packages.Cascading

  1. Import the data
  2. Show ‘Continent’ from the data
  3. Subset the data further for selected ‘Continent’
  4. Show ‘Country’ from the subset data
  5. Subset the subset data for selected ‘Country’
  6. Show ‘State’ from the subset data
  7. Subset the above data for selected ‘State’

Cascading_2

ui.r

########################################################################
setwd("......\\R Codes")

header <- dashboardHeader(
  title = "TopBullets.com Dashboard",
  dropdownMenu(type = "notifications",
               notificationItem(
                 text = "Designed by Deepesh Singh",
                 icon("cog", lib = "glyphicon")
               )
  )
...........

server.r

options(shiny.maxRequestSize = 15*1024^2)
shinyServer(function(input,output){

  # Importing data and save it temporarily in data variable
  data <- reactive({
    read.table(file = "......\\Countries-Continents-csv.csv",
               sep = ",", header = T,
               stringsAsFactors = F)
  })

  output$state <- renderUI({
    selectInput(inputId = "State", "Select State",choices = var_state(),selected = "Goa")
  })

..........
############################ CODE ENDS HERE ###########################################

You can download the codes here. Download

Signature

Deepesh Singh
logo

Advertisement

4 thoughts on “How to Create Cascading Filters in R Shiny Dashboard by TopBullets.com

  1. Hi Deepesh. Many thanks for sharing this example – it has helped me better understand as I get through the Shiny learning curve! Also, thank you for referencing shinyFilters, which unfortunately I could not install because of an error related to a file called Rprofile? All the best.

Please leave your valuable comment.

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s