-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.R
348 lines (240 loc) · 11.9 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
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
library(shiny)
library(tidyverse)
library(DT)
library(shinythemes)
# devtools::install_github("ITHIM/ITHIM", ref="devel", force=TRUE)
library("ITHIM")
# PAfilePath <- "https://raw.githubusercontent.com/ITHIM/ITHIM/devel/inst/activeTransportTime.csv"
# BURfilePath <- "https://raw.githubusercontent.com/ITHIM/ITHIM/devel/inst/burden.portland.csv"
# POPfilePath <- "https://raw.githubusercontent.com/ITHIM/ITHIM/devel/inst/F.portland.csv"
PAexamplePath <- system.file("activeTravelOHAS.csv", package = "ITHIM")
BURexamplePath <- system.file("burden.portland.csv", package = "ITHIM")
POPexamplePath <- system.file("F.portland.11_21_2017.csv", package = "ITHIM")
PAdownload <- read.csv(PAexamplePath, header=T)
BURdownload <- read.csv(BURexamplePath, header=T)
POPdownload <- read.csv(POPexamplePath, header=T)
ui <- fluidPage(
navbarPage(position = "fixed-top",
header = tags$style(type="text/css", "body {padding-top: 70px;}"),
theme = shinytheme(theme = "simplex"),
title = "ITHIMio",
tabPanel(title = "Introduction",
column(6,
includeHTML("intro.html")
)
),
navbarMenu(title = "Inputs",
tabPanel("Physical Activity",
fluidRow(column(3,
####### PHYSICAL ACTIVITY ########
# Download Button
downloadLink(outputId = "downloadPAexample", label = "Download Sample Transport Times"),
br(),
# Upload PA Data
fileInput('file1', 'Choose Physical Activity File (csv)',
accept=c('text/csv', 'text/comma-separated-values,text/plain', '.csv'))
),
column(9,
wellPanel(
plotOutput('PAplot')
)
)
),
fluidRow(
wellPanel(
dataTableOutput('PAtab')
)
)
),
tabPanel("Disease Burden",
fluidRow(column(3,
####### BURDEN ########
# Download Button
downloadLink("downloadBURexample", "Download Sample Disease Burdens"),
br(),
# Upload burden Data
fileInput('file2', 'Choose Disease Burden File (csv)',
accept=c('text/csv', 'text/comma-separated-values,text/plain', '.csv'))
),
column(9,
wellPanel(
plotOutput('BURplot')
)
)
),
fluidRow(
wellPanel(
dataTableOutput('BURtab')
)
)
),
tabPanel("Population",
fluidRow(column(3,
####### POP ########
# Download Button
downloadLink("downloadPOPexample", "Download Sample Populations"),
br(),
# Upload Pop Data
fileInput('file3', 'Choose Population File (csv)',
accept=c('text/csv', 'text/comma-separated-values,text/plain', '.csv')) # ,
#
# fileInput('fileXL', 'Or, upload an excel version with a single calibration sheet (in Beta)',
# accept=c('text/csv', 'text/comma-separated-values,text/plain', '.csv'))
),
column(9,
wellPanel(
plotOutput('POPplot')
)
)
),
fluidRow(
wellPanel(
dataTableOutput('POPtab')
)
)
)
), # close NavBar Menu
tabPanel(title = "Scenario",
fluidRow(
column(3,
sliderInput(inputId = "newWalk", label = "% Increase in Mean Walking Time",value = 0, step = 5,round = T,max = 100, min = 0)
),
column(3,
sliderInput(inputId = "newCycle", label = "% Increase in Mean Cycling Time", value = 0, step = 5,round = T, max = 100, min = 0)
)
)
),
tabPanel(title = "Results",
fluidRow(
column(3,
tags$h4(textOutput('summary'))
),
column(9,
wellPanel(dataTableOutput('superTab'))
)
),
wellPanel(plotOutput("resultsPlot"))
)
)
)
server <- function(input, output, session) {
##### PHYSICAL ACTIVITY #######
output$PAplot <- renderPlot({
# input$file1 will be NULL initially. After the user selects
# and uploads a file, it will be a data frame with 'name',
# 'size', 'type', and 'datapath' columns. The 'datapath'
# column will contain the local filenames where the data can
# be found.
inFilePA <- input$file1
PAfile <- read.csv(ifelse(is.null(inFilePA),PAexamplePath,inFilePA$datapath),header = T, sep=",")
ggplot(PAfile, aes(x=ageClass, y=value, fill=sex)) + geom_bar(stat="identity", position="dodge") +
facet_grid(mode ~ .) +
ggtitle("Age-Sex Active Transport Times") +
ylab("Minutes per Week")
})
output$PAtab <- renderDataTable({
inFilePA <- input$file3
PAfile <- read.csv(ifelse(is.null(inFilePA),PAexamplePath,inFilePA$datapath), header = T, sep=",")
PAfile
})
# Downloadable csv of selected dataset ----
output$downloadPAexample <- downloadHandler(
filename = "ActiveTransportTime.csv",
content = function(file) {
write.csv(PAdownload, file, row.names = FALSE)
}
)
##### BURDEN ###########
output$BURplot <- renderPlot({
# input$file1 will be NULL initially. After the user selects
# and uploads a file, it will be a data frame with 'name',
# 'size', 'type', and 'datapath' columns. The 'datapath'
# column will contain the local filenames where the data can
# be found.
inFileBurden <- input$file2
burdenFile <- read.csv(ifelse(is.null(inFileBurden),BURexamplePath,inFileBurden$datapath), header = T, sep=",")
ggplot(burdenFile, aes(x=ageClass, y=value, fill=sex)) + geom_bar(stat="identity", position="dodge") +
facet_grid(burdenType ~ disease) +
ggtitle("Age-Sex Baseline Disease Burdens")
})
output$BURtab <- renderDataTable({
inFileBUR <- input$file3
BURfile <- read.csv(ifelse(is.null(inFileBUR),BURexamplePath,inFileBUR$datapath), header = T, sep=",")
BURfile
})
# Downloadable csv of selected dataset ----
output$downloadBURexample <- downloadHandler(
filename = "PortlandBurden.csv",
content = function(file) {
write.csv(BURdownload, file, row.names = FALSE)
}
)
##### POPULATION ###########
output$POPplot <- renderPlot({
# input$file1 will be NULL initially. After the user selects
# and uploads a file, it will be a data frame with 'name',
# 'size', 'type', and 'datapath' columns. The 'datapath'
# column will contain the local filenames where the data can
# be found.
inFilePOP <- input$file3
POPfile <- read.csv(ifelse(is.null(inFilePOP),POPexamplePath,inFilePOP$datapath), header = T, sep=",")
ggplot(POPfile, aes(x=ageClass, y=value, fill=sex)) + geom_bar(stat="identity", position="dodge") +
ggtitle("Age-Sex Population Distribution")
})
output$POPtab <- renderDataTable({
inFilePOP <- input$file3
POPfile <- read.csv(ifelse(is.null(inFilePOP),POPexamplePath,inFilePOP$datapath), header = T, sep=",")
POPfile
})
# Downloadable csv of selected dataset ----
output$downloadPOPexample <- downloadHandler(
filename = "PortlandPopulation.csv",
content = function(file) {
write.csv(POPdownload, file, row.names = FALSE)
}
)
##### summary in create ITHIM File ###########
ITHIM.baseline <- reactive({
inFilePA <- ifelse(is.null(input$file1), PAexamplePath, input$file1$datapath)
inFileBUR <- ifelse(is.null(input$file2), BURexamplePath, input$file2$datapath)
inFilePOP <- ifelse(is.null(input$file3), POPexamplePath, input$file3$datapath)
return(createITHIM(activeTransportFile = inFilePA, GBDFile = inFileBUR, FFile = inFilePOP))
})
ITHIM.double <- reactive({
update(ITHIM.baseline(),
list(muwt = ITHIM.baseline()@parameters@muwt*2,
muct = ITHIM.baseline()@parameters@muct*2))
})
newWalk <- reactive({ITHIM.baseline()@parameters@muwt*((input$newWalk/100)+1)})
newCycle <- reactive({ITHIM.baseline()@parameters@muct*((input$newCycle/100)+1)})
ITHIM.scenario <- reactive({
update(ITHIM.baseline(), list(muwt = newWalk(), muct = newCycle()))
})
output$summary <- renderText({
lives0 <- round(deltaBurden(ITHIM.baseline(), update(ITHIM.baseline(), list(muwt = 0.1,muct = 0.1)), bur = "deaths" , dis = "all"),0)
lives.scenario <- round(deltaBurden(ITHIM.baseline(), ITHIM.scenario(), bur = "deaths" , dis = "all"),0) *-1
paste0("The estimated number of deaths prevented by current walking and cycling levels is ", lives0,
". \n Under the suggested scenario: \n mean walking time increased by ",input$newWalk,
"% (to ", round(newWalk(),0)," min/week) \n mean cycling time increased by ",input$newCycle,
"% (to ", round(newCycle(),0)," min/week). \n This would prevent an additional ",lives.scenario," deaths per year.")
})
superTable <- eventReactive(c(input$newWalk, input$newCycle),
{
superTabulate(ITHIM.baseline = ITHIM.baseline(), ITHIM.scenario.list = c(ITHIM.scenario(), ITHIM.double()))
}
)
output$superTab <- renderDataTable({
superTable()
})
output$resultsPlot <- renderPlot({
superTable() %>%
filter(burdenType == "daly") %>%
spread(key = vision, value = value) %>%
mutate(difference = scenario1-scenario2) %>%
group_by(disease, ageClass) %>%
summarise(avoidedBurden = sum(difference, na.rm=T)) %>%
ggplot(aes(x = ageClass, y = avoidedBurden, fill = disease)) +
geom_bar(stat = "identity")
})
}
shinyApp(server = server, ui = ui)