Shiny App新冠数据展示求助:筛选与排序功能实现
Hey there! Let's work through fixing your Shiny app issue—since you're new to R and Shiny, I'll keep this step-by-step and easy to follow.
First, Let's Diagnose the Core Issues
From what you described:
- Your dropdown works, but charts won't render → This is almost certainly a problem with how you're handling reactive data in the server logic.
- Running ggplot alone shows missing parts of the chart → Likely a date formatting issue or missing date entries in your raw data.
Let's Build a Working Solution
I'll share a complete, annotated example that matches your requirements, then break down the key parts you might have missed.
Step 1: Prep Your Data First
First, make sure your date column (Day) is formatted as a proper date type—this fixes the "missing middle" issue in ggplot. Also, we'll fill in any missing dates so your line charts are continuous:
library(tidyverse) library(lubridate) # Load your long-format data (replace with your file path) covid_data <- read.csv("your_covid_data.csv") %>% # Convert Day to date type (adjust format if your dates are in a different style, e.g., "%m/%d/%Y") mutate(Day = ymd(Day)) %>% # Group by county to fill missing dates group_by(County) %>% # Add any missing dates between the first and last entry complete(Day = seq(min(Day), max(Day), by = "day")) %>% # Replace NA values with 0 for cases/recoveries/deaths replace_na(list(`positive cases` = 0, recoveries = 0, deaths = 0)) %>% ungroup()
Step 2: Full Shiny App Code
Here's the complete UI and server logic that will work with your data:
library(shiny) # UI Section ui <- fluidPage( titlePanel("COVID-19 County Data Dashboard"), sidebarLayout( # Dropdown menu for county selection sidebarPanel( selectInput( inputId = "selected_county", label = "Choose a County:", choices = unique(covid_data$County), selected = unique(covid_data$County)[1] # Default to first county ) ), # Main panel with three tabbed charts mainPanel( tabsetPanel( tabPanel("Confirmed Cases", plotOutput("cases_plot")), tabPanel("Recoveries", plotOutput("recoveries_plot")), tabPanel("Deaths", plotOutput("deaths_plot")) ) ) ) ) # Server Logic server <- function(input, output) { # Reactive filtered data: updates automatically when user picks a new county filtered_data <- reactive({ # req() ensures we don't run this until the user selects a county req(input$selected_county) covid_data %>% filter(County == input$selected_county) }) # Render Confirmed Cases Chart output$cases_plot <- renderPlot({ filtered_data() %>% ggplot(aes(x = Day, y = `positive cases`)) + geom_line(color = "#e74c3c", linewidth = 1) + labs(title = "Daily Confirmed COVID-19 Cases", x = "Date", y = "Number of Cases") + theme_minimal() + theme(axis.text.x = element_text(angle = 45, hjust = 1)) # Rotate dates to avoid overlap }) # Render Recoveries Chart output$recoveries_plot <- renderPlot({ filtered_data() %>% ggplot(aes(x = Day, y = recoveries)) + geom_line(color = "#2ecc71", linewidth = 1) + labs(title = "Daily COVID-19 Recoveries", x = "Date", y = "Number of Recoveries") + theme_minimal() + theme(axis.text.x = element_text(angle = 45, hjust = 1)) }) # Render Deaths Chart output$deaths_plot <- renderPlot({ filtered_data() %>% ggplot(aes(x = Day, y = deaths)) + geom_line(color = "#34495e", linewidth = 1) + labs(title = "Daily COVID-19 Deaths", x = "Date", y = "Number of Deaths") + theme_minimal() + theme(axis.text.x = element_text(angle = 45, hjust = 1)) }) } # Run the app shinyApp(ui = ui, server = server)
Key Fixes & Explanations
- Reactive Data: The
filtered_data()reactive expression is the heart of the app. It automatically updates whenever the user selects a new county, so all three charts use the latest filtered data—no need to repeat filtering code for each plot. req()Function: This prevents Shiny from trying to render charts before the user picks a county, which avoids confusing error messages.- Date Formatting: Converting
Dayto aDatetype tells ggplot to treat it as a continuous variable, so your X-axis will show all dates without gaps. Thecomplete()step fills in any missing dates in your raw data, making the line chart smooth. - Column Names with Spaces: When referencing
positive cases(a column name with a space), wrap it in backticks`so R knows it's a single column.
Testing the ggplot Code Alone
If you want to test your ggplot code outside Shiny, just filter manually and check the date type:
# Pick a county to test test_county <- "Your County Name" test_data <- covid_data %>% filter(County == test_county) # Verify date type (should say "Date") class(test_data$Day) # Plot ggplot(test_data, aes(x = Day, y = `positive cases`)) + geom_line(color = "red") + theme_minimal()
This should show a continuous line without missing sections now that your data is properly formatted.
内容的提问来源于stack exchange,提问作者Nathan May

