Plot and Table in Shiny App Not Dynamic

Hi All,

I'm having the following issues with creating a very simple Shiny app. The purpose of the app is to
dynamically display three sets of boxplots and a dynamic table. There is one boxplot per quarter in my dataset, and each time I select a different quarter, I expect the set of boxplots and corresponding table to update.

The problem I am having is when I select different values for the input (i.e., different quarters) the plot does not update and the table shows all NA values.

Below is my code. I have been struggling with this all day. Please help me.

Thank you,
Aaron


ui <- fluidPage(

    # Application title
    titlePanel("Boxplots of Measures of Performance by Race"),

    # Sidebar with a slider input for number of bins
    sidebarLayout(
        sidebarPanel(
            selectInput(inputId = "Quarter_Num",
                        label = "Select Quarter:",
                        choices = c("Quarter 1", "Quarter 2", "Quarter 3"))),
    
        mainPanel(
            plotOutput("boxplot"),
            tableOutput("info")
           
        )
        
   )
)
server <- function(input, output) {
  output$boxplot <- renderPlot({
    Restructured_Race_Table %>%
    ggplot(aes(x=Race_Desc, y=Percentage, fill = Race_Desc)) +
                geom_boxplot() + ylim(0, 100)
  })
  
  output$info <- renderTable({
    Restructured_Race_Table %>%
    group_by(Race_Desc) %>%
    summarise(mean_Percentage = mean(Percentage), 
              median_Percentage = median(Percentage)) %>%
              ungroup()
              
  })
  
}
shinyApp(ui, server)

You aren't making use of input$Quarter_Num in the server. You should make something like filtered_data <- reactive(<more code>) that uses that input value to filter the data and pass that to your render functions instead of Restructured_Race_Table using `filtered_data().

I think you should take some time to read through at least the first few pages of Shiny documentation: Shiny - Welcome to Shiny. I do not see any usage of inputs in your server which is a fundamental component of creating an interactive Shiny application.

I don't know how your data looks, but I think you might intend for your server code to look something like the code below. Note that I've added a filtering function where the quarter (I don't know what your quarter variable is named so this is just a placeholder) needs to be set equal to the value of input$Quarter_Num which would the selected value in the selectInput box. Note that the choices you have available must also be available in the data set exactly as written or you will still get NAs/missing plots.

server <- function(input, output) {
  output$boxplot <- renderPlot({
    Restructured_Race_Table %>%
   dplyr::filter(quarter == input$Quarter_Num) %>%
    ggplot(aes(x=Race_Desc, y=Percentage, fill = Race_Desc)) +
                geom_boxplot() + ylim(0, 100)
  })
  
  output$info <- renderTable({
    Restructured_Race_Table %>%
    group_by(Race_Desc) %>%
   dplyr::filter(quarter == input$Quarter_Num) %>%
    summarise(mean_Percentage = mean(Percentage), 
              median_Percentage = median(Percentage)) %>%
              ungroup()
              
  })
  
}
1 Like

Thank, you helped me figure out what to do.
It works now.