-
Notifications
You must be signed in to change notification settings - Fork 1
/
boxly.R
256 lines (232 loc) · 8.28 KB
/
boxly.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
# Copyright (c) 2023 Merck & Co., Inc., Rahway, NJ, USA and its affiliates.
# All rights reserved.
#
# This file is part of the boxly program.
#
# boxly is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
#' Create an interactive box plot
#'
#' @param outdata An `outdata` object created from `prepare_ae_forestly()`.
#' @param color Color for box plot.
#' @param hover_summary_var A character vector of statistics to be displayed
#' on hover label of box.
#' @param hover_outlier_label A character vector of hover label for outlier.
#' A label from an input data is used if `NA` for a variable is specified.
#' @param x_label x-axis label.
#' @param y_label y-axis label.
#' @param heading_select_list Select list menu label.
#' @param heading_summary_table Summary table label.
#'
#' @return Interactive box plot.
#'
#' @importFrom ggplot2 ggplot aes geom_point position_jitterdodge
#' scale_color_manual ylab xlab theme_bw .data
#' @importFrom plotly config layout add_trace ggplotly
#' @importFrom stats reshape
#'
#' @export
#'
#' @examples
#' # Only run this example in interactive R sessions
#' if (interactive()) {
#' library(metalite)
#'
#' meta_boxly(
#' boxly_adsl,
#' boxly_adlb,
#' population_term = "apat",
#' observation_term = "wk12"
#' ) |>
#' prepare_boxly() |>
#' boxly()
#' }
boxly <- function(outdata,
color = NULL,
hover_summary_var = c("n", "min", "q1", "median", "mean", "q3", "max"),
hover_outlier_label = c("Participant ID", "Parameter value"),
x_label = "Visit",
y_label = "Change",
heading_select_list = "Lab parameter",
heading_summary_table = "Number of Participants") {
x_var <- outdata$x_var
y_var <- outdata$y_var
id_var <- outdata$id_var
group_var <- outdata$group_var
param_var <- outdata$param_var
hover_var_outlier <- outdata$hover_var_outlier
n_group <- length(unique(outdata$plotds[[group_var]]))
tbl <- outdata$plotds |> as.data.frame()
tbl$x <- as.numeric(tbl[[x_var]])
tbl$y <- tbl[[y_var]]
tbl$group <- tbl[[group_var]]
tbl$param <- tbl[[param_var]]
# prepare hover label in hover_summary_var
stat_var_label <- data.frame(
stat_var = c("n", "min", "q1", "median", "mean", "q3", "max"),
label = c("N: ", "Min: ", "Q1: ", "Median: ", "Mean: ", "Q3: ", "Max: "),
value = c("tbl$n", "tbl$min", "tbl$q1", "tbl$median", "tbl$mean", "tbl$q3", "tbl$max"),
stringsAsFactors = FALSE
)
tbl$label <- NULL
for (var in hover_summary_var) {
if (var %in% hover_summary_var) {
tbl$label <- paste0(
tbl$label,
stat_var_label$label[stat_var_label$stat_var == var],
eval(parse(text = stat_var_label$value[stat_var_label$stat_var == var])),
"\n"
)
}
}
# paste multiple hover_outlier_labels
# Check length of variables and labels
if (length(hover_outlier_label) > 0) {
if (!length(hover_var_outlier) == length(hover_outlier_label)) {
warning("The Length of hover labels should be same as that of hover variables.")
}
}
# Set labels
label <- vapply(tbl, function(x) {
if (is.null(attr(x, "label"))) {
return(NA_character_)
} else {
attr(x, "label")
}
}, FUN.VALUE = character(1))
listing_label <- ifelse(is.na(label), names(tbl), label)
tbl$text <- apply(tbl, 1, function(x) {
text <- NULL
var <- NULL
if (!is.na(x[["outlier"]])) {
for (i in seq(hover_var_outlier)) {
var <- hover_var_outlier[i]
if (!is.null(hover_outlier_label)) {
label <- ifelse(!is.na(hover_outlier_label[i]), hover_outlier_label[i], listing_label[var])
} else {
label <- listing_label[var]
}
text <- ifelse(i == 1,
paste0(text, label, ": ", x[[var]]),
paste0(text, "\n", label, ": ", x[[var]])
)
}
} else {
text <- NA
}
return(text)
})
# implement color
if (is.null(color)) {
color_pal <- c("#00857C", "#6ECEB2", "#BFED33", "#FFF063", "#0C2340", "#5450E4")
color <- c("#66203A", rep(color_pal, length.out = n_group - 1))
} else {
color <- rep(color, length.out = n_group)
}
# input data set for bar
bar <- tbl[, c("group", "param", "min", "max", "label", "x")]
bar$range <- bar$max - bar$min
bar <- unique.data.frame(bar)
shareddata_id <- uuid::UUIDgenerate()
bar <- crosstalk::SharedData$new(bar, key = ~param, group = shareddata_id)
# create 2 shareddata, 1 all data + 1 outlier data only
box_all <- crosstalk::SharedData$new(tbl, key = ~param, group = shareddata_id)
box_outlier <- crosstalk::SharedData$new(tbl |> subset(is.na(tbl$outlier)), key = ~param, group = shareddata_id)
# get the summary of subjects counts
cnt <- tbl[!duplicated(tbl[, c("param", "x", "group", "n")]), c("param", "x", "group", "n")] |>
reshape(timevar = "x", idvar = c("param", "group"), v.names = "n", direction = "wide")
names(cnt) <- c("param", "Treatment Group", as.character(unique(tbl$AVISITN)))
row.names(cnt) <- NULL
cnt <- crosstalk::SharedData$new(cnt, key = ~param, group = shareddata_id)
# Get first parameter name
default_param <- as.character(unique(tbl$param)[1])
random_id <- paste0("filter_param_", uuid::UUIDgenerate(), "|", default_param)
# get the select list of parameters
select_list <- crosstalk::filter_select(
id = random_id,
label = heading_select_list,
sharedData = box_all,
group = ~param,
multiple = FALSE
)
# get the interactive box plot
p <- ggplot(
box_all,
aes(
x = .data$x,
y = .data$outlier,
color = .data$group,
group = .data$group,
text = .data$text
)
) +
geom_point(position = position_jitterdodge(jitter.width = 0), show.legend = FALSE) +
scale_color_manual(values = color) +
ylab(y_label) +
xlab(x_label) +
theme_bw()
# combine into interactive plots & tables
ans <- htmltools::div(
select_list,
# slider_bar,
ggplotly(p, tooltip = "text", dynamicTicks = TRUE) |>
add_trace(
data = box_outlier, x = ~x, y = ~y, type = "box", boxpoints = FALSE, color = ~group,
opacity = 1, colors = color,
hoverinfo = "none",
legendgroup = "group", showlegend = FALSE
) |>
add_trace(
data = bar, x = ~x, y = ~range, base = ~min, type = "bar", color = ~group,
opacity = 0, text = ~label, colors = color,
hovertemplate = paste("%{text}<br>", "<extra></extra>"),
legendgroup = "group", showlegend = FALSE
) |>
layout(
xaxis = list(
ticktext = as.character(unique(tbl[[x_var]])),
tickvals = as.numeric(unique(tbl[[x_var]])),
tickmode = "array"
)
) |>
layout(
barmode = "group",
boxmode = "group"
) |>
config(displayModeBar = FALSE) |>
htmlwidgets::onRender("function(el,x){el.on('plotly_legendclick', function(){ return false; })}"),
heading_summary_table,
DT::datatable(cnt,
options = list(columnDefs = list(list(visible = FALSE, targets = c(0)))),
rownames = FALSE
)
)
offline <- TRUE
brew::brew(system.file("js/filter_default.js", package = "boxly"),
output = file.path(tempdir(), "filter_default.js")
)
paste(readLines(file.path(tempdir(), "filter_default.js")), collapse = "\n")
htmltools::browsable(htmltools::tagList(
htmltools::htmlDependency(
# name and version: doesn't really matter, can be anything
"default-parameter", "0.1.0",
# src: path to directory containing the script (e.g., the current directory '.')
src = tempdir(),
# script: filename of script to include
script = "filter_default.js",
# Exclude all other files in src directory
all_files = FALSE
),
ans
))
}