-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathserver.R
133 lines (108 loc) · 3.98 KB
/
server.R
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
library(shiny)
library(ggrepel)
#Sources data file
source("R/covid_source_file.R", local = TRUE)
server <- function(input, output, session) {
# Variables ---------------------------------------------------------------
main_data <-reactive({
state_longer_elections
})
# Creates variable for selected states
states_selected <- reactive({
input$state_filter
})
#Filters main_data to the states selected from UI
filterdata <- reactive({
main_data() %>%
filter(state %in% states_selected())
})
#Retrieves max values in order to do labeling on ggplot graph
toplabels <- reactive({
data_to_label <- filterdata() %>%
filter(scaled_deaths_per_unit == max(scaled_deaths_per_unit))
})
# Button Actions -----------------------------------------------------------------
# reset_button action
observeEvent(input$reset_button, {
updateCheckboxGroupInput(session, "state_filter", selected = c("Florida", "New York"))
updateCheckboxInput(session, "label_button", value = TRUE)
})
# all_democrat_button action
observeEvent(input$all_democrat_button, {
updateCheckboxGroupInput(session, "state_filter", selected = unique(c(state_longer_elections %>%
filter(party == "democrat"))$state))
updateCheckboxInput(session, "label_button", value = FALSE)
})
# all_republican_button action
observeEvent(input$all_republican_button, {
updateCheckboxGroupInput(session, "state_filter", selected = unique(c(state_longer_elections %>%
filter(party == "republican"))$state))
updateCheckboxInput(session, "label_button", value = FALSE)
})
# all_state_button action
observeEvent(input$all_state_button, {
updateCheckboxGroupInput(session, "state_filter", selected = unique(state_longer_elections$state))
updateCheckboxInput(session, "label_button", value = FALSE )
})
# Outputs -----------------------------------------------------------------
# lineplot
output$lineplot <- renderPlot({
p <- ggplot(
filterdata(),
aes(
x = daycount,
y = scaled_deaths_per_unit,
color = factor(party, c("republican", "democrat")),
group = state,
)
) +
labs(
title = "Deaths of COVID-19 by State" ,
subtitle = "Scaled to population of state - Colored by vote in 2016 Election (Popular)",
x = "Day Count",
y = "Deaths per 100,000 People"
) +
theme(
legend.title = element_blank(),
legend.position = "bottom",
plot.margin = unit(c(.5, 5, 1, 1), "cm") # Margins big so theres no cutoff
) +
geom_line() +
geom_point()
# label_button logical expression
if(input$label_button == TRUE){
p <- p + geom_label_repel( # This allows the labeling of the states
data = toplabels(),
aes(
x = daycount,
y = scaled_deaths_per_unit,
label = state,
group = state,
),
xlim = c((max(toplabels()$daycount) + 2.5), (max(toplabels()$daycount) + 2.5)), #This offsets the labels
show.legend = FALSE)
}
# facet_button logical expression
if(input$facet_button == TRUE){
p <- p + facet_wrap(~party)
updateCheckboxInput(session, "label_button", value = FALSE)
}
#This allows for clipping of labels outside of plot
gt <- ggplotGrob(p)
gt$layout$clip[gt$layout$name == "panel"] <- "off"
grid.draw(gt)
#downloadData Button action
output$downloadData <- downloadHandler(
filename = paste("covid_death_state_election", ".csv", sep = ""),
content = function(file) {
write.csv(main_data(), file, row.names = FALSE)
}
)
output$summary <- renderPrint({
summary(filterdata())
})
output$table <- renderTable({
filterdata()
})
}, height = 700, width = 1000)
}