-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathtab_tlg.R
More file actions
316 lines (276 loc) · 10 KB
/
tab_tlg.R
File metadata and controls
316 lines (276 loc) · 10 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
#' Tab handling Tables, Listings and Graphs.
#'
#' @details
#' Tab provides the user with a selection of TLGs and allows the display and customization of
#' various tables, lists and graphs. The definitions table as well as all customization options
#' are based on `tlg.yaml` file in the root directory of the application. This module calls on
#' `tlg_module`s for each TLG in submitted order, creating a coprehensive UI for all visualizations.
#'
#' Read more in the contributing guide.
#'
#' @param id ID of the module
#' @param data ADNCA data object, processed and mapped.
#' Parses TLG definitions from the yaml file, holds all definitions.
.TLG_DEFINITIONS <- {
defs <- yaml::read_yaml(system.file("shiny/tlg.yaml", package = "aNCA"))
defs <- purrr::imap(defs, function(opt_def, opt_id) {
if ("template" %in% names(opt_def)) {
template_def <- defs[[opt_def$template]]
for (d in names(opt_def)) {
if (d == "template") next
if (d == "options") {
for (o in names(opt_def$options)) {
template_def$options[[o]] <- opt_def$options[[o]]
}
} else {
template_def[[d]] <- opt_def[[d]]
}
}
opt_def <- template_def
}
opt_def
}) %>%
setNames(names(defs))
}
js_close_button <- tags$button(
type = "button",
onclick = "$(this).closest('.modal').modal('hide');",
`aria-label` = "Close",
# Style updated for color and size
style = "color: white; border: none; background: transparent; font-size: 1.2em; padding: 0;",
icon("times")
)
tab_tlg_ui <- function(id) {
ns <- NS(id)
navset_pill(
id = ns("tlg_tabs"),
nav_panel(
"Order details",
card(
style = "margin-top: 1em;",
div(
actionButton(ns("add_tlg"), "Add TLG"),
actionButton(ns("remove_tlg"), "Remove TLG"),
actionButton(ns("submit_tlg_order"), "Submit Order Details", class = "btn-primary")
)
),
card(reactable_ui(ns("selected_tlg_table"))),
),
nav_panel("Tables", "To be added"),
nav_panel("Listings", uiOutput(ns("listings"), class = "tlg-module"), value = "Listings"),
nav_panel("Graphs", uiOutput(ns("graphs"), class = "tlg-module"), value = "Graphs"),
# disable loader for initial empty UI render #
footer = tags$style(
id = "tlg-load-hide", HTML(paste0(".tlg-module .load-container {opacity: 0;}"))
)
)
}
tab_tlg_server <- function(id, data) {
moduleServer(id, function(input, output, session) {
log_trace("{session$ns(id)}: Attaching server.")
#' Load TLG orders definitions
tlg_order <- reactiveVal({
purrr::map_dfr(.TLG_DEFINITIONS, ~ dplyr::tibble(
Selection = .x$is_default,
Type = .x$type,
Dataset = case_when(
.x$dataset == "ADNCA" ~ "PK Concentrations",
.x$dataset == "ADPP" ~ "PK Parameters",
TRUE ~ .x$dataset
),
PKid = .x$pkid,
Output = paste0("<a href='", .x$link, "' target='_blank'>", .x$description, "</a>"),
Label = .x$label,
Description = .x$description,
Condition = .x$condition,
Footnote = NA_character_,
Stratification = NA_character_,
Comment = NA_character_
)) %>%
dplyr::mutate(id = dplyr::row_number(), .before = dplyr::everything())
})
# Based on the TLG list conditions for data() define the preselected rows in $Selection
observeEvent(list(tlg_order(), data()), {
req(data())
# Unparsable conditions will be ignored
new_tlg_order <- tryCatch({
tlg_order() %>%
mutate(
Selection = case_when(
Condition == "" | is.na(Condition) | is.null(Condition) ~ Selection,
any(unique(toupper(data()$conc$data$PCSPEC)) %in% Condition) ~ TRUE,
TRUE ~ Selection
)
)
}, error = function(e) {
tlg_order()
})
tlg_order(new_tlg_order)
})
displayed_order <- reactive({
dplyr::filter(tlg_order(), Selection) %>%
dplyr::select(-id, -Selection)
}) %>%
bindEvent(data(), input$confirm_add_tlg, input$remove_tlg)
selected_tlg_state <- reactable_server(
"selected_tlg_table",
displayed_order,
download_buttons = c("csv", "xlsx"),
groupBy = c("Type", "Dataset"),
defaultExpanded = TRUE,
wrap = TRUE,
selection = "multiple",
editable = c("Footnote", "Stratification", "Condition", "Comment"),
columns = function(df) {
define_cols(df, overrides = list(Output = colDef(html = TRUE)))
}
)
observeEvent(selected_tlg_state()$edit(), {
info <- selected_tlg_state()$edit()
new_tlg_order <- tlg_order()
new_tlg_order[new_tlg_order$Selection, ][info$row, info$column] <- info$value
tlg_order(new_tlg_order)
})
# Show modal when the add_tlg button is pressed
observeEvent(input$add_tlg, {
showModal(modalDialog(
title = div(
"Add TLGs to Order",
js_close_button,
style = "position: relative;"
),
reactable_ui(session$ns("modal_tlg_table")),
footer = tagList(
modalButton("Close"),
actionButton(session$ns("confirm_add_tlg"), "Add TLGs to Order")
),
size = "l"
))
})
modal_tlg_state <- reactable_server(
"modal_tlg_table",
reactive({
dplyr::filter(tlg_order(), !Selection) %>%
dplyr::select(-id, -Selection, -Footnote, -Stratification, -Condition, -Comment)
}),
download_buttons = c("csv", "xlsx"),
groupBy = c("Type", "Dataset"),
wrap = TRUE,
selection = "multiple",
defaultExpanded = TRUE,
width = "775px", # fit to the modal width
columns = function(df) {
define_cols(df, overrides = list(Output = colDef(html = TRUE)))
}
)
# Update the Selection column when the confirm_add_tlg button is pressed
observeEvent(input$confirm_add_tlg, {
selected_rows <- modal_tlg_state()$selected
if (length(selected_rows) > 0) {
tlg_order_data <- tlg_order()
tlg_order_data$Selection[!tlg_order_data$Selection][selected_rows] <- TRUE
tlg_order(tlg_order_data)
}
removeModal()
})
# Update the Selection column when the remove_tlg button is pressed
observeEvent(input$remove_tlg, {
selected_rows <- selected_tlg_state()$selected
if (length(selected_rows) > 0) {
tlg_order_data <- tlg_order()
tlg_order_data$Selection[tlg_order_data$Selection][selected_rows] <- FALSE
tlg_order(tlg_order_data)
}
})
# Toggle submit button depending on whether the data is available #
observeEvent(data(), ignoreInit = FALSE, ignoreNULL = FALSE, {
shinyjs::toggleState("submit_tlg_order", !is.null(data()$conc$data))
shinyjs::toggleState("submit_tlg_order_alt", !is.null(data()$conc$data))
})
#' change tab to first populated tab
#' for mysterious reasons nav_select() and updateTabsetPanel() were not working,
#' so solved this using JavaScript
observeEvent(list(input$submit_tlg_order), ignoreInit = TRUE, {
tab_to_switch <- pull(tlg_order_filtered()[1, "Type"]) %>% paste0("s")
shinyjs::runjs(
paste0("
// change the tab to graphs //
$(`#", session$ns("tlg_tabs"), " a[data-value='", tab_to_switch, "']`)[0].click();
// enable spinner, as it was disabled for initial empty UI render //
setTimeout(function() {
$('#tlg-load-hide').remove();
}, 500);
")
)
})
# Submit the TLG order, filter selected TLGs
tlg_order_filtered <- reactive({
req(data())
print(tlg_order())
tlg_order_filt <- tlg_order()[tlg_order()$Selection, ]
log_debug("Submitted TLGs:\n", paste0("* ", tlg_order_filt$Description, collapse = "\n"))
tlg_order_filt
}) %>%
bindEvent(c(input$submit_tlg_order))
# Create and render Graph interface and modules
output$graphs <- renderUI({
req(tlg_order_filtered())
tlg_order_graphs <- filter(tlg_order_filtered(), Type == "Graph") %>%
select("id") %>%
pull()
panels <- lapply(tlg_order_graphs, function(g_id) {
graph_ui <- {
g_def <- .TLG_DEFINITIONS[[g_id]]
module_id <- paste0(
g_id,
paste0(sample(c(letters, 0:9), 5, replace = TRUE), collapse = "")
)
if (exists(g_def$fun)) {
tlg_module_server(module_id, data, "graph", get(g_def$fun), g_def$options)
tlg_module_ui(session$ns(module_id), "graph", g_def$options)
} else {
tags$div("Graph not implemented yet")
}
}
nav_panel(g_def$label, graph_ui)
})
panels$"widths" <- c(2, 10)
do.call(navset_pill_list, panels)
})
output$listings <- renderUI({
req(tlg_order_filtered())
tlg_order_listings <- filter(tlg_order_filtered(), Type == "Listing") %>%
select("id") %>%
pull()
if (!requireNamespace("rlistings", quietly = TRUE)) {
panels <- list(nav_panel(
"Listings",
tags$div(
class = "alert alert-warning",
"Package 'rlistings' is not installed. Install it to view listings:",
tags$code("install.packages('rlistings')")
)
))
} else {
panels <- lapply(tlg_order_listings, function(g_id) {
list_ui <- {
g_def <- .TLG_DEFINITIONS[[g_id]]
module_id <- paste0(
g_id,
paste0(sample(c(letters, 0:9), 5, replace = TRUE), collapse = "")
)
if (exists(g_def$fun)) {
tlg_module_server(module_id, data, "listing", get(g_def$fun), g_def$options)
tlg_module_ui(session$ns(module_id), "listing", g_def$options)
} else {
tags$div("Listing not implemented yet")
}
}
nav_panel(g_def$label, list_ui)
})
}
panels$"widths" <- c(2, 10)
do.call(navset_pill_list, panels)
})
})
}