-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathADLB_Task3.R
More file actions
360 lines (291 loc) · 13.9 KB
/
ADLB_Task3.R
File metadata and controls
360 lines (291 loc) · 13.9 KB
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
library(shiny)
library(readxl)
library(dplyr)
library(ggplot2)
library(plotly)
library(tidyverse)
file_path <- 'adlb.xlsx'
# Define UI for application that draws a histogram
ui <- fluidPage(
# Application title
titlePanel("Laboratory parameters - summary"),
# Main panel
fluidRow(
column(12,
tabsetPanel(
id = "tabs",
# Tab panel to display box plot
tabPanel(
"BoxPlot",
# Description
div(
style = "margin-bottom: 20px; padding: 10px; background-color: #f9f9f9; border-radius: 5px;",
h4("Explore the change and distribution of laboratory parameters during the course of trial."),
p("Use the controls below to filter and customize the time-series box plot.")
),
# Customization filters and controls
fluidRow(
column(2,
selectInput("boxparam", "Parameter",
choices = c("BASO", "EOS", "HCT", "HGB", "LYM", "MCH", "MCHC", "MCV", "MONO", "PLAT", "RBC", "WBC"),
selected = "HGB")
),
column(3,
selectInput("boxtrtarm", "Treatment Arm",
choices = c("Placebo", "Low dose", "High dose"),
selected = "Placebo")
)),
# Time-series box pot
div(
style = "margin-top: 30px;",
plotlyOutput("boxPlot", height = "600px", width = "100%")
)
),
# Tab panel to display dumbbell plot
tabPanel(
"Dumbbell plot",
# Description
div(
style = "margin-bottom: 20px; padding: 10px; background-color: #f9f9f9; border-radius: 5px;",
h4("Explore the change of laboratory parameters during the course of trial between specific time periods across different treatment arms."),
p("Use the controls below to filter and customize the dumbbell plot."),
tags$a(href="https://plotly.com/~nimal/1/", "Overview plot")
),
# Customization filters and controls
fluidRow(
column(2,
selectInput("dpparam", "Parameter",
choices = c("BASO", "EOS", "HCT", "HGB", "LYM", "MCH", "MCHC", "MCV", "MONO", "PLAT", "RBC", "WBC"),
selected = "HGB")
),
column(3,
selectInput("start", "Start",
choices = c("Baseline","Week 2","Week 4","Week 6","Week 8","Week 12","Week 16","Week 20","Week 24","End of Treatment","Week 26"),
selected = "Baseline")
),
column(3,
conditionalPanel(
condition = 'input.start == "Baseline"',
selectInput("end", "End",
choices = c("Week 2","Week 4","Week 6","Week 8","Week 12","Week 16","Week 20","Week 24","End of Treatment","Week 26"),
selected = "Week 26")),
conditionalPanel(
condition = 'input.start == "Week 2"',
selectInput("end", "End",
choices = c("Week 4","Week 6","Week 8","Week 12","Week 16","Week 20","Week 24","End of Treatment","Week 26"),
selected = "Week 26")),
conditionalPanel(
condition = 'input.start == "Week 4"',
selectInput("end", "End",
choices = c("Week 6","Week 8","Week 12","Week 16","Week 20","Week 24","End of Treatment","Week 26"),
selected = "Week 26")),
conditionalPanel(
condition = 'input.start == "Week 6"',
selectInput("end", "End",
choices = c("Week 8","Week 12","Week 16","Week 20","Week 24","End of Treatment","Week 26"),
selected = "Week 26")),
conditionalPanel(
condition = 'input.start == "Week 8"',
selectInput("end", "End",
choices = c("Week 12","Week 16","Week 20","Week 24","End of Treatment","Week 26"),
selected = "Week 26")),
conditionalPanel(
condition = 'input.start == "Week 12"',
selectInput("end", "End",
choices = c("Week 16","Week 20","Week 24","End of Treatment","Week 26"),
selected = "Week 26")),
conditionalPanel(
condition = 'input.start == "Week 16"',
selectInput("end", "End",
choices = c("Week 20","Week 24","End of Treatment","Week 26"),
selected = "Week 26")),
conditionalPanel(
condition = 'input.start == "Week 20"',
selectInput("end", "End",
choices = c("Week 24","End of Treatment","Week 26"),
selected = "Week 26")),
conditionalPanel(
condition = 'input.start == "Week 24"',
selectInput("end", "End",
choices = c("End of Treatment","Week 26"),
selected = "Week 26"))
)
),
div(
style = "margin-top: 30px;",
plotlyOutput("dbPlot", height = "100%", width = "100%")
)
),
tabPanel(
"Table",
# Description
div(
style = "margin-bottom: 20px; padding: 10px; background-color: #f9f9f9; border-radius: 5px;",
h4("Table of minimum and maximum values for each LBNRIND flag"),
p("Ambiguous values have been highlighted in red.")
),
# Table
div(
style = "margin-top: 30px;",
imageOutput("table", height = "100%", width = "100%")
)
)
)
)
)
)
# Define server logic
server <- function(input, output) {
# Read the Excel file
adlb_sheet <- read_excel(file_path, sheet = "adlb")
# Reactive expression to read and process data
reactive_data <- reactive({
# Columns to drop
drops_ad <- c("STUDYID", "PARCAT1", "USUBJID", "TRTP", "TRTPN",
"AEHLGTCD", "LBSEQ")
adlb <- adlb_sheet[, !(names(adlb_sheet) %in% drops_ad)]
# Common data pre-processing
adlb <- adlb |> mutate(TRTA = ifelse(TRTA == "Trt A", "Low dose", TRTA))
adlb <- adlb |> mutate(TRTA = ifelse(TRTA == "Trt B", "High dose", TRTA))
# Common data filtering
norm_param <- adlb |> filter(PARAMN %in% seq(1, 12), !AVISIT %in% c("."))
abnorm_baseline <- adlb |> filter(LBNRIND == "ABNORMAL", ABLFL == "Y")
abnorm_baseline_subs <- unique(abnorm_baseline$SUBJID)
# Box plot data
boxPlot_data <- norm_param |> filter(PARAMCD == input$boxparam, TRTA == input$boxtrtarm)
boxPlot_data$AVISIT <- factor(boxPlot_data$AVISIT, levels=c("Baseline","Week 2","Week 4","Week 6","Week 8","Week 12","Week 16","Week 20","Week 24","End of Treatment","Week 26"))
# Dumbbell plot data
dbPlot_data <- norm_param |> group_by(PARAMCD, AVISIT, TRTA) |> summarise(values = format(round(mean(AVAL), 3))) |> ungroup() |> mutate_at(vars(values), as.numeric)
dbPlot_data <- dbPlot_data |> filter(AVISIT %in% c(input$start, input$end), PARAMCD == input$dpparam)
dbPlot_data <- dbPlot_data |> mutate(AVISIT = ifelse(AVISIT == "End of Treatment", "EOT", AVISIT))
if(input$end == "End of Treatment"){
param_chg <- dbPlot_data %>%
group_by(TRTA) %>%
pivot_wider(names_from = AVISIT, values_from = values) %>%
mutate(chg = EOT - !!sym(input$start)) %>%
mutate(pchg = (chg/!!sym(input$start)) * 100)
}
else{
param_chg <- dbPlot_data %>%
group_by(TRTA) %>%
pivot_wider(names_from = AVISIT, values_from = values) %>%
mutate(chg = !!sym(input$end) - !!sym(input$start)) %>%
mutate(pchg = (chg/!!sym(input$start)) * 100)
}
param_chg$chg <- round(param_chg$chg, 3)
param_chg$pchg <- round(param_chg$pchg, 2)
dbPlot_data$chg <- c(param_chg$chg, param_chg$chg)
dbPlot_data$pchg <- c(param_chg$pchg, param_chg$pchg)
dbPlot_data <- dbPlot_data |> mutate(AVISIT = ifelse(AVISIT == "EOT", "End of Treatment", AVISIT))
# Calculate mean values for geom_line
bp_mean <- norm_param |> filter(PARAMCD == input$boxparam, TRTA == input$boxtrtarm) |>
group_by(AVISIT) |>
summarise(mean_value = mean(AVAL, na.rm = TRUE)) |>
ungroup()
return(list(boxPlot_data = boxPlot_data, bp_mean = bp_mean, dbPlot_data = dbPlot_data))
})
output$boxPlot <- renderPlotly({
data <- reactive_data()
boxPlot_data <- data$boxPlot_data
bp_mean <- data$bp_mean
options(repr.plot.width =50, repr.plot.height =10)
p <- ggplot(boxPlot_data, aes(x = AVISIT, y = AVAL)) +
geom_boxplot(fill = 'white') +
labs(x = "", y = "",
title = paste("Change of", input$boxparam, "over treatment duration in", input$boxtrta, "treatment group")) +
theme_minimal() +
geom_line(data = bp_mean, aes(x = AVISIT, y = mean_value, group = 1), color = "blue")
if(input$boxparam == "BASO"){
p <- p + geom_hline(yintercept = 0, color = "green", linetype = "dashed", size = 1)
p <- p + geom_hline(yintercept = .2, color = "red", linetype = "dashed", size = 1)
}
if(input$boxparam == "MCV"){
p <- p + geom_hline(yintercept = 80, color = "green", linetype = "dashed", size = 1)
p <- p + geom_hline(yintercept = 100, color = "red", linetype = "dashed", size = 1)
}
if(input$boxparam == "MCH"){
p <- p + geom_hline(yintercept = 1.5, color = "green", linetype = "dashed", size = 1)
p <- p + geom_hline(yintercept = 2.15, color = "red", linetype = "dashed", size = 1)
}
if(input$boxparam == "MCHC"){
p <- p + geom_hline(yintercept = 19, color = "green", linetype = "dashed", size = 1)
}
if(input$boxparam == "WBC"){
p <- p + geom_hline(yintercept = 3.8, color = "green", linetype = "dashed", size = 1)
p <- p + geom_hline(yintercept = 10.7, color = "red", linetype = "dashed", size = 1)
}
if(input$boxparam == "MONO"){
p <- p + geom_hline(yintercept = .12, color = "green", linetype = "dashed", size = 1)
p <- p + geom_hline(yintercept = .92, color = "red", linetype = "dashed", size = 1)
}
if(input$boxparam == "EOS"){
p <- p + geom_hline(yintercept = 0, color = "green", linetype = "dashed", size = 1)
p <- p + geom_hline(yintercept = .57, color = "red", linetype = "dashed", size = 1)
}
if(input$boxparam == "PLAT"){
p <- p + geom_hline(yintercept = 130, color = "green", linetype = "dashed", size = 1)
p <- p + geom_hline(yintercept = 395, color = "red", linetype = "dashed", size = 1)
}
if(input$boxparam == "LYM"){
p <- p + geom_hline(yintercept = .8, color = "green", linetype = "dashed", size = 1)
}
ggplotly(p)
})
output$dbPlot <- renderPlotly({
data <- reactive_data()
dp_data <- data$dbPlot_data
options(repr.plot.width =50, repr.plot.height =10)
p <- ggplot(dp_data, aes(x= values, y= TRTA,
text = case_when(
AVISIT == input$start ~ paste(input$start, "value:", values),
AVISIT == input$end ~ paste(input$end, "value:", values, "<br>Change:", chg, "(", pchg, "%)"))
)) +
geom_line(aes(group = TRTA))+
geom_point(aes(color=AVISIT), size=4) +
labs(y="", x="", title = paste(input$dpparam ,"change - Summary"), colour = "Legend")
if(input$dpparam == "BASO"){
p <- p + geom_vline(xintercept = 0, color = "green", linetype = "dashed", size = 1)
p <- p + geom_vline(xintercept = .2, color = "red", linetype = "dashed", size = 1)
}
if(input$dpparam == "MCV"){
p <- p + geom_vline(xintercept = 80, color = "green", linetype = "dashed", size = 1)
p <- p + geom_vline(xintercept = 100, color = "red", linetype = "dashed", size = 1)
}
if(input$dpparam == "MCH"){
p <- p + geom_vline(xintercept = 1.5, color = "green", linetype = "dashed", size = 1)
p <- p + geom_vline(xintercept = 2.15, color = "red", linetype = "dashed", size = 1)
}
if(input$dpparam == "MCHC"){
p <- p + geom_vline(xintercept = 19, color = "green", linetype = "dashed", size = 1)
}
if(input$dpparam == "WBC"){
p <- p + geom_vline(xintercept = 3.8, color = "green", linetype = "dashed", size = 1)
p <- p + geom_vline(xintercept = 10.7, color = "red", linetype = "dashed", size = 1)
}
if(input$dpparam == "MONO"){
p <- p + geom_vline(xintercept = .12, color = "green", linetype = "dashed", size = 1)
p <- p + geom_vline(xintercept = .92, color = "red", linetype = "dashed", size = 1)
}
if(input$dpparam == "EOS"){
p <- p + geom_vline(xintercept = 0, color = "green", linetype = "dashed", size = 1)
p <- p + geom_vline(xintercept = .57, color = "red", linetype = "dashed", size = 1)
}
if(input$dpparam == "PLAT"){
p <- p + geom_vline(xintercept = 130, color = "green", linetype = "dashed", size = 1)
p <- p + geom_vline(xintercept = 395, color = "red", linetype = "dashed", size = 1)
}
if(input$dpparam == "LYM"){
p <- p + geom_vline(xintercept = .8, color = "green", linetype = "dashed", size = 1)
}
ggplotly(p, tooltip = "text")
})
# A plot of fixed size
output$table <- renderImage({
img_file <- './table.png'
# Return a list
list(src = img_file,
alt = "Table containing min. and max. values for low, normal and high flags")}
, deleteFile = FALSE)
}
# Run the application
shinyApp(ui = ui, server = server)