I am trying to create an interactive map in R with the covid-19 prison data in the U.S. dataset given (after tidying), trying to plot a map of different variables (ex. staff cases, prisoner cases, prisoner deaths) and having a slider for the different months out of 15 months.
A sample of my dataset for one of the 50 states is seen here: Sample set
Below is my code so far, and I am quite stuck on everything from asterisk divider and below. I keep getting an error message saying that Error in UseMethod("select") : no applicable method for 'select' applied to an object of class "function"
when I try to use select within the created function STATE, though I don't know if that function is correct at all with what I am trying to do.
Any help would mean the world.
if (!require(Lahman)) install.packages('Lahman')
if (!require(plotly)) install.packages('plotly')
if (!require(shiny)) install.packages('shiny')
if (!require(tidyverse)) install.packages('tidyverse')
library(albersusa)
library(shiny)
library(Lahman)
library(tidyverse)
library(plotly)
my_map_theme <- function(){
theme(panel.background=element_blank(),
axis.text=element_blank(),
axis.ticks=element_blank(),
axis.title=element_blank())
}
# Load Dataset from CSV, Dataset saved and tidied from Github
cpc <- read_csv("cpc.csv")
us_states <- usa_sf("laea")
cpc_tidy <- cpc %>%
select(name,
staff_tests,
total_staff_cases,
total_staff_deaths,
prisoner_tests,
total_prisoner_cases,
total_prisoner_deaths,
as_of_date,
year,
Month) %>%
arrange(name)
cpc_tidy_f <- cpc_tidy %>%
group_by(name, year, Month) %>%
summarise(across(everything(), last))
cpc_sel <- cpc_tidy_f
cpc_sel$merge <- as.character.Date(paste(cpc_sel$year, cpc_sel$Month, sep = "-"))
TEST_JOIN <- left_join(us_states, cpc_sel, c=("name"="name"))
*****************************************************
STATE <- function(stat, Month = 11, data = cpc_sel) {
my_stat <- enquo(stat)
data %>%
select(name, merge, plot_stat = !!my_stat) %>%
# filter(yearID >= 1901) %>%
group_by(name, merge) %>%
summarize(plot_stat = sum(plot_stat)) %>%
ungroup() %>%
group_by(merge) %>%
top_n(n_players, wt = plot_stat)
}
COVID_Plot <- function(data) {
p <- TEST_JOIN %>%
mutate(text_y = paste("<b>",name,
"</b>\n Total Variable:",
signif(plot_stat,3),
"in 2020-2021")) %>%
ggplot(cpc_sel) +
geom_sf(aes(fill=plot_stat, text=text_y), color="black") +
scale_fill_continuous("Total of Variable:", low="#EEFBE5", high="#082573") +
my_map_theme()
ggplotly(p, tooltip = "text") %>%
style(hoveron = "fill")
}
# Define user interface (UI) for our app
ui <- fluidPage(
# Application title
titlePanel("Covid-19 Data in prisons across the United States"),
# Sidebar with a slider input for number of bins
sidebarLayout(
sidebarPanel(
selectInput("my_stat",
"Statistic to Plot:",
choices = list("Total Staff Cases" = "tota_staff_cases",
"Total Prisoner Cases" = "tota_prisoner_cases"
),
selected = "tota_prisoner_cases"),
sliderInput("n",
"Months:",
min = 1,
max = 15,
value = 11)),
# Show our plot
mainPanel(
h3(textOutput("TitleText")),
h5(textOutput("SubtitleText")),
h5("Graphs for U.S. prison covid data not implemented yet."),
plotlyOutput("statPlot"),
h5("Data source:",
tags$a(href="https://github.com/themarshallproject/COVID_prison_data/blob/master/data/covid_prison_cases.csv/",
"The Marshall Project Covid Prison Data")),
h5("Graphs inspired by plots in The Marshall Project article, ",
tags$a(href="https://www.themarshallproject.org/2020/05/01/a-state-by-state-look-at-coronavirus-in-prisons/",
"\"A State-By-State Look at 15 Months of Coronavirus in Prisons\""))
)
)
)
# Define server logic required to create the graph
server <- function(input, output) {
output$TitleText <- renderText(paste(input$my_stat, "Records over time"))
output$SubtitleText <- renderText(paste("Graph shows", input$n,
"for each state across the U.S."))
output$statPlot <- renderPlotly({
COVID_Plot(STATE(stat = input$my_stat, Month = input$n,
data = cpc_sel))
})
}
# Run the application
shinyApp(ui = ui, server = server)