/
server.R
377 lines (304 loc) · 12.4 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
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
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
######################
####### SERVER #######
######################
server <- function(input, output, session) {
# Second tab - builds participant list
participant_list_outputs <- callModule(participantListModule, "pl", data = participant_list, filter = c("gender", "Gender"))
# Possible filters for participants list table
observeEvent(input$filter_choice, {
if (input$filter_choice == "Gender") {
participant_list_outputs <- callModule(participantListModule, "pl", data = participant_list, filter = c("gender", "Gender"))
} else if (input$filter_choice == "Race") {
participant_list_outputs <- callModule(participantListModule, "pl", data = participant_list, filter = c("race", "Race"))
} else if (input$filter_choice == "Age") {
participant_list_outputs <- callModule(participantListModule, "pl", data = participant_list, filter = c("age", "Age"))
} else if (input$filter_choice == "Treatment") {
participant_list_outputs <- callModule(participantListModule, "pl", data = participant_list, filter = c("treatment", "Treatment"))
} else if (input$filter_choice == "Hospital") {
participant_list_outputs <- callModule(participantListModule, "pl", data = participant_list, filter = c("clinic_name", "Age"))
}
})
siteid <- reactive({
participant_list_outputs$filtered_data()$siteid
})
# Defining propensity
propensity <- reactive({
participant_list_outputs$filtered_data()$propensity
})
# First tab -------
# Bar plot for cohort 1
site_plot1 <- callModule(barplotFilter, "barplot_cohort1", values = siteid, selection_type = "many", fillOpacity.hover := 0.8)
# Density plot for cohort 1
density_plot1 <- callModule(densityFilter, "density_cohort1", values = propensity, label = "Select Risk Range")
# Tooltip that appears when browsing over the barplot
site_tooltip <- function(x) {
if(is.null(x)) return(NULL)
if(x$clicked == 0) {
return("Click to Add Clinic to Cohort")
}
if(x$clicked == 1) {
return("Click to Remove Clinic from Cohort")
}
}
# Barplot for cohort 1
site_plot1$barplot %>%
add_axis("x", title = "Clinic ID") %>% # X axis
add_axis("y", title = "Number of Participants") %>% # Y axis
add_tooltip(site_tooltip, on = "hover") %>% # Tooltip
set_options(width = "auto") %>% # Options
bind_shiny("barplot1") # Embedding ggvis in a shiny app
# Density plot for cohort 1
density_plot1$density %>%
add_axis("x", title = "Risk of Asthma Exacerbation") %>% # X axis
set_options(width = "auto") %>% # Options
bind_shiny("density1") # Embedding ggvis in a shiny app
# Bar plot for cohort 2
site_plot2 <- callModule(barplotFilter, "barplot_cohort2", values = siteid, selection_type = "many", fillOpacity.hover := 0.8)
# Density plot for cohort 2
density_plot2 <- callModule(densityFilter, "density_cohort2", values = propensity, label = "Select Risk Range")
site_plot2$barplot %>% set_options(width = "auto") %>%
add_axis("x", title = "Clinic ID") %>%
add_axis("y", title = "Number of Participants") %>%
add_tooltip(site_tooltip, on = "hover") %>%
set_options(width = "auto") %>%
bind_shiny("barplot2")
density_plot2$density %>%
set_options(width = "auto") %>%
add_axis("x", title = "Risk of Asthma Exacerbation") %>%
bind_shiny("density2")
cohort1_filters <- reactive({
cohort_sites <- site_plot1$clicked()
cohort_range <- density_plot1$x_range()
cohort_date_range <- input$date1
if(is.null(cohort_sites)) {
cohort_sites <- sort(unique(participant_list$siteid))
}
if(length(cohort_sites) == 0) {
cohort_sites <- sort(unique(participant_list$siteid))
}
if(is.null(cohort_range)) {
cohort_range <- c(0, 1)
}
list(sites = cohort_sites, range = cohort_range, date = cohort_date_range)
})
cohort1 <- reactive({
d <- participant_list_outputs$filtered_data()
filters <- cohort1_filters()
cohort_sites <- filters$sites
cohort_range <- filters$range
cohort_date_range <- filters$date
out <- d %>% filter(siteid %in% cohort_sites, propensity >= cohort_range[1], propensity <= cohort_range[2], date_of_inclusion >= cohort_date_range[1], date_of_inclusion <= cohort_date_range[2])
if(nrow(out) == 0) {
return(d)
} else {
return(out)
}
})
output$cohort1_summary <- renderUI({
d <- cohort1()
filters <- cohort1_filters()
n <- nrow(d)
HTML(
paste0("<h3><strong style=\"font-size: 125%;\">", n, "</strong> Participants in Current Selection:</h3><p>Clinic ID in ",
paste(filters$sites, collapse = ","),
"</br> Risk of asthma exacerbation between ", filters$range[1], " and ", filters$range[2],
"</br> Date of Inclusion between ", as.Date(filters$date[1]), " and ", as.Date(filters$date[2]),
"</p>"
)
)
})
cohort2_filters <- reactive({
cohort_sites <- site_plot2$clicked()
cohort_range <- density_plot2$x_range()
cohort_date_range <- input$date2
if(is.null(cohort_sites)) {
cohort_sites <- sort(unique(participant_list$siteid))
}
if(length(cohort_sites) == 0) {
cohort_sites <- sort(unique(participant_list$siteid))
}
if(is.null(cohort_range)) {
cohort_range <- c(0, 1)
}
list(sites = cohort_sites, range = cohort_range, date = cohort_date_range)
})
cohort2 <- reactive({
d <- participant_list_outputs$filtered_data()
filters <- cohort2_filters()
cohort_sites <- filters$sites
cohort_range <- filters$range
cohort_date_range <- filters$date
out <- d %>% filter(siteid %in% cohort_sites, propensity >= cohort_range[1], propensity <= cohort_range[2], date_of_inclusion >= cohort_date_range[1], date_of_inclusion <= cohort_date_range[2])
if(nrow(out) == 0) {
return(d)
} else {
return(out)
}
})
output$cohort2_summary <- renderText({
d <- cohort2()
filters <- cohort2_filters()
n <- nrow(d)
HTML(
paste0("<h3><strong style=\"font-size: 125%;\">", n, "</strong> Participants in Current Selection:</h3><p>Clinic ID in ",
paste(filters$sites, collapse = ","),
"</br> Risk of asthma exacerbation ", filters$range[1], " and ", filters$range[2],
"</br> Date of Inclusion between ", as.Date(filters$date[1]), " and ", as.Date(filters$date[2]),
"</p>"
)
)
})
cohort1 %>% ggvis(~gender) %>%
layer_bars(fill := "B22222", fillOpacity := 0.8, fillOpacity.hover := 0.9, strokeWidth := 2) %>%
add_axis("x", title = "Gender") %>%
add_axis("y", title = "Number of Participants") %>%
set_options(width = "auto") %>%
bind_shiny("sex_cohort1")
cohort2 %>% ggvis(~gender) %>%
layer_bars(fill := "B22222", fillOpacity := 0.8, fillOpacity.hover := 0.9, strokeWidth := 2) %>%
add_axis("x", title = "Gender") %>%
add_axis("y", title = "Number of Participants") %>%
set_options(width = "auto") %>%
bind_shiny("sex_cohort2")
cohort1 %>% ggvis(~age_bands) %>% layer_bars() %>%
add_axis("x", title = "", properties = axis_props(labels = list(angle = 45, align = "left", baseline = "middle"))) %>%
add_axis("y", title = "Number of Participants") %>%
set_options(width = "auto") %>%
bind_shiny("age_cohort1")
cohort2 %>% ggvis(~age_bands) %>% layer_bars() %>%
add_axis("x", title = "", properties = axis_props(labels = list(angle = 45, align = "left", baseline = "middle"))) %>%
add_axis("y", title = "Number of Participants") %>%
set_options(width = "auto") %>%
bind_shiny("age_cohort2")
cross_tab_data1 <- reactive({
d <- cohort1()
var1 <- input$cross_var11
var2 <- input$cross_var12
if(is.null(var1) || is.null(var2) || nrow(d) == 0) {
return(data.frame(x = c(0, 0, 1, 1), x2 = c(1, 1, 2, 2), y = c(0, 1, 0, 1), y2 = c(1, 2, 1, 2), values = c(0, 0, 0, 0)))
}
vars <- name_map[c(var1, var2)]
d <- d[, vars]
t <- table(factor(d[,1], levels = c(0, 1)), factor(d[,2], levels = c(0, 1)))
d <- data.frame(x = c(0, 0, 1, 1), x2 = c(1, 1, 2, 2), y = c(0, 1, 0, 1), y2 = c(1, 2, 1, 2))
d$values <- c(t[2], t[4], t[1], t[3])
d
})
cross_tab_tt <- function(x) {
if(is.null(x)) return(NULL)
link1 <- if (x$x == 0) {
"with "
} else {
"without "
}
link2 <- if (x$y == 1) {
"with "
} else {
"without "
}
paste0(x$values, " Participants ", link1, input$cross_var11, " and ", link2, input$cross_var12)
}
observe({
cross_tab_data1 %>% cross_tab() %>%
add_tooltip(cross_tab_tt) %>%
set_options(width = "auto") %>%
hide_legend("fill") %>%
bind_shiny("cross_tab1")
})
output$cohort1_measures <- renderUI({
d <- cohort1()
values <- lapply(d[, cross_var_choices[1:4]], perc_equal_to, a = 1)
fluidRow(
column(3,
valueBox(values[[1]], cross_var_choices[1], width = NULL)
),
column(3,
valueBox(values[[2]], cross_var_choices[2], width = NULL)
),
column(3,
valueBox(values[[3]], cross_var_choices[3], width = NULL)
),
column(3,
valueBox(values[[4]], cross_var_choices[4], width = NULL)
)
)
})
cross_tab_data2 <- reactive({
d <- cohort2()
var1 <- input$cross_var21
var2 <- input$cross_var22
if(is.null(var1) || is.null(var2) || nrow(d) == 0) {
return(data.frame(x = c(0, 0, 1, 1), x2 = c(1, 1, 2, 2), y = c(0, 1, 0, 1), y2 = c(1, 2, 1, 2), values = c(0, 0, 0, 0)))
}
vars <- name_map[c(var1, var2)]
d <- d[, vars]
t <- table(d[,1], d[,2])
d <- data.frame(x = c(0, 0, 1, 1), x2 = c(1, 1, 2, 2), y = c(0, 1, 0, 1), y2 = c(1, 2, 1, 2))
d$values <- c(t[2], t[4], t[1], t[3])
d
})
observe({
cross_tab_data2 %>% cross_tab() %>%
add_tooltip(cross_tab_tt) %>%
set_options(width = "auto") %>%
hide_legend("fill") %>%
bind_shiny("cross_tab2")
})
output$cohort2_measures <- renderUI({
d <- cohort2()
values <- lapply(d[, cross_var_choices[1:4]], perc_equal_to, a = 1)
fluidRow(
column(3,
valueBox(values[[1]], cross_var_choices[1], width = NULL)
),
column(3,
valueBox(values[[2]], cross_var_choices[2], width = NULL)
),
column(3,
valueBox(values[[3]], cross_var_choices[3], width = NULL)
),
column(3,
valueBox(values[[4]], cross_var_choices[4], width = NULL)
)
)
})
bp_density1 <- reactive({
cohort1() %>% group_by(gender)
})
bp_density2 <- reactive({
cohort2() %>% group_by(gender)
})
bp_density1 %>% ggvis(~bp_diastolic) %>% layer_densities(fill = ~gender, stroke = ~gender) %>%
set_options(width = "auto") %>%
bind_shiny("bp_density1")
bp_density1 %>% ggvis(~gender, ~resp) %>% layer_boxplots(fill = ~gender, stroke = ~gender, fillOpacity := 0.8) %>%
set_options(width = "auto") %>%
bind_shiny("resp_box1")
bp_density2 %>% ggvis(~bp_diastolic) %>% layer_densities(fill = ~gender, stroke = ~gender) %>%
set_options(width = "auto") %>%
bind_shiny("bp_density2")
bp_density2 %>% ggvis(~gender, ~resp) %>% layer_boxplots(fill = ~gender, stroke = ~gender, fillOpacity := 0.8) %>%
set_options(width = "auto") %>%
bind_shiny("resp_box2")
cohort1 %>% ggvis(~treatment) %>% layer_bars(fill = ~treatment) %>%
set_options(width = "auto") %>%
bind_shiny("treatment_bars1")
cohort2 %>% ggvis(~treatment) %>% layer_bars(fill = ~treatment) %>%
set_options(width = "auto") %>%
bind_shiny("treatment_bars2")
cohort1 %>% ggvis(~height, ~weight) %>% layer_points(fill = ~treatment) %>%
set_options(width = "auto") %>%
bind_shiny("treatment_points1")
cohort2 %>% ggvis(~height, ~weight) %>% layer_points(fill = ~treatment) %>%
set_options(width = "auto") %>%
bind_shiny("treatment_points2")
observe({
input$report
if(input$report > 0) {
updateNavbarPage(session, inputId = "navbar", selected = "Report")
}
})
output$pdf <- renderText({
return(paste('<iframe style="height:900px; width:100%" src="report.pdf"></iframe>', sep = ""))
})
}