Filtering on the basis of option selected in the previous filter in Shiny

Hi. I am new to Shiny. I have a data where 3 filters are to be made- Region, cluster and schools. When I select a particular region, the relevant clusters must only appear and when i select the cluster, only the relevant schools must appear. Thank you in advance.

library(tidyverse)
library(shiny)
library(shinydashboard)
#> 
#> Attaching package: 'shinydashboard'
#> The following object is masked from 'package:graphics':
#> 
#>     box
data<-tibble::tribble(
           ~region,             ~cluster,                                       ~school,
   "Hubballi City",    "BAIRIDEVARKOPPA",    "GLPS Sadashivanand Nagar BAIRIDEVARKOPPA",
   "Dharwad Rural",           "NARENDRA",       "GHPS KANNADA GIRLS NARENDRA, NARENDRA",
   "Dharwad Rural",           "NARENDRA",                   "GHPS MULAMUTTAL, NARENDRA",
   "Hubballi City",        "GOPANAKOPPA",   "KARNATAKA PUBLIC SCHOOLS GMPS GOPANAKOPPA",
   "Hubballi City",        "GOPANAKOPPA",                                "GHPS BENGERI",
   "Hubballi City",        "GOPANAKOPPA",   "KARNATAKA PUBLIC SCHOOLS GMPS GOPANAKOPPA",
   "Hubballi City",        "GOPANAKOPPA",   "KARNATAKA PUBLIC SCHOOLS GMPS GOPANAKOPPA",
   "Hubballi City",           "INDIPUMP",                   "GMKBGS NO.01 OLD HUBBALLI",
  "Hubballi Rural",             "NOOLVI",                       "GHPKBS NOOLVI, NOOLVI",
   "Dharwad Rural",           "NARENDRA",                  "GHPS KURUBAGATTI, NARENDRA",
   "Hubballi City",    "BAIRIDEVARKOPPA",    "GLPS Sadashivanand Nagar BAIRIDEVARKOPPA",
   "Hubballi City",    "BAIRIDEVARKOPPA",    "GLPS Sadashivanand Nagar BAIRIDEVARKOPPA",
   "Hubballi City",              "GOKUL", "GHPS MANJUNATH NAGAR HUBLI, ANAND NAGAR HBL",
  "Hubballi Rural",             "NOOLVI",                       "GHPKBS NOOLVI, NOOLVI",
   "Dharwad Rural",           "NARENDRA",       "GHPS KANNADA GIRLS NARENDRA, NARENDRA",
  "Hubballi Rural",            "KUNDGOL",                           "GMPS, ADARAGUNCHI",
    "Dharwad City",          "MALAMADDI",          "LPS LAKSHMI SINGANAKERI, MALAMADDI",
   "Hubballi City",          "NAVANAGAR",                                "GMGS AMARGOL",
  "Hubballi Rural",             "NOOLVI",                  "GLPS SHEREWAD PLOT, NOOLVI",
  "Hubballi Rural",             "NOOLVI",                       "GHPKBS NOOLVI, NOOLVI",
  "Hubballi Rural",             "NOOLVI",                       "GHPKBS NOOLVI, NOOLVI",
    "Dharwad City", "GANDHI CHWOKA, DWD",              "GHPS NO.8 GANDHI CHOUK DHARWAD",
   "Dharwad Rural",       "DEVARHUBBALI",         "GHPS MANSUR DICE NUMBER-29090108001",
  "Hubballi Rural",             "RAYNAL", "GOVERNMENT HIGHER PRIMARY SCHOOL Channapura"
  )
ui<-dashboardPage(
  skin="red",
  dashboardHeader(title = "EarlySpark Assessments Dashboard for Level-1 (Age 6)",titleWidth = 600),
  dashboardSidebar("Select the Inputs here", width = 300,
                   selectInput("region","Select the region",choices = sort(unique(data$region)),selected = "Hubballi Rural"),
                   selectInput("cluster","Select the cluster",choices = sort(unique(data$cluster)),multiple = T),
                   selectInput("school","select the school name",choices = sort(unique(data$school)),multiple = T)),
                   
  dashboardBody(
    tabsetPanel(type="tabs",
                id="tab_selected",
                tabPanel(title="Dashboard for Level-1"))
  )
)


server<-function(input,output){}

shinyApp(ui,server)
#> PhantomJS not found. You can install it with webshot::install_phantomjs(). If it is installed, please make sure the phantomjs executable can be found via the PATH variable.
Shiny applications not supported in static R Markdown documents
Created on 2022-03-29 by the reprex package (v2.0.1)

At this point you have only prepared the frontend. What you need to do now is to write the background part.

To display your data frame in Shiny you can use for example DT package (DT: An R interface to the DataTables library).

In server function you have to add reference to your inputs.

  1. Create an output and assign it a render function. There are different render functions for table, plot, text. Because I recommended you DT package your render function is renderDataTable()

  2. Complete the function that has to generate the table with the code that takes into account your choices

  3. Don't forget to make sure that your inputs cannot be empty. The user must select one of the options so that you can filter the data. This is one way to avoid an error. For example req(input$region)

  4. Create output in the front end. In your case, the output will be dataTableOutput("enter a name from output$name to which you will assign the render function")

It's hard to help you without giving you the finished code, but if I gave you the full solution you probably wouldn't learn it. Try it yourself. Look for a diagram of how to do these things on the internet. You will learn more that way.

This topic was automatically closed 21 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.