Giving the option of "All" in choices of filter and linking the output with it in R shiny

Hi
I am new to shiny. I have made a dashboard here with two plots. I have given a few filters. So when I select a region, an option called "All" comes up, but the plot does not react to it.
How can I resolve it?

library(tidyverse)
library(shiny)
library(janitor)
#> 
#> Attaching package: 'janitor'
#> The following objects are masked from 'package:stats':
#> 
#>     chisq.test, fisher.test
windowsFonts(a=windowsFont("Times New Roman"))

combined_lvl1<-tibble::tribble(
                 ~student_id, ~duration_min, ~enumerator,            ~en_name, ~selectedregionid, ~selectedclusterid,                   ~selectedschoolid,              ~survey_date, ~child_age2, ~total_point_l1, ~total_nr_ratio_l1,
                    "8S5G43",   50.76666667,    "BEN103", "Sarvamangala Godi",   "Dharwad Urban",          "NAVALUR", "SCH251-GMKPS NAVALURU-29090102801", "2022-07-04 00:00:10 UTC",          6L,             40L,        7.352941176,
                    "98UBYO",   31.71666667,    "BEN074",        "Jyoti Godi",   "Dharwad Urban",          "NAVALUR", "SCH251-GMKPS NAVALURU-29090102801", "2022-07-04 00:00:10 UTC",          6L,             61L,        4.411764706,
                    "ON2C1L",          23.1,    "BEN103", "Sarvamangala Godi",   "Dharwad Urban",          "NAVALUR", "SCH251-GMKPS NAVALURU-29090102801", "2022-07-04 00:00:10 UTC",          6L,             17L,        30.88235294,
                    "17OX3D",   24.11666667,    "BEN074",        "Jyoti Godi",   "Dharwad Urban",          "NAVALUR", "SCH251-GMKPS NAVALURU-29090102801", "2022-07-04 00:00:10 UTC",          6L,             55L,        4.411764706,
                    "0FAV2F",   54.01666667,    "BEN083",    "Divya Neelagar",   "Dharwad Urban",          "NAVALUR", "SCH251-GMKPS NAVALURU-29090102801", "2022-07-04 00:00:10 UTC",          6L,             43L,        17.64705882,
                    "KBFAIF",          46.4,    "BEN015",    "Kartik Nippani",  "Hubballi Rural",         "BYAHATTI",  "SCH294-GMPS BYAHATTI-29090700904", "2022-07-04 00:00:10 UTC",          6L,             50L,        1.470588235,
                    "EUY3V4",   25.66666667,    "BEN001",   "Laxman kutaband",  "Hubballi Rural",         "BYAHATTI",  "SCH294-GMPS BYAHATTI-29090700904", "2022-07-04 00:00:10 UTC",          6L,             65L,                  0
                 )


ui<-fluidPage(
  titlePanel(title = "EarlySpark Assessment Dashboard (Age6: Level-1)"),
  sidebarLayout(
    sidebarPanel(
      selectInput("region","Select the region",choices = unique(combined_lvl1$selectedregionid)),
      selectInput("cluster","Select the cluster",choices = NULL),
      selectInput("school","Select the school",choices = NULL),
      selectInput("enumerator","Select the enumerator",choices = NULL),
      selectInput("date","Select the survey date",choices = unique(combined_lvl1$survey_date))
    ),
    mainPanel(
      plotOutput("plot1"),
      plotOutput("plot2")
    )
  )
)

server<-function(input,output,session){
  filtered<-reactive({
    combined_lvl1 %>% 
      filter(selectedregionid==input$region,
             selectedclusterid==input$cluster) %>% 
      if (input$cluster == "All") {
        combined_lvl1
      } else {
        combined_lvl1[combined_lvl1$selectedclusterid == input$cluster, ]
      }
  })
  
  
  observe({
    x<-combined_lvl1 %>% 
      filter(selectedregionid==input$region) %>% 
      select(selectedclusterid)
    updateSelectInput(session,"cluster","Select the cluster",choices = c("All",x))
  })
  
  observe({
    y<-combined_lvl1 %>% 
      filter(selectedregionid==input$region&selectedclusterid==input$cluster) %>% 
      select(selectedschoolid)
    updateSelectInput(session,"school","Select the school",choices = c("All",y))
  })
  
  observe({
    z<-combined_lvl1 %>% 
      filter(selectedregionid==input$region&selectedclusterid==input$cluster&selectedschoolid==input$school) %>% 
      select(en_name)
    updateSelectInput(session,"enumerator","Select the enumerator",choices= c("All",z))
  })  
  
  output$plot1<-renderPlot({
    ggplot(filtered(),aes(total_point_l1,duration_min))+
      geom_point(size=2,color="orange",alpha=0.6)+
      geom_smooth(size=2,color="red",method = "lm",se=F)+
      theme_minimal()+
      labs(title = "Will giving more time to the student improve score?",
           x="Total Score (out of 72)",
           y="Duration (in minutes)")+
      theme(plot.title = element_text(face="bold",hjust=0.5,size=20),
            text = element_text(family="a"),
            axis.title = element_text(face = "bold",size=15))
  })
  
  output$plot2<-renderPlot({
    ggplot(filtered(),aes(total_point_l1,total_nr_ratio_l1))+
      geom_point(size=2.54,color="blue")+
      geom_smooth(color="red",size=2,method="lm",se=F,alpha=0.6)+
      labs(title = "Will giving more time to students reduce No Answer?",
           x="Total Score (out of 91)",
           y="No Response ratio")+
      theme_minimal()+
      theme(plot.title = element_text(face="bold",hjust=0.5,size=20),
            text = element_text(family="a"),
            axis.title = element_text(face = "bold",size=15))
  })
  
}
Created on 2022-08-12 by the reprex package (v2.0.1)

Hello.
Thanks for providing code , but you could take further steps to make it more convenient for other forum users to help you.

Share some representative data that will enable your code to run and show the problematic behaviour.

You might use tools such as the library datapasta, or the base function dput() to share a portion of data in code form, i.e. that can be copied from forum and pasted to R session.

Reprex Guide

Yeah sure. I have edited my question with a sample data.

Ive not run your code yet, but
In principle this approach i highlighted would not be expected to work. Filtering a datamframe results in a dataframe; you then pipe that resultant dataframe into one or another dataframes. This does not make sense. Presumably you want to conditionally apply a filter or not letting more things through. So it seems this is not really a shiny issue. Think of how you might approach this in a simple non shiny script. I might check back later when i have more time, or perhaps someone else will chime in.

Ok I got your point. I am trying to figure out where I made the error. But I am unable to find out. I wanted the filter to show "All" so that the visualization for all clusters will be shown.

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.