Flexibility while selecting filters in shiny

Hi,
I have a dataset for making shiny app for monitoring schools. The filters are as follows:

  1. Trainer
  2. Teachers who come under the trainer selected above.
  3. Relevant schools which come under the selected teachers.
  4. Relevant weeks that have been monitored.

But I want flexibility such that I can select the schools after selecting the trainer (basically filter 1 and 3 bypassing the 2nd filter). For example, if I have an output that depends on 1st and 3rd filter, I will select these and see the output. In my observe function, earlier I had used & and now I changed it to |, but still it doesn't work. How can I solve this?

library(shiny)
library(tidyverse)
library(janitor)
#> 
#> Attaching package: 'janitor'
#> The following objects are masked from 'package:stats':
#> 
#>     chisq.test, fisher.test
library(shinydashboard)
#> 
#> Attaching package: 'shinydashboard'
#> The following object is masked from 'package:graphics':
#> 
#>     box
data<-tibble::tribble(
       ~master,    ~teacher,            ~school, ~week,
  "Madhumathi",    "Nithin",     "GHPS KUSUGAL",    3L,
       "Nagma", "Prathibha",       "GHPS UNKAL",    1L,
        "Anil",   "Mrigank",     "GLPS TADAKOD",    1L,
  "Madhumathi",    "Girish",       "GHPS VIDYA",    3L,
       "Nagma",    "Akshay",       "GHPS KALLE",    1L,
        "Anil",    "Ganesh",      "GLPS NOOLVI",    1L,
  "Madhumathi",    "Chintu",    "HPS YARIKOPPA",    2L,
       "Nagma",    "Freddy",    "GHPS SHEREWAD",    2L,
        "Anil",     "Derek",     "GHPS YADAWAD",    3L,
  "Madhumathi",     "Navin", "GHPS KURUBAGATTI",    2L,
       "Nagma",      "Jaya",       "GLPS SULLA",    2L,
        "Anil",     "Obama",    "GLPS BYAHATTI",    2L
  )

ui<-dashboardPage(
  skin = "red",
  dashboardHeader(title = "EarlySpark MIS Dashboard 2022-23",titleWidth = 550),
  dashboardSidebar("Choose your inputs here",
                   selectInput("master","Select the Master Trainer",choices=unique(data$master),multiple=T),
                   selectInput("teacher","Select the Teacher",choices = NULL,multiple = T),
                   selectInput("school","Select the School",choices = NULL,multiple = T),
                   selectInput("week","Select the Week number", choices = NULL,multiple = T)),
  dashboardBody(
    tabsetPanel(
      tabPanel(title = "first"
        
      )
      
    )
  )
)

server<-function(input,output,session){
  
  observe({
    req(input$master)
    
    x<-data %>% 
      filter(master %in% input$master) %>% 
      select(teacher)
    updateSelectInput(session,"teacher","Select the Teacher",choices = c("All",x))
  })
  
  observe({
    req(input$master)
    req(input$teacher)
    
    
    y<-data %>% 
      filter(mastert%in% input$master | teacher %in% input$teacher) %>% 
      select(school)
    updateSelectInput(session,"school","Select the School",choices = c("All",y))
  })
  
  observe({
    req(input$master)
    req(input$teacher)
    req(input$school)
    
    
    
    z<-data %>% 
      filter(mastertraid %in% input$master | 
               en_name %in% input$instructor | 
               schoolid %in% input$school) %>% 
      select(week)
    updateSelectInput(session,"week","Select the Week number",choices = c("All",z))
  })
}

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-09-20 by the reprex package (v2.0.1)

The issue stems from req(input$teacher). By including this within the two observe statements, a value must be selected for teacher in order to execute the code that follows it. Comment out or remove these lines from the two observe statements and the code should work.

Also - I had to rename mastertraid, en_name, and schoolid when creating z because they did not appear in data (changed to master, teacher, and school, respectively).

So is it enough for me to keep just req(input$master) inside the observe function?
Oh I am sorry I forgot to change the variable names..

Since master is needed to determine which schools to show, then it can be left in.

But an add on question here. There are 4 filters here. Can something as the following workflow be made possible?

  1. Select the Master Trainer
  2. I skip the selection of teachers.
  3. Then I go directly to the schools filter, but then it must show only the schools relevant for the selected master trainer.
    Is this possible?

By removing req(input$teacher) from both observe functions, the workflow you described is the result.

Oh yes, this is working as desired. Thanks again.

I am sorry to ask another related question.
In the filter I select the variable name also appears as shown in the screenshot. How can I remove that (I have highlighted in red?

Use pull() in place of select() when creating y.

y<-data %>% 
      filter(master %in% input$master | teacher %in% input$teacher) %>% 
      pull(school)

It works now. Than you very much.

Regards,
Nithin

I have a dataset for making shiny app for monitoring schools. The filters are as follows: Trainer; Teachers who come under the trainer selected

krnt.run
indigocard.ltd

Why did you post this ?

This topic was automatically closed 7 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.