Pie plot doesn't render for some input variable combos

Hi, I have created a dashboard which renders a pie plot in an Age Group tab. A data table is also rendered into the Table tab. The data table renders perfectly for any combination of widgets checked, however, the pie plot doesn't render for all combinations of widgets. They fail on certain combinations and I get the error message,

Error in [.data.frame: undefined columns selected.

I have four check box widgets, month, year, status and officer. If I select say, 2019-01 in Month, then 2018 in Year, then acc in Status, all plots render perfectly including the data table as I check each widget. If I then select, Officer A, the table renders perfectly but I get the error message above in the plot tab.
If I clear all selections then start again by checking 2018 in Year, acc in Status and Officer A I will get a warning message

Warning: Factor dfnew[[grp_vars]] contains implicit NA, consider using forcats::fct_explicit_na
Warning: Removed 1 rows containing missing values (position_stack).

My fully reproduceable code is below. Could someone help solve this problem so all pies render with any combo of check box widget selected please? Much appreciated.

library(shiny)

shinyUI(navbarPage("Example",
                   
    
                   tabPanel("Data",
                            sidebarLayout(
                              sidebarPanel(
                                "Nothing here at the moment"),
                              mainPanel("Select Dashboard Panel for results.Click on Select/All to make the plots 
          	           render"))
                   ),
                   
         tabPanel("Dashboard",
         sidebarLayout(
          sidebarPanel(
              checkboxInput('all', 'Select All/None', value = TRUE),
              uiOutput("year_month"),
              tags$head(tags$style("#year_month{color:red; font-size:12px; font-style:italic; 
              overflow-y:scroll; max-height: 100px; background: ghostwhite;}")),
              
              checkboxInput('all1', 'Select All/None', value = TRUE),
              uiOutput("year"),
              tags$head(tags$style("#year{color:red; font-size:12px; font-style:italic; 
              overflow-y:scroll; max-height: 100px; background: ghostwhite;}")),
              
              checkboxInput('all2', 'Select All/None', value = TRUE),
              uiOutput("status"),
              tags$head(tags$style("#status{color:red; font-size:12px; font-style:italic; 
              overflow-y:scroll; max-height: 100px; background: ghostwhite;}")),
              
              checkboxInput('all3', 'Select All/None', value = TRUE),
              uiOutput("officer"),
              tags$head(tags$style("#officer{color:red; font-size:12px; font-style:italic; 
              overflow-y:scroll; max-height: 100px; background: ghostwhite;}"))
              
              #radioButtons("var3", "Select the file type", choices=c("png", "pdf")),
              #downloadButton("down", "Download the plot")
            ),
          mainPanel( 
                uiOutput("tb")))
                   )
))    
library(shiny)
library(ggplot2)
library(dplyr)
library(gridExtra)
#use the below if you want to increase the file size being inputed to 9MB
#options(shiny.maxRequestSize = 9.1024^2)

complaint_id <- 
  c(1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,26,27,28,29,30,
    31,32,33,34,35,36,37,38,39,40,41,42,43,44,45,46,47,48,49,50,51,52,53,54,55,56,57,58,59,60,
    61,62,63,64,65,66)

age_group <- c("Over a year", "06 Months", "01 Months", "Over a year", "06 Months", "09 Months",
               "01 Months", "03 Months", "06 Months", "03 Months", "12 Months", "09 Months",
               "01 Months", "06 Months", "01 Months", "12 Months", "01 Months", "09 Months",
               "06 Months", "09 Months", "Over a year", "Over a year", "01 Months", "12 Months",
               "06 Months", "01 Months", "09 Months", "12 Months", "03 Months", "01 Months",
               "Over a year", "01 Months", "01 Months",
               "Over a year", "06 Months", "01 Months", "Over a year", "06 Months", "09 Months",
               "01 Months", "03 Months", "06 Months", "03 Months", "12 Months", "09 Months",
               "01 Months", "06 Months", "01 Months", "12 Months", "01 Months", "09 Months",
               "06 Months", "09 Months", "Over a year", "Over a year", "01 Months", "12 Months",
               "06 Months", "01 Months", "09 Months", "12 Months", "03 Months", "01 Months",
               "Over a year", "01 Months", "01 Months")

closed_fy_ending <- c("2019", "2019", "2019", "2019", "2019", "2019", "2019", "2019", "2019", "2019",
		                  "2019", "2019", "2019", "2019", "2019", "2019", "2019", "2019", "2019", "2019",
		                  "2019", "2019", "2019", "2019", "2019", "2019", "2019", "2019","2019", "2019",
                 		  "2019", "2019", "2019",
		                  "2018", "2018", "2018", "2018", "2018", "2018", "2018", "2018", "2018", "2018",
		                  "2018", "2018", "2018", "2018", "2018", "2018", "2018", "2018", "2018", "2018",
		                  "2018", "2018", "2018", "2018", "2018", "2018", "2018", "2018", "2018", "2018",
		                  "2018", "2018", "2018")

closed_date_ym <- c("2019-01", "2019-02", "2019-01", "2019-01", "2019-01", "2019-01", "2019-02",
                    "2019-01", "2019-01", "2019-02", "2019-02", "2019-02", "2019-01", "2019-01",
                    "2019-02", "2019-02", "2019-01", "2019-02", "2019-02", "2019-02", "2019-02",
                    "2019-02", "2019-02", "2019-02", "2019-01", "2019-01", "2019-02", "2019-01",
                    "2019-01", "2019-01", "2019-01", "2019-02", "2019-02",
                    "2018-11", "2018-12", "2018-11", "2018-11", "2018-11", "2018-11", "2018-12",
                    "2018-11", "2018-11", "2018-12", "2018-12", "2018-12", "2018-11", "2018-11",
                    "2018-12", "2018-12", "2018-11", "2018-12", "2018-12", "2018-12", "2018-12",
                    "2018-12", "2018-12", "2018-12", "2018-11", "2018-11", "2018-12", "2018-11",
                    "2018-11", "2018-11", "2018-11", "2018-12", "2018-12")
                    
officer <- c("E", "D", "B", "A", "A", "D", "C", "C", "C", "D", "C", "B", "C", "D", "A", "A", "D",
             "A", "E", "C", "B", "C", "E", "E", "E", "A", "A", "A", "B", "E", "C", "D", "B",
             "E", "D", "B", "A", "A", "D", "C", "C", "C", "D", "C", "B", "C", "D", "A", "A", "D",
             "A", "E", "C", "B", "C", "E", "E", "E", "A", "A", "A", "B", "E", "C", "D", "B")

status <- c("FT","FT","acc","acc", "acc", "FT", "FT", "FT", "FT", "FT", "FT", "acc", "FT", "FT", "acc",
            "acc", "FT", "acc", "FT", "FT", "acc", "FT", "FT", "FT", "FT", "acc","acc", "acc", "acc",
            "FT", "FT", "FT", "acc",
            "FT","FT","acc","acc", "acc", "FT", "FT", "FT", "FT", "FT", "FT", "acc", "FT", "FT", "acc",
            "acc", "FT", "acc", "FT", "FT", "acc", "FT", "FT", "FT", "FT", "acc","acc", "acc", "acc",
            "FT", "FT", "FT", "acc")

Outcome <- c("Excellent", "Poor", "OK", "Excellent", "Poor", "Good", "Poor", "Good", "Poor", 
             "Excellent","Poor", "Good", "Excellent", "Good", "Poor", "Poor", "Excellent", "Poor", 
             "Poor", "Good", "OK", "OK", "Excellent", "Poor", "Good", "OK", "Good", "OK", "Good", 
             "Excellent", "Excellent", "Excellent", "Excellent",
             "Excellent", "Poor", "OK", "Excellent", "Poor", "Good", "Poor", "Good", "Poor", 
             "Excellent","Poor", "Good", "Excellent", "Good", "Poor", "Poor", "Excellent", "Poor", 
             "Poor", "Good", "OK", "OK", "Excellent", "Poor", "Good", "OK", "Good", "OK", "Good", 
             "Excellent", "Excellent", "Excellent", "Excellent")

sample_data <- data.frame(complaint_id, age_group, closed_fy_ending, closed_date_ym, officer, 
                          status, Outcome)

shinyServer(function(session, input, output){
  
  #This reactive function takes the inputs from ui.r and use them for read.table()
  #file$datapath -> gives the path of the file
  data <- reactive({
      sample_data 
   })

  # Have to modify the reactive data object to add a column of 1s(Ones) inorder
  # that the Pie chart %s are calculated correctly within the segments. We apply
  # this modification to a new reactive object, data_mod()
  data_mod <- reactive({
    if(is.null(data()))return()
    req(data())
    data_mod <-
      data() %>% select(complaint_id, age_group, closed_fy_ending, closed_date_ym, officer, 
                        status, Outcome)
    data_mod$Ones <- rep(1, nrow(data()))
    data_mod
  })


  # creates a selectInput widget with unique YYYY-MM variables ordered from most
  # recent to oldest time period

  output$year_month <- renderUI({
    if(is.null(data()))return()
    req(data_mod())
    data_ordered <-
      order(data_mod()$closed_date_ym, decreasing = TRUE)
    data_ordered <- data_mod()[data_ordered,]
    checkboxGroupInput("variable_month",
                       "Select Month",
                       choices = unique(data_ordered$closed_date_ym))
  })

  # creates a selectInput widget with unique YYYY variables ordered from most
  # recent to oldest time period
   
    output$year <- renderUI({
      if(is.null(data()))return()
      req(data_mod())
      data_ordered <-
        order(data_mod()$closed_fy_ending, decreasing = TRUE)
      data_ordered <- data_mod()[data_ordered,]
      checkboxGroupInput("variable_year",
                         "Select Year",
                         choices = unique(data_ordered$closed_fy_ending))  
    })
    
    # creates a selectInput widget for status variables ordered alphabetically
    output$status <- renderUI({
      if(is.null(data()))return()
      req(data_mod())
      data_ordered <-
        order(data_mod()$status, decreasing = FALSE)
      data_ordered <- data_mod()[data_ordered,]
      checkboxGroupInput("variable_status",
                         "Select status",
                         choices = unique(data_ordered$status)) 
    })
      
      # creates a selectInput widget for officer variables ordered alphabetically
      output$officer <- renderUI({
        if(is.null(data()))return()
        req(data_mod())
        data_ordered <-
          order(data_mod()$officer, decreasing = FALSE)
        data_ordered <- data_mod()[data_ordered,]
        checkboxGroupInput("variable_officer",
                           "Select Officer",
                           choices = unique(data_ordered$officer))      

  })
      
#Observe function for the month tick box widget
  observe({
    if(is.null(data()))return()
    req(data_mod())
    data_ordered <-
      order(data_mod()$closed_date_ym, decreasing = TRUE)
    data_ordered <- data_mod()[data_ordered,]
    updateCheckboxGroupInput(
      session,
      "variable_month",
      choices = unique(data_ordered$closed_date_ym),
      selected = if (input$all)
        unique(data_ordered$closed_date_ym)
    )
   
  })
  
  #Observe function for the year tick box widget  
    observe({
      if(is.null(data()))return()
      req(data_mod())
      data_ordered <-
        order(data_mod()$closed_fy_ending, decreasing = TRUE)
      data_ordered <- data_mod()[data_ordered,]
      updateCheckboxGroupInput(
        session,
        "variable_year",
        choices = unique(data_ordered$closed_fy_ending),
        selected = if (input$all1)
          unique(data_ordered$closed_fy_ending)
      )  
  })
    #Observe function for the status tick box widget  
    observe({
      if(is.null(data()))return()
      req(data_mod())
      data_ordered <-
        order(data_mod()$status, decreasing = FALSE)
      data_ordered <- data_mod()[data_ordered,]
      updateCheckboxGroupInput(
        session,
        "variable_status",
        choices = unique(data_ordered$status),
        selected = if (input$all2)
          unique(data_ordered$status)
      )  
    })
    
    #Observe function for the officer tick box widget  
    observe({
      if(is.null(data()))return()
      req(data_mod())
      data_ordered <-
        order(data_mod()$officer, decreasing = FALSE)
      data_ordered <- data_mod()[data_ordered,]
      updateCheckboxGroupInput(
        session,
        "variable_officer",
        choices = unique(data_ordered$officer),
        selected = if (input$all3)
          unique(data_ordered$officer)
      )  
    })
    
    subset <- reactive({
      if(is.null(input$variable_year) & is.null(input$variable_status) 
         & is.null(input$variable_officer)) {
        req(data_mod())
        dftable <- data_mod()
        
        df_subset <- dftable[, 1:6][dftable$closed_date_ym %in%
                                      input$variable_month, ]
      }
      else if (is.null(input$variable_month) & is.null(input$variable_status) 
               & is.null(input$variable_officer)) {
        req(data_mod())
        dftable <- data_mod()
        
        df_subset <- dftable[, 1:6][dftable$closed_fy_ending %in%
                                      input$variable_year, ]  
      }
      else if (is.null(input$variable_month) & is.null(input$variable_year) 
               & is.null(input$variable_officer)) {
        req(data_mod())
        dftable <- data_mod()
        
        df_subset <- dftable[, 1:6][dftable$status %in%
                                      input$variable_status, ] 
      }
      else if (is.null(input$variable_month) & is.null(input$variable_year) 
               & is.null(input$variable_status)) {
        req(data_mod())
        dftable <- data_mod()
        
        df_subset <- dftable[, 1:6][dftable$officer %in%
                                      input$variable_officer, ] 
      }
      else if (is.null(input$variable_year) & is.null(input$variable_officer) 
               & !is.null(input$variable_month) & !is.null(input$variable_status)) {
        req(data_mod())
        dftable <- data_mod()
        
        df_subset <- dftable[, 1:6][dftable$closed_date_ym %in%
                                      input$variable_month, ]
        df_subsetnew <- df_subset[, 1:6][df_subset$status %in%
                                           input$variable_status, ]
      }
      else if (is.null(input$variable_month) & is.null(input$variable_officer) 
               & !is.null(input$variable_year) & !is.null(input$variable_status)) {
        req(data_mod())
        dftable <- data_mod()
        
        df_subset <- dftable[, 1:6][dftable$closed_fy_ending %in%
                                      input$variable_year, ]
        df_subsetnew <- df_subset[, 1:6][df_subset$status %in%
                                           input$variable_status, ]
      }
      else if (is.null(input$variable_year) & is.null(input$variable_status) 
               & !is.null(input$variable_month) & !is.null(input$variable_officer)) {
        req(data_mod())
        dftable <- data_mod()
        
        df_subset <- dftable[, 1:6][dftable$closed_date_ym %in%
                                      input$variable_month, ]
        df_subsetnew <- df_subset[, 1:6][df_subset$officer %in%
                                           input$variable_officer, ]
      }
      else if (is.null(input$variable_month) & is.null(input$variable_status) 
               & !is.null(input$variable_year) & !is.null(input$variable_officer)) {
        req(data_mod())
        dftable <- data_mod()
        
        df_subset <- dftable[, 1:6][dftable$closed_fy_ending %in%
                                      input$variable_year, ]
        df_subsetnew <- df_subset[, 1:6][df_subset$officer %in%
                                           input$variable_officer, ]
      }
      else if (is.null(input$variable_status) & is.null(input$variable_officer) 
               & !is.null(input$variable_month) & !is.null(input$variable_year)) {
        req(data_mod())
        dftable <- data_mod()
        
        df_subset1 <- dftable[, 1:6][dftable$closed_date_ym %in%
                                       input$variable_month, ]
        df_subset2 <- dftable[, 1:6][dftable$closed_fy_ending %in%
                                       input$variable_year, ]
        df_subsetnew <- rbind(df_subset1, df_subset2)
      }
      else if (is.null(input$variable_month) & is.null(input$variable_year) 
               & !is.null(input$variable_status) & !is.null(input$variable_officer)) {
        req(data_mod())
        dftable <- data_mod()
        
        df_subset <- dftable[, 1:6][dftable$status %in%
                                      input$variable_status, ]
        df_subsetnew <- df_subset[, 1:6][df_subset$officer %in%
                                           input$variable_officer, ]
      }
      else if (is.null(input$variable_status) & !is.null(input$variable_year) 
               & !is.null(input$variable_month) & !is.null(input$variable_officer)) {
        req(data_mod())
        dftable <- data_mod()
        
        df_subset1 <- dftable[, 1:6][dftable$closed_fy_ending %in%
                                       input$variable_year, ]
        df_subset2 <- dftable[, 1:6][dftable$closed_date_ym %in%
                                       input$variable_month, ]
        df_subset <- rbind(df_subset1, df_subset2)
        df_subsetnew <- df_subset[, 1:6][df_subset$officer %in%
                                           input$variable_officer, ]
      }
      else if (is.null(input$variable_officer) & !is.null(input$variable_year) 
               & !is.null(input$variable_month) & !is.null(input$variable_status)) {
        req(data_mod())
        dftable <- data_mod()
        
        df_subset1 <- dftable[, 1:6][dftable$closed_fy_ending %in%
                                       input$variable_year, ]
        df_subset2 <- dftable[, 1:6][dftable$closed_date_ym %in%
                                       input$variable_month, ]
        df_subset <- rbind(df_subset1, df_subset2)
        df_subsetnew <- df_subset[, 1:6][df_subset$status %in%
                                           input$variable_status, ]
      }
      else if (is.null(input$variable_year) & !is.null(input$variable_month) 
               & !is.null(input$variable_status) & !is.null(input$variable_officer)) {
        req(data_mod())
        dftable <- data_mod()
        
        df_subset1 <- dftable[, 1:6][dftable$closed_date_ym %in%
                                       input$variable_month, ]
        df_subset <- df_subset1[, 1:6][df_subset1$status %in%
                                         input$variable_status, ]
        df_subsetnew <- df_subset[, 1:6][df_subset$officer %in%
                                           input$variable_officer, ]
      }
      else if (is.null(input$variable_month) & !is.null(input$variable_year) 
               & !is.null(input$variable_status) & !is.null(input$variable_officer)) {
        req(data_mod())
        dftable <- data_mod()
        
        df_subset1 <- dftable[, 1:6][dftable$closed_fy_ending %in%
                                       input$variable_year, ]
        df_subset <- df_subset1[, 1:6][df_subset1$status %in%
                                         input$variable_status, ]
        df_subsetnew <- df_subset[, 1:6][df_subset$officer %in%
                                           input$variable_officer, ]
      }
      else
      {
        req(data_mod())
        dftable <- data_mod()
        df_subset1 <- dftable[, 1:6][dftable$closed_fy_ending %in%
                                       input$variable_year, ]
        df_subset2 <- dftable[, 1:6][dftable$closed_date_ym %in%
                                       input$variable_month, ]
        df_subset <- rbind(df_subset1, df_subset2)
        df_subset3 <- df_subset[, 1:6][df_subset$status %in%
                                         input$variable_status, ]
        df_subsetnew <- df_subset3[, 1:6][df_subset3$officer %in%
                                            input$variable_officer, ]
      }
    })
    # options = list(scrollX = TRUE))
    output$table <- renderTable({subset()})
    
    
    # This takes the modified reactive data object data_mod(), assigns it to a
    # dataframe df. The dataset in df is subsetted based on the selected variable
    # month above and assigned into a new data frame, dfnew. The Pie chart is
    # built on the variables within dfnew
    
    plot_func <- function(dfnew, grp_vars, title, scale, dfnewinitial, dfnewinitial1, dfnewinitial2, 
                          dfnewinitial3, dfnewinitial4) {
      plotdf <- group_by(dfnew, dfnew[[grp_vars]]) %>%
        summarize(volume = sum(Ones)) %>%
        mutate(share = volume / sum(volume) * 100.0) %>%
        arrange(desc(volume))
      plotdf %>%
        ggplot(aes("", share, fill = `dfnew[[grp_vars]]`)) +
        geom_bar(
          width = 1,
          size = 1,
          color = "white",
          stat = "identity"
        ) +
        coord_polar("y") +
        geom_text(aes(label = paste0(round(share, digits = 2), "%")),
                  position = position_stack(vjust = 0.5)) +
        labs(
          x = NULL,
          y = NULL,
          fill = NULL,
          title = title
        ) +
        guides(fill = guide_legend(reverse = TRUE)) +
        
        scale_fill_manual(values = scale) +
        theme_classic() +
        theme(
          axis.line = element_blank(),
          axis.text = element_blank(),
          axis.ticks = element_blank(),
          plot.title = element_text(hjust = 0.5, color = "#666666")
        )
    }
    
    
    ###1st call to plot function to produce plot1. If/else depends on widget ticked, month or year
    
    p1 <- reactive({
      if(is.null(input$variable_year) & is.null(input$variable_status) 
         & is.null(input$variable_officer) & !is.null(input$variable_month)) {
        req(data_mod(), input$variable_month)
        df <- data_mod()
        plot_func(
          dfnew = df[, 1:8][df$closed_date_ym %in% input$variable_month, ],
          grp_vars = "age_group",
          title = "Age group segmentation",
          scale = c("#ffd700","#bcbcbc","#ffa500","#254290","#f0e68c","#808000")
        )
      }
      else if(is.null(input$variable_month) & is.null(input$variable_status)
              & is.null(input$variable_officer) & !is.null(input$variable_year)){
        req(data_mod(), input$variable_year)
        df <- data_mod()
        plot_func(
          dfnew = df[, 1:8][df$closed_fy_ending %in% input$variable_year, ],
          grp_vars = "age_group",
          title = "Age group segmentation",
          scale = c("#ffd700","#bcbcbc","#ffa500","#254290","#f0e68c","#808000")
        ) 
      }
      else if(is.null(input$variable_month) & is.null(input$variable_year)
              & is.null(input$variable_officer) & !is.null(input$variable_status)){
        req(data_mod(), input$variable_status)
        df <- data_mod()
        plot_func(
          dfnew = df[, 1:8][df$status %in% input$variable_status, ],
          grp_vars = "age_group",
          title = "Age group segmentation",
          scale = c("#ffd700","#bcbcbc","#ffa500","#254290","#f0e68c","#808000")
        ) 
      }
      else if(is.null(input$variable_month) & is.null(input$variable_year)
              & is.null(input$variable_status) & !is.null(input$variable_officer)){
        req(data_mod(), input$variable_officer)
        df <- data_mod()
        plot_func(
          dfnew = df[, 1:8][df$officer %in% input$variable_officer, ],
          grp_vars = "age_group",
          title = "Age group segmentation",
          scale = c("#ffd700","#bcbcbc","#ffa500","#254290","#f0e68c","#808000")
        ) 
      }
      else if (is.null(input$variable_year) & is.null(input$variable_officer) 
               & !is.null(input$variable_month) & !is.null(input$variable_status)) {
        req(data_mod(), input$variable_month, input$variable_status)
        df <- data_mod()
        dfnewinitial = df[, 1:8][df$closed_date_ym %in% input$variable_month, ]
        plot_func(
          dfnew = dfnewinitial[, 1:8][dfnewinitial$status %in% input$variable_status, ],  
          grp_vars = "age_group",
          title = "Age group segmentation",
          scale = c("#ffd700","#bcbcbc","#ffa500","#254290","#f0e68c","#808000")
        ) 
      } 
      else if(is.null(input$variable_month) & is.null(input$variable_officer)
              & !is.null(input$variable_year) & !is.null(input$variable_status)) {    
        req(data_mod(), input$variable_year, input$variable_status)
        df <- data_mod()
        dfnewinitial = df[, 1:8][df$closed_fy_ending %in% input$variable_year, ]
        plot_func(
          dfnew = dfnewinitial[, 1:8][dfnewinitial$status %in% input$variable_status, ],  
          grp_vars = "age_group",
          title = "Age group segmentation",
          scale = c("#ffd700","#bcbcbc","#ffa500","#254290","#f0e68c","#808000")
        ) 
      }
      else if (is.null(input$variable_status) & is.null(input$variable_year) 
               & !is.null(input$variable_month) & !is.null(input$variable_officer)) {  
        req(data_mod(), input$variable_month, input$variable_officer)
        df <- data_mod()
        dfnewinitial = df[, 1:8][df$closed_date_ym %in% input$variable_month, ]
        plot_func(
          dfnew = dfnewinitial[, 1:8][dfnewinitial$officer %in% input$variable_officer, ],  
          grp_vars = "age_group",
          title = "Age group segmentation",
          scale = c("#ffd700","#bcbcbc","#ffa500","#254290","#f0e68c","#808000")
        )
      }
      else if (is.null(input$variable_status) & is.null(input$variable_month) 
               & !is.null(input$variable_year) & !is.null(input$variable_officer)) {  
        req(data_mod(), input$variable_year, input$variable_officer)
        df <- data_mod()
        dfnewinitial = df[, 1:8][df$closed_fy_ending %in% input$variable_year, ]
        plot_func(
          dfnew = dfnewinitial[, 1:8][dfnewinitial$officer %in% input$variable_officer, ],  
          grp_vars = "age_group",
          title = "Age group segmentation",
          scale = c("#ffd700","#bcbcbc","#ffa500","#254290","#f0e68c","#808000")
        )
      }
      else if (is.null(input$variable_status) & is.null(input$variable_officer) 
               & !is.null(input$variable_month) & !is.null(input$variable_year)) {  
        req(data_mod(), input$variable_month, input$variable_year)
        df <- data_mod()
        dfnewinitial1 = df[, 1:8][df$closed_fy_ending %in% input$variable_year, ]
        dfnewinitial2 = df[, 1:8][df$closed_date_ym %in% input$variable_month, ]
        plot_func(
          dfnew = rbind(dfnewinitial1, dfnewinitial2),
          grp_vars = "age_group",
          title = "Age group segmentation",
          scale = c("#ffd700","#bcbcbc","#ffa500","#254290","#f0e68c","#808000")
        )
      }
      else if (is.null(input$variable_month) & is.null(input$variable_year) 
               & !is.null(input$variable_status) & !is.null(input$variable_officer)) {  
        req(data_mod(), input$variable_status, input$variable_officer)
        df <- data_mod()
        dfnewinitial = df[, 1:8][df$status %in% input$variable_status, ]
        plot_func(
          dfnew = dfnewinitial[, 1:8][dfnewinitial$officer %in% input$variable_officer, ],  
          grp_vars = "age_group",
          title = "Age group segmentation",
          scale = c("#ffd700","#bcbcbc","#ffa500","#254290","#f0e68c","#808000")
        )
      }
      else if (is.null(input$variable_officer) & !is.null(input$variable_month) 
               & !is.null(input$variable_status) & !is.null(input$variable_year)) {  
        req(data_mod(), input$variable_status, input$variable_month, input$variable_year)
        df <- data_mod()
        dfnewinitial1 = df[, 1:8][df$closed_fy_ending %in% input$variable_year, ]
        dfnewinitial2 = df[, 1:8][df$closed_date_ym %in% input$variable_month, ]
        dfnewinitial3 = rbind(dfnewinitial1, dfnewinitial2)
        plot_func(
          dfnew = dfnewinitial3[, 1:8][dfnewinitial3$status %in% input$variable_status, ],
          grp_vars = "age_group",
          title = "Age group segmentation",
          scale = c("#ffd700","#bcbcbc","#ffa500","#254290","#f0e68c","#808000")
          
        )
      }
      else if (is.null(input$variable_year) & !is.null(input$variable_month) 
               & !is.null(input$variable_status) & !is.null(input$variable_officer)) {  
        req(data_mod(), input$variable_month, input$variable_status, input$variable_officer)
        df <- data_mod()
        dfnewinitial1 = df[, 1:8][df$closed_date_ym %in% input$variable_closed_date_ym, ]
        dfnewinitial2 = dfnewinitial1[, 1:8][dfnewinitial1$status %in% input$variable_status, ]
        plot_func(
          dfnew = dfnewinitial2[, 1:8][dfnewinitial2$officer %in% input$variable_officer, ],  
          grp_vars = "age_group",
          title = "Age group segmentation",
          scale = c("#ffd700","#bcbcbc","#ffa500","#254290","#f0e68c","#808000")
        )
      }
      else if (is.null(input$variable_month) & !is.null(input$variable_year) 
               & !is.null(input$variable_status) & !is.null(input$variable_officer)) {  
        req(data_mod(), input$variable_status, input$variable_officer, input$variable_year)
        df <- data_mod()
        dfnewinitial1 = df[, 1:8][df$status %in% input$variable_status, ]
        dfnewinitial2 = dfnewinitial1[, 1:8][dfnewinitial1$closed_fy_ending %in% input$variable_closed_fy_ending, ]
        plot_func(
          dfnew = dfnewinitial2[, 1:8][dfnewinitial2$officer %in% input$variable_officer, ],  
          grp_vars = "age_group",
          title = "Age group segmentation",
          scale = c("#ffd700","#bcbcbc","#ffa500","#254290","#f0e68c","#808000")
        )
      }
      else if (is.null(input$variable_status) & !is.null(input$variable_month) 
               & !is.null(input$variable_year) & !is.null(input$variable_officer)) {  
        req(data_mod(), input$variable_year, input$variable_officer, input$variable_month)
        df <- data_mod()
        dfnewinitial1 = df[, 1:8][df$closed_fy_ending %in% input$variable_year, ]
        dfnewinitial2 = df[, 1:8][df$closed_date_ym %in% input$variable_month, ]
        dfnewinitial3 = rbind(dfnewinitial1, dfnewinitial2)
        plot_func(
          dfnew = dfnewinitial3[, 1:8][dfnewinitial3$officer %in% input$variable_officer, ],
          grp_vars = "age_group",
          title = "Age group segmentation",
          scale = c("#ffd700","#bcbcbc","#ffa500","#254290","#f0e68c","#808000")
        ) 
      }
      else 
      {
        req(data_mod(), input$variable_month, input$variable_year, input$variable_status,
            input$variable_officer)
        df <- data_mod()
        dfnewinitial1 = df[, 1:8][df$closed_fy_ending %in% input$variable_year, ]
        dfnewinitial2 = df[, 1:8][df$closed_date_ym %in% input$variable_month, ]
        dfnewinitial3 = rbind(dfnewinitial1, dfnewinitial2) 
        dfnewinitial4 = dfnewinitial3[, 1:8][dfnewinitial3$status %in% input$variable_status]
        plot_func(
          dfnew = dfnewinitial4[, 1:8][dfnewinitial4$officer %in% input$variable_officer, ],
          grp_vars = "age_group",
          title = "Age group segmentation",
          scale = c("#ffd700","#bcbcbc","#ffa500","#254290","#f0e68c","#808000")
        )
      }
    })
    
    
  
    output$plot1 <- renderPlot({
      p1()
    })
  
    output$tb <- renderUI({
      req(data(), subset())
      tabsetPanel(tabPanel("Age Group" ,plotOutput("plot1", height=500)), 
           
                  tabPanel("Table", tableOutput("table")))
    })
    
})

The problem is solved. Just required all variable combos to be listed correctly within the if/else statements, but also needed the condition at the end (return statement), where there was no variable selected. Also required the input variables to be listed correctly.
if (is.null( )..... & !is.null( ).....)) {
req(input$variable_..., input$variable_........)
.....
)
}
if else....
if else....
else
{
return()
}

However, if a checkbox is selected and there is no information from it, an error mesasge is produced in the plot tab. The dashboard still works and doesn't crash but a further condition will need to be included to remove this error message.
Error:Aesthetics must be either length 1 or the same as the data (1): y, fill

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