Fixing Data Frame Column Names and Date Conversions in Shiny App

The problem lies in the fact that data and TOTALE, anno are column names from your data frame, but they should be anno and TOTALE respectively.

Also, dmy("16-03-2020") is used to convert a date string into a Date object. However, since the date string “16-03-2020” corresponds to March 16th, 2020 (not March 16th, 2016), this might be causing issues if you’re trying to match it with another date.

Here’s an updated version of your code:

ui <- fluidPage(
  # Application title
  titlePanel("Exploratory Data Analysis of Trieste Airport"),
  # Sidebar with a slider input for number of bins
  sidebarLayout(
    sidebarPanel(
      checkboxGroupInput(
        inputId = "year",
        label = "Anno",
        choices = unique(eda_trieste_airport$anno),
        selected = unique(eda_trieste_airport$anno)
      )
    ),
    # Show a plot of the generated distribution
    mainPanel(
      plotOutput("colPlot")
    )
  )
)

# Define server logic required to draw a histogram
server <- function(input, output) {
  eda_reactive <- reactive({
    req(input$year)
    eda_trieste_airport %>% filter(anno == input$year)
  })
  
  output$colPlot <- renderPlot({
    # generate a col plot
    ggplot(data = eda_reactive(), aes(x = anno, y = TOTALE, fill = anno)) +
      geom_col() +
      geom_vline(xintercept = as.numeric(16/3), color = "black", size = 1) +
      labs(
        x = "Anno",
        y = "Totale passeggeri"
      )
  })
}

# Run the application
shinyApp(ui = ui, server = server)

In this updated code:

  • I replaced data with anno in aes() function.
  • I removed dmy("16-03-2020") since it’s not needed anymore and changed xintercept to as.numeric(16/3) for the vertical line.
  • I also added a line break after fill = anno in aes().

This updated code should work correctly now.


Last modified on 2025-03-02