-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.R
290 lines (212 loc) · 9.66 KB
/
app.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
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
library(shiny)
library(shinythemes)
library(here)
library(tidyverse)
library(sf)
library(tmap)
library(shapefiles)
library(infer)
library(markdown)
library(tm)
library(SnowballC)
library(wordcloud)
library(RColorBrewer)
library(rsconnect)
source(here("r_scripts/word_cloud.R"))
source(here("r_scripts/life_expectancy.R"))
source(here("r_scripts/priority_2_ p1_bmi_script.R"))
source(here("r_scripts/bmi.R"))
source(here("r_scripts/exercise.R"))
source(here("r_scripts/map_asthma.R"))
source(here("r_scripts/priority_3_graph.R"))
source(here("r_scripts/priority_4_graph.R"))
source(here("r_scripts/asthma_area_graph_MF_script.R"))
source(here("r_scripts/asthma_line_deaths_by_gender_script.R"))
source(here("r_scripts/asthma_line_rate_by_gender_script.R"))
source(here("r_scripts/asthma_MF_boxplot_script.R"))
source(here("r_scripts/hypothesis_test.R"))
source(here("r_scripts/priority_5_graph.R"))
# Define UI for application that draws a presentation with three tabs (add text, graphs, action buttons etc)
ui <- (fluidPage(
theme = shinytheme("flatly"),
# Application title
titlePanel("Public Health in Scotland"),
tabsetPanel(
tabPanel("Intro",
br(),
fluidRow(
column(8,
includeMarkdown("descriptions/introduction_text.md")
)
)
),
tabPanel("Overview",
sidebarLayout(
sidebarPanel(
selectInput("select_priority",
label = "Select Priority",
choices = c("Life Expectancy", "BMI in Children", "BMI in Adults", "Activity Levels of Adults", "Mental Health", "Smoking Levels", "Health Inequality")
),
br(),
br(),
# show word cloud
plotOutput("word_cloud"),
br(),
# link to the Public Health Priorities document
tags$a("Public Health Priorities for Scotland", href = "https://www.gov.scot/binaries/content/documents/govscot/publications/corporate-report/2018/06/scotlands-public-health-priorities/documents/00536757-pdf/00536757-pdf/govscot%3Adocument/00536757.pdf")
),
mainPanel(
# overview related graph
plotOutput("graph"),
uiOutput("md_file_overview_tab")
)
)
),
tabPanel("Asthma",
sidebarLayout(
sidebarPanel(
selectInput("select_indicator",
label = "Select Indicator",
choices = c("Prevalence of doctor-diagnosed asthma", "Hospital stays related to Asthma diagnosis", "Hospital stay rates related to Asthma diagnosis")
),
hr(),
hr(),
absolutePanel(top = 90, #left = 80,
style="z-index:500;", # legend over my map (map z = 400)
#tags$h3("map"),
sliderInput("year_slider",
"Choose year range",
min = 2012, # min(map_and_data$discharge_fin_yr_end),
max = 2019, # max(map_and_data$discharge_fin_yr_end),
value = 2012,
sep = "", #getting rid of the default "," separator (eg 1,234)
width = "400px"
)
),
hr(),
hr(),
# interactive map
tmapOutput("map", width = "100%", height = 600),
textOutput("copyright")
),
mainPanel(
selectInput("select_topic",
label = "Select Topic",
choices = c("Asthma in proportion", "Death by gender", "Rate by gender", "Hypothesis test - box plot", "Hypothesis test - null distribution")
),
mainPanel(
# asthma related graph
plotOutput("graph_2"),
textOutput("text_with_graph_2"),
uiOutput("md_file_asthma_tab")
)
)
)
)
)
))
# Define server logic
server <- (function(input, output) {
### setting up the reactive functionality, to get the results only after selecting filters and pressing action button
### improving efficiency of the code by applying the filter function only once if used multiple times
# data_filtered <- eventReactive(input$update, {
# data %>%
# filter( == input$something_from_UI)
# })
### widget - select priority for which to display a graph (Overview Tab)
output$priority <- renderPrint({ input$select_priority })
# plot word cloud
output$word_cloud <- renderPlot({
wordcloud(words = d$word, freq = d$freq, min.freq = 1,
max.words=100, random.order=FALSE, rot.per=0.35,
colors=brewer.pal(8, "Dark2"))
})
output$graph <- renderPlot({
if(input$select_priority=="Life Expectancy"){
return(life_expectancy_graph)
}
if(input$select_priority=="Health Inequality"){
return(priority_5_graph)
}
if(input$select_priority=="BMI in Children"){
return(p1_bmi_for_graph)
}
if(input$select_priority=="BMI in Adults"){
return(bmi_graph)
}
if(input$select_priority=="Activity Levels of Adults"){
return(physical_activity_graph)
}
if(input$select_priority=="Mental Health"){
return(priority_3_graph)
}
if(input$select_priority=="Smoking Levels"){
return(priority_4_graph)
}
})
# plot thematic map based on selected input (Asthma Tab)
output$map <- renderTmap({
map_and_data <- map_and_data %>%
filter(discharge_fin_yr_end == input$year_slider)
if (input$select_indicator == "Prevalence of doctor-diagnosed asthma") {
tm_shape(map_and_data) +
tm_polygons("Value", id = "HBName", fill = "Value", title = "Asthma prevalence (%)") + # add polygons colored by the Value (percentage), display name of HB when hovering mouse over the map
tmap_mode("view") # turn it into interactive (clickable) map, set tmap viewing mode to "view" = interactive
}
else if (input$select_indicator == "Hospital stays related to Asthma diagnosis") {
tm_shape(map_and_data) +
tm_polygons("stay", id = "HBName", fill = "stay", title = "Hospital stays (days)") +
tmap_mode("view")
}
else if (input$select_indicator == "Hospital stay rates related to Asthma diagnosis") {
tm_shape(map_and_data) +
tm_polygons("avg_rate", id = "HBName", fill = "avg_rate", title = "Rate per 100,000 population (%)") +
tmap_mode("view")
}
})
### widget - select topic for which to display a graph (Asthma Tab)
output$asthma_topic <- renderPrint({ input$select_priority })
output$graph_2 <- renderPlot({
if(input$select_topic=="Asthma in proportion"){
return(area_graph_asthma_data_graph)
}
if(input$select_topic=="Death by gender"){
return(asthma_line_deaths_by_gender_graph)
}
if(input$select_topic=="Rate by gender"){
return(asthma_line_rate_MF_BS_graph)
}
if(input$select_topic=="Hypothesis test - box plot"){
return(box_plot_viz)
}
if(input$select_topic=="Hypothesis test - null distribution"){
return(null_distribution_viz)
}
})
output$copyright <- renderText({ "Copyright Scottish Government, contains Ordnance Survey data © Crown copyright and database right (2021)" })
# description with the graphs in Overview tab
output$md_file_overview_tab <- renderUI({
file_overview <- switch(input$select_priority,
"Life Expectancy" = "descriptions/life_expectancy.md",
"BMI in Children" = "descriptions/children_bmi_p1_description.md",
"BMI in Adults" = "descriptions/bmi_id_adults.md",
"Activity Levels of Adults" = "descriptions/activity_levels_of_adults.md",
"Mental Health" = "descriptions/Mental_Health_points.md",
"Smoking Levels" = "descriptions/Smoking_points.md",
"Health Inequality" = "descriptions/health_inequality.md"
)
includeMarkdown(file_overview)
})
# description with the graphs in Asthma tab
output$md_file_asthma_tab <- renderUI({
file_asthma <- switch(input$select_topic,
"Asthma in proportion" = "descriptions/combined_respiratory_deaths_description.md",
"Death by gender" = "descriptions/death_by_gender_description.md",
"Rate by gender" = "descriptions/rate_by_gender_description.md",
"Hypothesis test - box plot" = "descriptions/box_plot.md",
"Hypothesis test - null distribution" = "descriptions/null_hypothesis.md"
)
includeMarkdown(file_asthma)
})
})
shinyApp(ui = ui, server = server)