title: R Shiny rendering plots with filtered select input
body: <p>I am trying to render conditional plots for a shiny app that would allow the use to view results by either the total sample or the selections of one factored variable. Here is a sample of the data:</p>
  <pre><code>&gt; head(data_share)
  # A tibble: 6 × 48
    student_id cohort    group       term   `Pre-Survey Que… `Pre-Survey Que… `Pre-Survey Que… `Pre-Survey Que… `Pre-Survey Que… `Pre-Survey Que… `Pre-Survey Que… `Pre-Survey Que…
         &lt;dbl&gt; &lt;fct&gt;     &lt;fct&gt;       &lt;fct&gt;  &lt;chr&gt;            &lt;chr&gt;            &lt;chr&gt;            &lt;chr&gt;            &lt;chr&gt;            &lt;chr&gt;            &lt;chr&gt;            &lt;chr&gt;           
  1          1 2017-2019 Spring 2018 Term 3 Undecided        Undecided        Undecided        Undecided        Undecided        Undecided        Agree            Undecided       
  2          2 2017-2019 Spring 2018 Term 3 Undecided        Undecided        Agree            Undecided        Agree            Undecided        Agree            Strongly Agree  
  3          3 2017-2019 Spring 2018 Term 3 Disagree         Disagree         Undecided        Disagree         Disagree         Disagree         Undecided        Disagree        
  4          4 2017-2019 Spring 2018 Term 3 Disagree         Strongly Disagr… Undecided        Disagree         Agree            Undecided        Undecided        Disagree        
  5          5 2017-2019 Spring 2018 Term 3 Disagree         Undecided        Undecided        Disagree         Agree            Undecided        Agree            Disagree        
  6          6 2017-2019 Spring 2018 Term 3 Undecided        Agree            Disagree         Undecided        Undecided        Agree            Agree            Disagree 
  
  </code></pre>
  <p>My goal is to plot results to the survey question where the user can view the total responses per question and then break down results by either group, cohort, or term. As an example, I'll share the ui and server code to produce the term plots:</p>
  <p>ui code:</p>
  <pre><code>tabPanel(shiny::HTML(&quot;&lt;span style = 'color: #0B7A42'&gt;Term Total&lt;/span&gt;&lt;/p&gt;&quot;),
                                    fluidRow(column(width = 12, offset = .7,
                                                    selectInput(&quot;prequestions_term&quot;,
                                                                shiny::HTML(&quot;&lt;span style = 'color: #0B7A42'&gt;Pre Survey Questions:&lt;/span&gt;&lt;/p&gt;&quot;),
                                                                choices = colnames(bricc_survey[c(6:22)]),
                                                                size = 5, selectize = FALSE, width = &quot;95%&quot;)),
                                             style = &quot;padding-top: 25px; padding-bottom: 25px; padding-left: 25px, padding-right: 25px&quot;),
                                    fluidRow(column(width = 12, offset = .7,
                                                    selectInput(&quot;postquestions_term&quot;,
                                                                shiny::HTML(&quot;&lt;span style = 'color: #0B7A42'&gt;Post Survey Questions:&lt;/span&gt;&lt;/p&gt;&quot;),
                                                                choices = colnames(bricc_survey[c(23:49)]),
                                                                size = 5, selectize = FALSE, width = &quot;95%&quot;)),
                                             style = &quot;padding-top: 25px; padding-bottom: 25px; padding-left: 25px, padding-right: 25px&quot;),
                                    fluidRow(column(width = 6, offset = .7,
                                                    selectInput(&quot;term_total_resp&quot;,
                                                                shiny::HTML(&quot;&lt;span style = 'color: #0B7A42'&gt;Term Number:&lt;/span&gt;&lt;/p&gt;&quot;),
                                                                choices = c(&quot;Total&quot;, levels(bricc_survey$term)),
                                                                size = 1, selectize = FALSE, width = &quot;50%&quot;)),
                                             style = &quot;padding-top: 25px; padding-bottom: 25px; padding-left: 25px, padding-right: 25px&quot;),
                                    plotlyOutput(&quot;pretermresp&quot;, width = &quot;auto&quot;),
                                    plotlyOutput(&quot;posttermresp&quot;, width = &quot;auto&quot;)),
  
  </code></pre>
  <p>server code:</p>
  <pre><code>  output$pretermresp &lt;- renderPlotly({
        
        if(input$term_total_resp == &quot;Total&quot;) {
          term_pre1 &lt;- bricc_survey %&gt;% 
            drop_na(!!sym(input$prequestions_term)) %&gt;% 
            count(!!sym(input$prequestions_term)) %&gt;% 
            mutate(pct = n/sum(n)*100) %&gt;% 
            mutate_if(is.numeric, round) 
          
          term_pre1 %&gt;% 
            ggplot() +
            aes(x = !!sym(input$prequestions_term),
                y = pct,
                label = pct) +
            geom_col(fill = &quot;#0B7A42&quot;) +
            scale_x_discrete(limits = survey_resp) +
            scale_y_continuous(limits = c(0, 100),
                               breaks = c(0, 10, 20, 30, 40, 50, 60, 70, 80, 90, 100),
                               labels = function(x) paste0(x, &quot;%&quot;)) +
            geom_text(aes(label = paste0(term_pre1$pct, &quot;%&quot;)),
                                                            nudge_y = 5, 
                                                                size = 4) +
            labs(x = &quot;&quot;, y = &quot;&quot;) +
            theme_minimal() +
            ggtitle(stringr::str_wrap(paste(&quot;Selected Question: &quot;, input$prequestions_term), 100)) +
            theme(plot.title = element_text(hjust = 0.5,size = 14, color = &quot;#0B7A42&quot;),
                  axis.text = element_text(size = 12),
                  #axis.text = element_text(angle = 90),
                  axis.title = element_text(size = 12),
                  strip.text.x = element_text(size = 12),
                  legend.title = element_text(size = 12),
                  legend.text = element_text(size = 12))
        } else {
          term_pre2 &lt;- bricc_survey %&gt;%
            filter(term %in% input$term_total_resp) %&gt;% 
            drop_na(!!sym(input$prequestions_term)) %&gt;%
            count(!!sym(input$term_total_resp), !!sym(input$prequestions_term)) %&gt;%
            group_by(!!sym(input$term_total_resp)) %&gt;%
            mutate(pct = n/sum(n)*100) %&gt;%
            mutate_if(is.numeric, round) 
  
          
          term_pre2 %&gt;% 
            ggplot() +
            aes(x = !!sym(input$prequestions_term),
                y = pct,
                label = pct) +
            geom_col(fill = &quot;#0B7A42&quot;) +
            scale_x_discrete(limits = survey_resp) +
            scale_y_continuous(limits = c(0, 100),
                               breaks = c(0, 10, 20, 30, 40, 50, 60, 70, 80, 90, 100),
                               labels = function(x) paste0(x, &quot;%&quot;)) +
            geom_text(aes(label = paste0(term_pre2$pct, &quot;%&quot;)),nudge_y = 5, size = 4) +
            labs(x = &quot;&quot;, y = &quot;&quot;) +
            theme_minimal() +
            ggtitle(stringr::str_wrap(paste(&quot;Selected Question: &quot;, input$prequestions_term), 100)) +
            theme(plot.title = element_text(hjust = 0.5,size = 14, color = &quot;#0B7A42&quot;),
                  axis.text = element_text(size = 12),
                  #axis.text = element_text(angle = 90),
                  axis.title = element_text(size = 12),
                  strip.text.x = element_text(size = 12),
                  legend.title = element_text(size = 12),
                  legend.text = element_text(size = 12))
        }
      })
  </code></pre>
  <p>when I run the app, the &quot;total&quot; plot renders correctly, but when I select a specific term, an error runs saying that the selected term cannot be found in the dataframe. Any suggestions on how to resolve this would be appreciated.</p>
  <p>Per comment below, here's an example of a smaller version of the app with the same error:</p>
  <p>data: (The term values cut off, but the factored levels for the term variable are: (&quot;Term 1&quot;, &quot;Term 2&quot;, &quot;Term 3&quot;, &quot;Term 4&quot;, &quot;Term 5&quot;)</p>
  <pre><code>student_id cohort    group       term  pre_survey_q1 pre_survey_q2 pre_survey_q3 pre_survey_q4 pre_survey_q5 pre_survey_q6 pre_survey_q7 pre_survey_q8 pre_survey_q9 pre_survey_q10
         &lt;dbl&gt; &lt;fct&gt;     &lt;fct&gt;       &lt;fct&gt; &lt;chr&gt;         &lt;chr&gt;         &lt;chr&gt;         &lt;chr&gt;         &lt;chr&gt;         &lt;chr&gt;         &lt;chr&gt;         &lt;chr&gt;         &lt;chr&gt;         &lt;chr&gt;         
  1          1 2017-2019 Spring 2018 Term… Undecided     Undecided     Undecided     Undecided     Undecided     Undecided     Agree         Undecided     Undecided     Undecided     
  2          2 2017-2019 Spring 2018 Term… Undecided     Undecided     Agree         Undecided     Agree         Undecided     Agree         Strongly Agr… Agree         Undecided     
  3          3 2017-2019 Spring 2018 Term… Disagree      Disagree      Undecided     Disagree      Disagree      Disagree      Undecided     Disagree      Undecided     Disagree      
  4          4 2017-2019 Spring 2018 Term… Disagree      Strongly Dis… Undecided     Disagree      Agree         Undecided     Undecided     Disagree      Undecided     Strongly Disa…
  5          5 2017-2019 Spring 2018 Term… Disagree      Undecided     Undecided     Disagree      Agree         Undecided     Agree         Disagree      Agree         Disagree      
  6          6 2017-2019 Spring 2018 Term… Undecided     Agree         Disagree      Undecided     Undecided     Agree         Agree         Disagree      Disagree      Disagree   
  
  </code></pre>
  <p>ui:</p>
  <pre><code>ui &lt;- fluidPage(
  
      # Application title
      titlePanel(&quot;Term Totals&quot;),
  
      # Sidebar with a slider input for number of bins 
      sidebarLayout(
          sidebarPanel(
            selectInput(&quot;prequestions_term&quot;,
                        shiny::HTML(&quot;&lt;span style = 'color: #0B7A42'&gt;Pre Survey Questions:&lt;/span&gt;&lt;/p&gt;&quot;),
                        choices = colnames(prac_data[c(5:21)]),
                        size = 5, selectize = FALSE, width = &quot;95%&quot;),
            selectInput(&quot;term_total_resp&quot;,
                        shiny::HTML(&quot;&lt;span style = 'color: #0B7A42'&gt;Term Number:&lt;/span&gt;&lt;/p&gt;&quot;),
                        choices = c(&quot;Total&quot;, levels(prac_data$term)),
                        size = 1, selectize = FALSE, width = &quot;50%&quot;)),
          
  
          # Show a plot of the generated distribution
          mainPanel(
             plotlyOutput(&quot;termplot&quot;)
          )
      ))
  
  </code></pre>
  <p>Server:</p>
  <pre><code>server &lt;- function(input, output) {
  
      
    output$termplot &lt;- renderPlotly({
      
      if(input$term_total_resp == &quot;Total&quot;) {
        term_pre1 &lt;- prac_data %&gt;% 
          drop_na(!!sym(input$prequestions_term)) %&gt;% 
          count(!!sym(input$prequestions_term)) %&gt;% 
          mutate(pct = n/sum(n)*100) %&gt;% 
          mutate_if(is.numeric, round) 
        
        term_pre1 %&gt;% 
          ggplot() +
          aes(x = !!sym(input$prequestions_term),
              y = pct,
              label = pct) +
          geom_col(fill = &quot;#0B7A42&quot;) +
          scale_x_discrete(limits = survey_resp) +
          scale_y_continuous(limits = c(0, 100),
                             breaks = c(0, 10, 20, 30, 40, 50, 60, 70, 80, 90, 100),
                             labels = function(x) paste0(x, &quot;%&quot;)) +
          geom_text(aes(label = paste0(term_pre1$pct, &quot;%&quot;)),
                    nudge_y = 5, 
                    size = 4) +
          labs(x = &quot;&quot;, y = &quot;&quot;) +
          theme_minimal() +
          ggtitle(stringr::str_wrap(paste(&quot;Selected Question: &quot;, input$prequestions_term), 100)) +
          theme(plot.title = element_text(hjust = 0.5,size = 14, color = &quot;#0B7A42&quot;),
                axis.text = element_text(size = 12),
                #axis.text = element_text(angle = 90),
                axis.title = element_text(size = 12),
                strip.text.x = element_text(size = 12),
                legend.title = element_text(size = 12),
                legend.text = element_text(size = 12))
      } else {
        term_pre2 &lt;- prac_data %&gt;%
          filter(term %in% input$term_total_resp) %&gt;% 
          drop_na(!!sym(input$prequestions_term)) %&gt;%
          count(!!sym(input$term_total_resp), !!sym(input$prequestions_term)) %&gt;%
          group_by(!!sym(input$term_total_resp)) %&gt;%
          mutate(pct = n/sum(n)*100) %&gt;%
          mutate_if(is.numeric, round) 
        
        term_pre2 %&gt;% 
          ggplot() +
          aes(x = !!sym(input$prequestions_term),
              y = pct,
              label = pct) +
          geom_col(fill = &quot;#0B7A42&quot;) +
          scale_x_discrete(limits = survey_resp) +
          scale_y_continuous(limits = c(0, 100),
                             breaks = c(0, 10, 20, 30, 40, 50, 60, 70, 80, 90, 100),
                             labels = function(x) paste0(x, &quot;%&quot;)) +
          geom_text(aes(label = paste0(term_pre2$pct, &quot;%&quot;)),nudge_y = 5, size = 4) +
          labs(x = &quot;&quot;, y = &quot;&quot;) +
          theme_minimal() +
          ggtitle(stringr::str_wrap(paste(&quot;Selected Question: &quot;, input$prequestions_term), 100)) +
          theme(plot.title = element_text(hjust = 0.5,size = 14, color = &quot;#0B7A42&quot;),
                axis.text = element_text(size = 12),
                #axis.text = element_text(angle = 90),
                axis.title = element_text(size = 12),
                strip.text.x = element_text(size = 12),
                legend.title = element_text(size = 12),
                legend.text = element_text(size = 12))
      }
    })
  
  }
  
  </code></pre>
  <p>dput() head of data:</p>
  <pre><code>dput(head(prac_data[, 1:21]))
  structure(list(student_id = structure(c(1, 2, 3, 4, 5, 6), label = &quot;Student ID&quot;, format.spss = &quot;F8.2&quot;, display_width = 9L), 
      cohort = structure(c(1L, 1L, 1L, 1L, 1L, 1L), .Label = c(&quot;2017-2019&quot;, 
      &quot;2018-2020&quot;, &quot;2019-2021&quot;, &quot;2020-2022&quot;, &quot;2021-2023&quot;), class = &quot;factor&quot;), 
      group = structure(c(9L, 9L, 9L, 9L, 9L, 9L), .Label = c(&quot;Fall 2018 1st Years&quot;, 
      &quot;Fall 2018 2nd Years&quot;, &quot;Fall 2019 1st Years&quot;, &quot;Fall 2019 2nd Years&quot;, 
      &quot;Fall 2020 1st Years&quot;, &quot;Fall 2020 2nd Years&quot;, &quot;Fall 2021 1st Years&quot;, 
      &quot;Fall 2021 2nd Years&quot;, &quot;Spring 2018&quot;, &quot;Spring 2019&quot;, &quot;Spring 2020&quot;, 
      &quot;Spring 2021&quot;, &quot;Spring 2022&quot;, &quot;Summer 2018&quot;, &quot;Summer 2019&quot;, 
      &quot;Summer 2020&quot;, &quot;Summer 2021&quot;, &quot;Winter 2019&quot;, &quot;Winter 2020&quot;, 
      &quot;Winter 2021&quot;, &quot;Winter 2022&quot;), class = &quot;factor&quot;), term = structure(c(3L, 
      3L, 3L, 3L, 3L, 3L), .Label = c(&quot;Term 1&quot;, &quot;Term 2&quot;, &quot;Term 3&quot;, 
      &quot;Term 4&quot;, &quot;Term 5&quot;, &quot;Term 6&quot;), class = &quot;factor&quot;), pre_survey_q1 = structure(c(&quot;Undecided&quot;, 
      &quot;Undecided&quot;, &quot;Disagree&quot;, &quot;Disagree&quot;, &quot;Disagree&quot;, &quot;Undecided&quot;
      ), label = &quot;Pre Survey Question 1 - I am confident in my ability to conduct a consultation for the BrICC clinic&quot;), 
      pre_survey_q2 = structure(c(&quot;Undecided&quot;, &quot;Undecided&quot;, &quot;Disagree&quot;, 
      &quot;Strongly Disagree&quot;, &quot;Undecided&quot;, &quot;Agree&quot;), label = &quot;Pre Survey Question 2 - I am confident in my ability to administer standardized cognitive tests&quot;), 
      pre_survey_q3 = structure(c(&quot;Undecided&quot;, &quot;Agree&quot;, &quot;Undecided&quot;, 
      &quot;Undecided&quot;, &quot;Undecided&quot;, &quot;Disagree&quot;), label = &quot;Pre Survey Question 3 - I am confident in my ability to conduct a client-centered clinical interview&quot;), 
      pre_survey_q4 = structure(c(&quot;Undecided&quot;, &quot;Undecided&quot;, &quot;Disagree&quot;, 
      &quot;Disagree&quot;, &quot;Disagree&quot;, &quot;Undecided&quot;), label = &quot;Pre Survey Question 4 - I am confident in my ability to identify treatment options to assist people with acquired cognitive impairments&quot;), 
      pre_survey_q5 = structure(c(&quot;Undecided&quot;, &quot;Agree&quot;, &quot;Disagree&quot;, 
      &quot;Agree&quot;, &quot;Agree&quot;, &quot;Undecided&quot;), label = &quot;Pre Survey Question 5 - I am prepared to write cognitive rehabilitation goals&quot;), 
      pre_survey_q6 = structure(c(&quot;Undecided&quot;, &quot;Undecided&quot;, &quot;Disagree&quot;, 
      &quot;Undecided&quot;, &quot;Undecided&quot;, &quot;Agree&quot;), label = &quot;Pre Survey Question 6 - I am prepared to administer direct interventions such as attention training or goal management training&quot;), 
      pre_survey_q7 = structure(c(&quot;Agree&quot;, &quot;Agree&quot;, &quot;Undecided&quot;, 
      &quot;Undecided&quot;, &quot;Agree&quot;, &quot;Agree&quot;), label = &quot;Pre Survey Question 7 - I am prepared to engage in systematic instruction to support the use of external aids&quot;), 
      pre_survey_q8 = structure(c(&quot;Undecided&quot;, &quot;Strongly Agree&quot;, 
      &quot;Disagree&quot;, &quot;Disagree&quot;, &quot;Disagree&quot;, &quot;Disagree&quot;), label = &quot;Pre Survey Question 8 - I am prepared to engage in a needs assessment to identify cognitive strategies and support learning and use of them&quot;), 
      pre_survey_q9 = structure(c(&quot;Undecided&quot;, &quot;Agree&quot;, &quot;Undecided&quot;, 
      &quot;Undecided&quot;, &quot;Agree&quot;, &quot;Disagree&quot;), label = &quot;Pre Survey Question 9 - I am confident in my ability to collect and analyze client session data&quot;), 
      pre_survey_q10 = structure(c(&quot;Undecided&quot;, &quot;Undecided&quot;, &quot;Disagree&quot;, 
      &quot;Strongly Disagree&quot;, &quot;Disagree&quot;, &quot;Disagree&quot;), label = &quot;Pre Survey Question 10 - I am prepared to justify my decisions related to assessment and treatment selection&quot;), 
      pre_survey_q11 = structure(c(&quot;Undecided&quot;, &quot;Agree&quot;, &quot;Agree&quot;, 
      &quot;Agree&quot;, &quot;Agree&quot;, &quot;Agree&quot;), label = &quot;Pre Survey Question 11 - I am confident in my ability to apply principles of evidence-based practice to my assessment and treatment decisions&quot;), 
      pre_survey_q12 = structure(c(&quot;Agree&quot;, &quot;Agree&quot;, &quot;Disagree&quot;, 
      &quot;Undecided&quot;, &quot;Undecided&quot;, &quot;Strongly Disagree&quot;), label = &quot;Pre Survey Question 12 - I am confident in my ability to make ‘online’ (in session) changes to my daily plans&quot;), 
      pre_survey_q13 = structure(c(&quot;Undecided&quot;, &quot;Agree&quot;, &quot;Undecided&quot;, 
      &quot;Undecided&quot;, &quot;Agree&quot;, &quot;Undecided&quot;), label = &quot;Pre Survey Question 13 - I am knowledgeable about concussion management&quot;), 
      pre_survey_q14 = structure(c(&quot;Agree&quot;, &quot;Agree&quot;, &quot;Undecided&quot;, 
      &quot;Undecided&quot;, &quot;Disagree&quot;, &quot;Undecided&quot;), label = &quot;Pre Survey Question 14 - I am comfortable working with clients with brain injuries&quot;), 
      pre_survey_q15 = structure(c(&quot;Undecided&quot;, &quot;Undecided&quot;, &quot;Undecided&quot;, 
      &quot;Undecided&quot;, &quot;Disagree&quot;, &quot;Undecided&quot;), label = &quot;Pre Survey Question 15 - I am comfortable working with clients with awareness deficits&quot;), 
      pre_survey_q16 = structure(c(&quot;Agree&quot;, &quot;Agree&quot;, &quot;Agree&quot;, &quot;Undecided&quot;, 
      &quot;Agree&quot;, &quot;Undecided&quot;), label = &quot;Pre Survey Question 16 - I am able to use case history information (e.g., information about etiology) to guide my clinical decision making&quot;), 
      pre_survey_q17 = structure(c(&quot;Agree&quot;, &quot;Undecided&quot;, &quot;Undecided&quot;, 
      &quot;Disagree&quot;, &quot;Undecided&quot;, &quot;Agree&quot;), label = &quot;Pre Survey Question 17 - I feel prepared to orally present cases&quot;)), row.names = c(NA, 
  -6L), class = c(&quot;tbl_df&quot;, &quot;tbl&quot;, &quot;data.frame&quot;))
  
  </code></pre>