Skip to content

Instantly share code, notes, and snippets.

@jrosen48
Last active April 15, 2020 22:06
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jrosen48/6a78548013d5764d270db47a61401c25 to your computer and use it in GitHub Desktop.
Save jrosen48/6a78548013d5764d270db47a61401c25 to your computer and use it in GitHub Desktop.
library(shiny)
library(tidyverse)
d <- read_csv("https://raw.githubusercontent.com/nytimes/covid-19-data/master/us-counties.csv")
states <- d %>%
pull(state) %>%
unique() %>%
sort()
ui <- fluidPage(
titlePanel("United States (US) County-level COVID-19 Cases Per Day"),
sidebarLayout(
sidebarPanel(
p("Many websites provide great data on the number of COVID-19 cases per day, but not at the county level, as this simple app does"),
selectInput("state", "US State", choices = states, selected = "Tennessee"),
uiOutput("counties_input"),
checkboxInput("p1_log", "Log scale for Plot 1"),
checkboxInput("p2_log", "Log scale for Plot 2")
),
mainPanel(
h4("Plot 1: Cases Over Time"),
plotOutput("p1"),
h4("Plot 2: New Cases Over Time"),
plotOutput("p2"),
p("Data source: https://github.com/nytimes/covid-19-data"),
p("Source code for this app: https://gist.github.com/jrosen48/6a78548013d5764d270db47a61401c25")
)
)
)
server <- function(input, output) {
get_counties <- reactive({
d %>%
filter(state == input$state) %>%
pull(county) %>%
unique() %>%
sort()
})
output$counties_input <- renderUI(
selectInput("county", "County", choices = get_counties())
)
output$p1 <- renderPlot({
p1 <- d %>%
filter(county == input$county & state == input$state) %>%
mutate(new_cases = cases - lag(cases)) %>%
ggplot(aes(x = date, y = cases)) +
geom_point() +
geom_smooth() +
labs(title = str_c("Total Cases in ", input$county, " County (", input$state, ") Per Day"),
x = NULL,
y = "") +
theme_bw() +
theme(text = element_text(size = 12))
if (input$p1_log) {
p1 + scale_y_log10() + ylab("log (Cases)")
} else {
p1
}
})
output$p2 <- renderPlot({
p2 <- d %>%
filter(county == input$county & state == input$state) %>%
mutate(new_cases = cases - lag(cases)) %>%
ggplot(aes(x = date, y = new_cases)) +
geom_col() +
labs(title = str_c("New Cases in ", input$county, " County (", input$state, ") Per Day"),
x = "",
y = "") +
theme_bw() +
theme(text = element_text(size = 12))
if (input$p2_log) {
p1 + scale_y_log10() + ylab("log (Cases)")
} else {
p2
}
})
}
shinyApp(ui = ui, server = server)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment