-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.R
250 lines (216 loc) · 9.24 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
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
#--------------------------------------------------------------------------#
# Server file #
#--------------------------------------------------------------------------#
# Version | Name | Remarks #
#--------------------------------------------------------------------------#
# 1.0 | Prathiba | Initial Version; Server script for #
# | | Home Page, Trend Page & Rank Page #
# | | #
# 1.1 | Derek | Server script for Life Expectancy #
# 1.2 | JP | Server script for Drug Abuse #
# 1.3 | Paddy | Server script for Map #
#--------------------------------------------------------------------------#
server <- function(input, output, session) {
# Server script for Home Page -----------------------------------------------
observeEvent(input$jumpToTrend, {
newtab <- switch(input$tabs, "home_tab" = "trend_tab",)
updateTabItems(session, "tabs", newtab)
})
observeEvent(input$jumpToRank, {
newtab <- switch(input$tabs,"home_tab" = "rank_tab",)
updateTabItems(session, "tabs", newtab)
})
observeEvent(input$jumpToMap, {
newtab <- switch(input$tabs,"home_tab" = "map_tab",)
updateTabItems(session, "tabs", newtab)
})
# Server script for Trend Page ------------------------------------------------
#This output variable is used in UI to display the plot title in Trend Tab
output$topic <- renderText({ input$topic_input})
output$area <- renderText({ input$area_input})
output$name <- renderText({ input$name_input})
output$demographic <- renderText({ input$demographic_input})
output$breakdown <- renderText({ input$breakdown_input})
# Update Inputs for Trend Page
# Event to populate the area dynamically
observeEvent( input$topic_input, {
updateSelectInput(
inputId = "area_input",
choices = choose_area(input$topic_input),
session = getDefaultReactiveDomain()
)
})
# Event to populate the name dynamically
observeEvent(c( input$area_input,
input$topic_input), {
updateSelectInput(
inputId = "name_input",
choices = choose_name(input$topic_input,input$area_input),
session = getDefaultReactiveDomain()
)
})
# Event to populate the breakdown topic dynamically
observeEvent(input$topic_input, {
updateSelectInput(
inputId = "breakdown_input",
choices = switch(input$topic_input,
"Life Expectancy" = "Gender",
"Drug Abuse" = c("Gender", "Age"),
"Smoking" = c("Gender", "Age")
),
session = getDefaultReactiveDomain()
)
})
# Event to populate the choices of breakdown dynamically
observeEvent(c(input$breakdown_input,
input$area_input,
input$name_input,
input$topic_input), {
choice <- choose_breakdown( input$topic_input,
input$breakdown_input,
input$area_input,
input$name_input )
updateCheckboxGroupInput(session = session,
inputId = "demographic_input",
choices = choice,
selected = sort(choice),
inline = FALSE
)
})
# Server script for Rank Page ------------------------------------------------
#This output variable is used in UI to display the plot title in Rank Tab
output$rank_topic<- renderText({ input$rank_topic_input})
output$rank_area <- renderText({ input$rank_area_input})
output$rank_sex <- renderText({ input$rank_sex_input})
output$rank_select <- renderText({input$rank_select_input})
# Server script for Trends Page -------------------------------------
filtered_data <- reactive(
select_trend_data(input$breakdown_input,input$name_input,input$area_input,input$demographic_input, input$topic_input)
)
# Function to create ggplot
trend_plot <- reactive(
make_trend_plot(data = filtered_data(), input$breakdown_input, input$topic_input)
)
# create plot
output$distPlot <- renderPlotly({
plotly::ggplotly(trend_plot())
})
# data table to show the data displayed in the life expectancy plot
output$output_table <- renderDataTable({
filtered_data()
})
# Server script for Rank Page -------------------------------------
filtered_rank_data <- reactive(
select_rank_data(input$rank_topic_input, input$rank_sex_input, input$rank_area_input, input$rank_select_input)
)
# Function to create ggplot
rank_plot <- reactive(
make_rank_plot(data = filtered_rank_data(),input$rank_topic_input,input$rank_select_input)
)
# create plot
output$rank_distPlot <- renderPlotly({
ggplotly(rank_plot())
})
# data table to show the data displayed in the life expectancy plot
output$rank_output_table <- renderDataTable({
filtered_rank_data()
})
# Server script for Map ------------------------------------------------
map_area_input <- reactive(input$map_area_input)
map_topic_input <- reactive(input$map_topic_input)
map_breakdown_input <- reactive(input$map_breakdown_input)
map_group_input <- reactive(input$map_group_input)
map_data <- reactive(get_map_data(map_topic_input(),
map_area_input(),
map_breakdown_input(),
map_group_input())
)
observeEvent(input$map_topic_input, {
updateSelectInput(
session = getDefaultReactiveDomain(),
inputId = "map_breakdown_input",
choices = switch(
input$map_topic_input,
"Life Expectancy" = c("None", "Gender"),
"Drug Abuse" = c("None", "Age", "Gender"),
"Smoking" = c("None", "Age", "Gender")
)
)
})
observeEvent(c(input$map_breakdown_input,
input$map_topic_input), {
updateSelectInput(
session = getDefaultReactiveDomain(),
inputId = "map_group_input",
choices = get_group_choices(map_topic_input(), map_breakdown_input())
)
})
#get the map
output$my_map <- renderLeaflet({
map_data() %>%
leaflet() %>%
#addTiles() %>% #uncomment this line to add background map
addPolygons(
popup = ~ str_c(name, "<br>", label, " = ", round(value, 2), sep = ""),
color = ~colour
) %>%
addLegend(
position = "bottomright",
colors = ~colour,
labels = ~name
)
})
#get the table
output$map_table <- renderDataTable({
map_data() %>%
as_tibble() %>%
mutate("Area Name" = name,
"Value" = round(value, 2)
) %>%
select("Area Name", Value)
})
#get the stats
output$map_stats <- renderTable({
map_data() %>%
as_tibble() %>%
summarise(
"Mean" = mean(value),
"Standard Deviation" = sd(value)
) %>%
pivot_longer(cols = 1:2)
},
colnames = FALSE)
#get the units
output$map_units <- renderText({
switch(
map_topic_input(),
"Life Expectancy" = "Displaying Years of Life Expectancy.",
"Drug Abuse" = "Displaying Number of Individuals Presenting for Assessment.",
"Smoking" = "Displaying Percentage of Population who Smoke."
)
})
# output$plot <- renderPlot(plot(),width = 850, height = 425)
#
# # Download script -----------------------------------------------------
# output$download_report <- downloadHandler(
#
# filename = "my_report.html",
#
# content = function(file) {
# src <- normalizePath('report.Rmd')
# owd <- setwd(tempdir())
# on.exit(setwd(owd))
# file.copy(src, 'report.Rmd', overwrite = TRUE)
# #all the info you need to pass to the output file
# params <- list( plot = plot )
#
# out <- render('report.Rmd',
# output_format = pdf_document(),
# params = params,
# envir = new.env(parent = globalenv())
# )
#
# file.rename(out, file)
# }
# )
}