| 1 |
#' `teal` module: Stack plots of variables and show association with reference variable |
|
| 2 |
#' |
|
| 3 |
#' Module provides functionality for visualizing the distribution of variables and |
|
| 4 |
#' their association with a reference variable. |
|
| 5 |
#' It supports configuring the appearance of the plots, including themes and whether to show associations. |
|
| 6 |
#' |
|
| 7 |
#' |
|
| 8 |
#' @note For more examples, please see the vignette "Using association plot" via |
|
| 9 |
#' `vignette("using-association-plot", package = "teal.modules.general")`.
|
|
| 10 |
#' |
|
| 11 |
#' @inheritParams teal::module |
|
| 12 |
#' @inheritParams shared_params |
|
| 13 |
#' @param ref (`data_extract_spec` or `list` of multiple `data_extract_spec`) |
|
| 14 |
#' Reference variable, must accepts a `data_extract_spec` with `select_spec(multiple = FALSE)` |
|
| 15 |
#' to ensure single selection option. |
|
| 16 |
#' @param vars (`data_extract_spec` or `list` of multiple `data_extract_spec`) |
|
| 17 |
#' Variables to be associated with the reference variable. |
|
| 18 |
#' @param show_association (`logical`) optional, whether show association of `vars` |
|
| 19 |
#' with reference variable. Defaults to `TRUE`. |
|
| 20 |
#' @param distribution_theme,association_theme (`character`) optional, `ggplot2` themes to be used by default. |
|
| 21 |
#' Default to `"gray"`. |
|
| 22 |
#' |
|
| 23 |
#' @param ggplot2_args `r roxygen_ggplot2_args_param("Bivariate1", "Bivariate2")`
|
|
| 24 |
#' |
|
| 25 |
#' @inherit shared_params return |
|
| 26 |
#' |
|
| 27 |
#' @section Decorating Module: |
|
| 28 |
#' |
|
| 29 |
#' This module generates the following objects, which can be modified in place using decorators: |
|
| 30 |
#' - `plot` (`grob` created with [ggplot2::ggplotGrob()]) |
|
| 31 |
#' |
|
| 32 |
#' A Decorator is applied to the specific output using a named list of `teal_transform_module` objects. |
|
| 33 |
#' The name of this list corresponds to the name of the output to which the decorator is applied. |
|
| 34 |
#' See code snippet below: |
|
| 35 |
#' |
|
| 36 |
#' ``` |
|
| 37 |
#' tm_g_association( |
|
| 38 |
#' ..., # arguments for module |
|
| 39 |
#' decorators = list( |
|
| 40 |
#' plot = teal_transform_module(...) # applied to the `plot` output |
|
| 41 |
#' ) |
|
| 42 |
#' ) |
|
| 43 |
#' ``` |
|
| 44 |
#' |
|
| 45 |
#' For additional details and examples of decorators, refer to the vignette |
|
| 46 |
#' `vignette("decorate-module-output", package = "teal.modules.general")`.
|
|
| 47 |
#' |
|
| 48 |
#' To learn more please refer to the vignette |
|
| 49 |
#' `vignette("transform-module-output", package = "teal")` or the [`teal::teal_transform_module()`] documentation.
|
|
| 50 |
#' |
|
| 51 |
#' @examplesShinylive |
|
| 52 |
#' library(teal.modules.general) |
|
| 53 |
#' interactive <- function() TRUE |
|
| 54 |
#' {{ next_example }}
|
|
| 55 |
#' @examples |
|
| 56 |
#' # general data example |
|
| 57 |
#' data <- teal_data() |
|
| 58 |
#' data <- within(data, {
|
|
| 59 |
#' require(nestcolor) |
|
| 60 |
#' CO2 <- CO2 |
|
| 61 |
#' factors <- names(Filter(isTRUE, vapply(CO2, is.factor, logical(1L)))) |
|
| 62 |
#' CO2[factors] <- lapply(CO2[factors], as.character) |
|
| 63 |
#' }) |
|
| 64 |
#' |
|
| 65 |
#' app <- init( |
|
| 66 |
#' data = data, |
|
| 67 |
#' modules = modules( |
|
| 68 |
#' tm_g_association( |
|
| 69 |
#' ref = data_extract_spec( |
|
| 70 |
#' dataname = "CO2", |
|
| 71 |
#' select = select_spec( |
|
| 72 |
#' label = "Select variable:", |
|
| 73 |
#' choices = variable_choices(data[["CO2"]], c("Plant", "Type", "Treatment")),
|
|
| 74 |
#' selected = "Plant", |
|
| 75 |
#' fixed = FALSE |
|
| 76 |
#' ) |
|
| 77 |
#' ), |
|
| 78 |
#' vars = data_extract_spec( |
|
| 79 |
#' dataname = "CO2", |
|
| 80 |
#' select = select_spec( |
|
| 81 |
#' label = "Select variables:", |
|
| 82 |
#' choices = variable_choices(data[["CO2"]], c("Plant", "Type", "Treatment")),
|
|
| 83 |
#' selected = "Treatment", |
|
| 84 |
#' multiple = TRUE, |
|
| 85 |
#' fixed = FALSE |
|
| 86 |
#' ) |
|
| 87 |
#' ) |
|
| 88 |
#' ) |
|
| 89 |
#' ) |
|
| 90 |
#' ) |
|
| 91 |
#' if (interactive()) {
|
|
| 92 |
#' shinyApp(app$ui, app$server) |
|
| 93 |
#' } |
|
| 94 |
#' |
|
| 95 |
#' @examplesShinylive |
|
| 96 |
#' library(teal.modules.general) |
|
| 97 |
#' interactive <- function() TRUE |
|
| 98 |
#' {{ next_example }}
|
|
| 99 |
#' @examples |
|
| 100 |
#' # CDISC data example |
|
| 101 |
#' data <- teal_data() |
|
| 102 |
#' data <- within(data, {
|
|
| 103 |
#' require(nestcolor) |
|
| 104 |
#' ADSL <- teal.data::rADSL |
|
| 105 |
#' }) |
|
| 106 |
#' join_keys(data) <- default_cdisc_join_keys[names(data)] |
|
| 107 |
#' |
|
| 108 |
#' app <- init( |
|
| 109 |
#' data = data, |
|
| 110 |
#' modules = modules( |
|
| 111 |
#' tm_g_association( |
|
| 112 |
#' ref = data_extract_spec( |
|
| 113 |
#' dataname = "ADSL", |
|
| 114 |
#' select = select_spec( |
|
| 115 |
#' label = "Select variable:", |
|
| 116 |
#' choices = variable_choices( |
|
| 117 |
#' data[["ADSL"]], |
|
| 118 |
#' c("SEX", "RACE", "COUNTRY", "ARM", "STRATA1", "STRATA2", "ITTFL", "BMRKR2")
|
|
| 119 |
#' ), |
|
| 120 |
#' selected = "RACE", |
|
| 121 |
#' fixed = FALSE |
|
| 122 |
#' ) |
|
| 123 |
#' ), |
|
| 124 |
#' vars = data_extract_spec( |
|
| 125 |
#' dataname = "ADSL", |
|
| 126 |
#' select = select_spec( |
|
| 127 |
#' label = "Select variables:", |
|
| 128 |
#' choices = variable_choices( |
|
| 129 |
#' data[["ADSL"]], |
|
| 130 |
#' c("SEX", "RACE", "COUNTRY", "ARM", "STRATA1", "STRATA2", "ITTFL", "BMRKR2")
|
|
| 131 |
#' ), |
|
| 132 |
#' selected = "BMRKR2", |
|
| 133 |
#' multiple = TRUE, |
|
| 134 |
#' fixed = FALSE |
|
| 135 |
#' ) |
|
| 136 |
#' ) |
|
| 137 |
#' ) |
|
| 138 |
#' ) |
|
| 139 |
#' ) |
|
| 140 |
#' if (interactive()) {
|
|
| 141 |
#' shinyApp(app$ui, app$server) |
|
| 142 |
#' } |
|
| 143 |
#' |
|
| 144 |
#' @export |
|
| 145 |
#' |
|
| 146 |
tm_g_association <- function(label = "Association", |
|
| 147 |
ref, |
|
| 148 |
vars, |
|
| 149 |
show_association = TRUE, |
|
| 150 |
plot_height = c(600, 400, 5000), |
|
| 151 |
plot_width = NULL, |
|
| 152 |
distribution_theme = c("gray", "bw", "linedraw", "light", "dark", "minimal", "classic", "void"), # nolint: line_length.
|
|
| 153 |
association_theme = c("gray", "bw", "linedraw", "light", "dark", "minimal", "classic", "void"), # nolint: line_length.
|
|
| 154 |
pre_output = NULL, |
|
| 155 |
post_output = NULL, |
|
| 156 |
ggplot2_args = teal.widgets::ggplot2_args(), |
|
| 157 |
transformators = list(), |
|
| 158 |
decorators = list()) {
|
|
| 159 | ! |
message("Initializing tm_g_association")
|
| 160 | ||
| 161 |
# Normalize the parameters |
|
| 162 | ! |
if (inherits(ref, "data_extract_spec")) ref <- list(ref) |
| 163 | ! |
if (inherits(vars, "data_extract_spec")) vars <- list(vars) |
| 164 | ! |
if (inherits(ggplot2_args, "ggplot2_args")) ggplot2_args <- list(default = ggplot2_args) |
| 165 | ||
| 166 |
# Start of assertions |
|
| 167 | ! |
checkmate::assert_string(label) |
| 168 | ||
| 169 | ! |
checkmate::assert_list(ref, types = "data_extract_spec") |
| 170 | ! |
if (!all(vapply(ref, function(x) !x$select$multiple, logical(1)))) {
|
| 171 | ! |
stop("'ref' should not allow multiple selection")
|
| 172 |
} |
|
| 173 | ||
| 174 | ! |
checkmate::assert_list(vars, types = "data_extract_spec") |
| 175 | ! |
checkmate::assert_flag(show_association) |
| 176 | ||
| 177 | ! |
checkmate::assert_numeric(plot_height, len = 3, any.missing = FALSE, finite = TRUE) |
| 178 | ! |
checkmate::assert_numeric(plot_height[1], lower = plot_height[2], upper = plot_height[3], .var.name = "plot_height") |
| 179 | ! |
checkmate::assert_numeric(plot_width, len = 3, any.missing = FALSE, null.ok = TRUE, finite = TRUE) |
| 180 | ! |
checkmate::assert_numeric( |
| 181 | ! |
plot_width[1], |
| 182 | ! |
lower = plot_width[2], upper = plot_width[3], null.ok = TRUE, .var.name = "plot_width" |
| 183 |
) |
|
| 184 | ||
| 185 | ! |
distribution_theme <- match.arg(distribution_theme) |
| 186 | ! |
association_theme <- match.arg(association_theme) |
| 187 | ||
| 188 | ! |
checkmate::assert_multi_class(pre_output, c("shiny.tag", "shiny.tag.list", "html"), null.ok = TRUE)
|
| 189 | ! |
checkmate::assert_multi_class(post_output, c("shiny.tag", "shiny.tag.list", "html"), null.ok = TRUE)
|
| 190 | ||
| 191 | ! |
plot_choices <- c("Bivariate1", "Bivariate2")
|
| 192 | ! |
checkmate::assert_list(ggplot2_args, types = "ggplot2_args") |
| 193 | ! |
checkmate::assert_subset(names(ggplot2_args), c("default", plot_choices))
|
| 194 | ||
| 195 | ! |
assert_decorators(decorators, "plot") |
| 196 |
# End of assertions |
|
| 197 | ||
| 198 |
# Make UI args |
|
| 199 | ! |
args <- as.list(environment()) |
| 200 | ||
| 201 | ! |
data_extract_list <- list( |
| 202 | ! |
ref = ref, |
| 203 | ! |
vars = vars |
| 204 |
) |
|
| 205 | ||
| 206 | ! |
ans <- module( |
| 207 | ! |
label = label, |
| 208 | ! |
server = srv_tm_g_association, |
| 209 | ! |
ui = ui_tm_g_association, |
| 210 | ! |
ui_args = args, |
| 211 | ! |
server_args = c( |
| 212 | ! |
data_extract_list, |
| 213 | ! |
list(plot_height = plot_height, plot_width = plot_width, ggplot2_args = ggplot2_args, decorators = decorators) |
| 214 |
), |
|
| 215 | ! |
transformators = transformators, |
| 216 | ! |
datanames = teal.transform::get_extract_datanames(data_extract_list) |
| 217 |
) |
|
| 218 | ! |
attr(ans, "teal_bookmarkable") <- TRUE |
| 219 | ! |
ans |
| 220 |
} |
|
| 221 | ||
| 222 |
# UI function for the association module |
|
| 223 |
ui_tm_g_association <- function(id, ...) {
|
|
| 224 | ! |
ns <- NS(id) |
| 225 | ! |
args <- list(...) |
| 226 | ! |
is_single_dataset_value <- teal.transform::is_single_dataset(args$ref, args$vars) |
| 227 | ||
| 228 | ! |
teal.widgets::standard_layout( |
| 229 | ! |
output = teal.widgets::white_small_well( |
| 230 | ! |
textOutput(ns("title")),
|
| 231 | ! |
tags$br(), |
| 232 | ! |
teal.widgets::plot_with_settings_ui(id = ns("myplot"))
|
| 233 |
), |
|
| 234 | ! |
encoding = tags$div( |
| 235 |
### Reporter |
|
| 236 | ! |
teal.reporter::add_card_button_ui(ns("add_reporter"), label = "Add Report Card"),
|
| 237 | ! |
tags$br(), tags$br(), |
| 238 |
### |
|
| 239 | ! |
tags$label("Encodings", class = "text-primary"),
|
| 240 | ! |
teal.transform::datanames_input(args[c("ref", "vars")]),
|
| 241 | ! |
teal.transform::data_extract_ui( |
| 242 | ! |
id = ns("ref"),
|
| 243 | ! |
label = "Reference variable", |
| 244 | ! |
data_extract_spec = args$ref, |
| 245 | ! |
is_single_dataset = is_single_dataset_value |
| 246 |
), |
|
| 247 | ! |
teal.transform::data_extract_ui( |
| 248 | ! |
id = ns("vars"),
|
| 249 | ! |
label = "Associated variables", |
| 250 | ! |
data_extract_spec = args$vars, |
| 251 | ! |
is_single_dataset = is_single_dataset_value |
| 252 |
), |
|
| 253 | ! |
checkboxInput( |
| 254 | ! |
ns("association"),
|
| 255 | ! |
"Association with reference variable", |
| 256 | ! |
value = args$show_association |
| 257 |
), |
|
| 258 | ! |
checkboxInput( |
| 259 | ! |
ns("show_dist"),
|
| 260 | ! |
"Scaled frequencies", |
| 261 | ! |
value = FALSE |
| 262 |
), |
|
| 263 | ! |
checkboxInput( |
| 264 | ! |
ns("log_transformation"),
|
| 265 | ! |
"Log transformed", |
| 266 | ! |
value = FALSE |
| 267 |
), |
|
| 268 | ! |
ui_decorate_teal_data(ns("decorator"), decorators = select_decorators(args$decorators, "plot")),
|
| 269 | ! |
bslib::accordion( |
| 270 | ! |
open = TRUE, |
| 271 | ! |
bslib::accordion_panel( |
| 272 | ! |
title = "Plot settings", |
| 273 | ! |
teal.widgets::optionalSliderInputValMinMax(ns("alpha"), "Scatterplot opacity:", c(0.5, 0, 1), ticks = FALSE),
|
| 274 | ! |
teal.widgets::optionalSliderInputValMinMax(ns("size"), "Scatterplot points size:", c(2, 1, 8), ticks = FALSE),
|
| 275 | ! |
checkboxInput(ns("swap_axes"), "Swap axes", value = FALSE),
|
| 276 | ! |
checkboxInput(ns("rotate_xaxis_labels"), "Rotate X axis labels", value = FALSE),
|
| 277 | ! |
selectInput( |
| 278 | ! |
inputId = ns("distribution_theme"),
|
| 279 | ! |
label = "Distribution theme (by ggplot):", |
| 280 | ! |
choices = ggplot_themes, |
| 281 | ! |
selected = args$distribution_theme, |
| 282 | ! |
multiple = FALSE |
| 283 |
), |
|
| 284 | ! |
selectInput( |
| 285 | ! |
inputId = ns("association_theme"),
|
| 286 | ! |
label = "Association theme (by ggplot):", |
| 287 | ! |
choices = ggplot_themes, |
| 288 | ! |
selected = args$association_theme, |
| 289 | ! |
multiple = FALSE |
| 290 |
) |
|
| 291 |
) |
|
| 292 |
) |
|
| 293 |
), |
|
| 294 | ! |
forms = tagList( |
| 295 | ! |
teal.widgets::verbatim_popup_ui(ns("rcode"), "Show R code")
|
| 296 |
), |
|
| 297 | ! |
pre_output = args$pre_output, |
| 298 | ! |
post_output = args$post_output |
| 299 |
) |
|
| 300 |
} |
|
| 301 | ||
| 302 |
# Server function for the association module |
|
| 303 |
srv_tm_g_association <- function(id, |
|
| 304 |
data, |
|
| 305 |
reporter, |
|
| 306 |
filter_panel_api, |
|
| 307 |
ref, |
|
| 308 |
vars, |
|
| 309 |
plot_height, |
|
| 310 |
plot_width, |
|
| 311 |
ggplot2_args, |
|
| 312 |
decorators) {
|
|
| 313 | ! |
with_reporter <- !missing(reporter) && inherits(reporter, "Reporter") |
| 314 | ! |
with_filter <- !missing(filter_panel_api) && inherits(filter_panel_api, "FilterPanelAPI") |
| 315 | ! |
checkmate::assert_class(data, "reactive") |
| 316 | ! |
checkmate::assert_class(isolate(data()), "teal_data") |
| 317 | ||
| 318 | ! |
moduleServer(id, function(input, output, session) {
|
| 319 | ! |
teal.logger::log_shiny_input_changes(input, namespace = "teal.modules.general") |
| 320 | ||
| 321 | ! |
selector_list <- teal.transform::data_extract_multiple_srv( |
| 322 | ! |
data_extract = list(ref = ref, vars = vars), |
| 323 | ! |
datasets = data, |
| 324 | ! |
select_validation_rule = list( |
| 325 | ! |
ref = shinyvalidate::compose_rules( |
| 326 | ! |
shinyvalidate::sv_required("A reference variable needs to be selected."),
|
| 327 | ! |
~ if ((.) %in% selector_list()$vars()$select) {
|
| 328 | ! |
"Associated variables and reference variable cannot overlap" |
| 329 |
} |
|
| 330 |
), |
|
| 331 | ! |
vars = shinyvalidate::compose_rules( |
| 332 | ! |
shinyvalidate::sv_required("An associated variable needs to be selected."),
|
| 333 | ! |
~ if (length(selector_list()$ref()$select) != 0 && selector_list()$ref()$select %in% (.)) {
|
| 334 | ! |
"Associated variables and reference variable cannot overlap" |
| 335 |
} |
|
| 336 |
) |
|
| 337 |
) |
|
| 338 |
) |
|
| 339 | ||
| 340 | ! |
iv_r <- reactive({
|
| 341 | ! |
iv <- shinyvalidate::InputValidator$new() |
| 342 | ! |
teal.transform::compose_and_enable_validators(iv, selector_list) |
| 343 |
}) |
|
| 344 | ||
| 345 | ! |
anl_merged_input <- teal.transform::merge_expression_srv( |
| 346 | ! |
datasets = data, |
| 347 | ! |
selector_list = selector_list |
| 348 |
) |
|
| 349 | ||
| 350 | ! |
qenv <- reactive( |
| 351 | ! |
teal.code::eval_code(data(), 'library("ggplot2");library("dplyr");library("tern")') # nolint quotes
|
| 352 |
) |
|
| 353 | ! |
anl_merged_q <- reactive({
|
| 354 | ! |
req(anl_merged_input()) |
| 355 | ! |
qenv() %>% teal.code::eval_code(as.expression(anl_merged_input()$expr)) |
| 356 |
}) |
|
| 357 | ||
| 358 | ! |
merged <- list( |
| 359 | ! |
anl_input_r = anl_merged_input, |
| 360 | ! |
anl_q_r = anl_merged_q |
| 361 |
) |
|
| 362 | ||
| 363 | ! |
output_q <- reactive({
|
| 364 | ! |
teal::validate_inputs(iv_r()) |
| 365 | ||
| 366 | ! |
ANL <- merged$anl_q_r()[["ANL"]] |
| 367 | ! |
teal::validate_has_data(ANL, 3) |
| 368 | ||
| 369 | ! |
vars_names <- merged$anl_input_r()$columns_source$vars |
| 370 | ||
| 371 | ! |
ref_name <- as.vector(merged$anl_input_r()$columns_source$ref) |
| 372 | ! |
association <- input$association |
| 373 | ! |
show_dist <- input$show_dist |
| 374 | ! |
log_transformation <- input$log_transformation |
| 375 | ! |
rotate_xaxis_labels <- input$rotate_xaxis_labels |
| 376 | ! |
swap_axes <- input$swap_axes |
| 377 | ! |
distribution_theme <- input$distribution_theme |
| 378 | ! |
association_theme <- input$association_theme |
| 379 | ||
| 380 | ! |
is_scatterplot <- is.numeric(ANL[[ref_name]]) && any(vapply(ANL[vars_names], is.numeric, logical(1))) |
| 381 | ! |
if (is_scatterplot) {
|
| 382 | ! |
shinyjs::show("alpha")
|
| 383 | ! |
shinyjs::show("size")
|
| 384 | ! |
alpha <- input$alpha |
| 385 | ! |
size <- input$size |
| 386 |
} else {
|
|
| 387 | ! |
shinyjs::hide("alpha")
|
| 388 | ! |
shinyjs::hide("size")
|
| 389 | ! |
alpha <- 0.5 |
| 390 | ! |
size <- 2 |
| 391 |
} |
|
| 392 | ||
| 393 | ! |
teal::validate_has_data(ANL[, c(ref_name, vars_names)], 3, complete = TRUE, allow_inf = FALSE) |
| 394 | ||
| 395 |
# reference |
|
| 396 | ! |
ref_class <- class(ANL[[ref_name]])[1] |
| 397 | ! |
if (is.numeric(ANL[[ref_name]]) && log_transformation) {
|
| 398 |
# works for both integers and doubles |
|
| 399 | ! |
ref_cl_name <- call("log", as.name(ref_name))
|
| 400 | ! |
ref_cl_lbl <- varname_w_label(ref_name, ANL, prefix = "Log of ") |
| 401 |
} else {
|
|
| 402 |
# silently ignore when non-numeric even if `log` is selected because some |
|
| 403 |
# variables may be numeric and others not |
|
| 404 | ! |
ref_cl_name <- as.name(ref_name) |
| 405 | ! |
ref_cl_lbl <- varname_w_label(ref_name, ANL) |
| 406 |
} |
|
| 407 | ||
| 408 | ! |
user_ggplot2_args <- teal.widgets::resolve_ggplot2_args( |
| 409 | ! |
user_plot = ggplot2_args[["Bivariate1"]], |
| 410 | ! |
user_default = ggplot2_args$default |
| 411 |
) |
|
| 412 | ||
| 413 | ! |
ref_call <- bivariate_plot_call( |
| 414 | ! |
data_name = "ANL", |
| 415 | ! |
x = ref_cl_name, |
| 416 | ! |
x_class = ref_class, |
| 417 | ! |
x_label = ref_cl_lbl, |
| 418 | ! |
freq = !show_dist, |
| 419 | ! |
theme = distribution_theme, |
| 420 | ! |
rotate_xaxis_labels = rotate_xaxis_labels, |
| 421 | ! |
swap_axes = FALSE, |
| 422 | ! |
size = size, |
| 423 | ! |
alpha = alpha, |
| 424 | ! |
ggplot2_args = user_ggplot2_args |
| 425 |
) |
|
| 426 | ||
| 427 |
# association |
|
| 428 | ! |
ref_class_cov <- ifelse(association, ref_class, "NULL") |
| 429 | ||
| 430 | ! |
var_calls <- lapply(vars_names, function(var_i) {
|
| 431 | ! |
var_class <- class(ANL[[var_i]])[1] |
| 432 | ! |
if (is.numeric(ANL[[var_i]]) && log_transformation) {
|
| 433 |
# works for both integers and doubles |
|
| 434 | ! |
var_cl_name <- call("log", as.name(var_i))
|
| 435 | ! |
var_cl_lbl <- varname_w_label(var_i, ANL, prefix = "Log of ") |
| 436 |
} else {
|
|
| 437 |
# silently ignore when non-numeric even if `log` is selected because some |
|
| 438 |
# variables may be numeric and others not |
|
| 439 | ! |
var_cl_name <- as.name(var_i) |
| 440 | ! |
var_cl_lbl <- varname_w_label(var_i, ANL) |
| 441 |
} |
|
| 442 | ||
| 443 | ! |
user_ggplot2_args <- teal.widgets::resolve_ggplot2_args( |
| 444 | ! |
user_plot = ggplot2_args[["Bivariate2"]], |
| 445 | ! |
user_default = ggplot2_args$default |
| 446 |
) |
|
| 447 | ||
| 448 | ! |
bivariate_plot_call( |
| 449 | ! |
data_name = "ANL", |
| 450 | ! |
x = ref_cl_name, |
| 451 | ! |
y = var_cl_name, |
| 452 | ! |
x_class = ref_class_cov, |
| 453 | ! |
y_class = var_class, |
| 454 | ! |
x_label = ref_cl_lbl, |
| 455 | ! |
y_label = var_cl_lbl, |
| 456 | ! |
theme = association_theme, |
| 457 | ! |
freq = !show_dist, |
| 458 | ! |
rotate_xaxis_labels = rotate_xaxis_labels, |
| 459 | ! |
swap_axes = swap_axes, |
| 460 | ! |
alpha = alpha, |
| 461 | ! |
size = size, |
| 462 | ! |
ggplot2_args = user_ggplot2_args |
| 463 |
) |
|
| 464 |
}) |
|
| 465 | ||
| 466 |
# helper function to format variable name |
|
| 467 | ! |
format_varnames <- function(x) {
|
| 468 | ! |
if (is.numeric(ANL[[x]]) && log_transformation) {
|
| 469 | ! |
varname_w_label(x, ANL, prefix = "Log of ") |
| 470 |
} else {
|
|
| 471 | ! |
varname_w_label(x, ANL) |
| 472 |
} |
|
| 473 |
} |
|
| 474 | ! |
new_title <- |
| 475 | ! |
if (association) {
|
| 476 | ! |
switch(as.character(length(vars_names)), |
| 477 | ! |
"0" = sprintf("Value distribution for %s", ref_cl_lbl),
|
| 478 | ! |
"1" = sprintf( |
| 479 | ! |
"Association between %s and %s", |
| 480 | ! |
ref_cl_lbl, |
| 481 | ! |
format_varnames(vars_names) |
| 482 |
), |
|
| 483 | ! |
sprintf( |
| 484 | ! |
"Associations between %s and: %s", |
| 485 | ! |
ref_cl_lbl, |
| 486 | ! |
paste(lapply(vars_names, format_varnames), collapse = ", ") |
| 487 |
) |
|
| 488 |
) |
|
| 489 |
} else {
|
|
| 490 | ! |
switch(as.character(length(vars_names)), |
| 491 | ! |
"0" = sprintf("Value distribution for %s", ref_cl_lbl),
|
| 492 | ! |
sprintf( |
| 493 | ! |
"Value distributions for %s and %s", |
| 494 | ! |
ref_cl_lbl, |
| 495 | ! |
paste(lapply(vars_names, format_varnames), collapse = ", ") |
| 496 |
) |
|
| 497 |
) |
|
| 498 |
} |
|
| 499 | ! |
teal.code::eval_code( |
| 500 | ! |
merged$anl_q_r(), |
| 501 | ! |
substitute( |
| 502 | ! |
expr = title <- new_title, |
| 503 | ! |
env = list(new_title = new_title) |
| 504 |
) |
|
| 505 |
) %>% |
|
| 506 | ! |
teal.code::eval_code( |
| 507 | ! |
substitute( |
| 508 | ! |
expr = {
|
| 509 | ! |
plots <- plot_calls |
| 510 | ! |
plot_top <- plots[[1]] |
| 511 | ! |
plot_bottom <- plots[[2]] |
| 512 | ! |
plot <- gridExtra::grid.arrange(plot_top, plot_bottom, ncol = 1) |
| 513 |
}, |
|
| 514 | ! |
env = list( |
| 515 | ! |
plot_calls = do.call( |
| 516 | ! |
"call", |
| 517 | ! |
c(list("list", ref_call), var_calls),
|
| 518 | ! |
quote = TRUE |
| 519 |
) |
|
| 520 |
) |
|
| 521 |
) |
|
| 522 |
) |
|
| 523 |
}) |
|
| 524 | ||
| 525 | ! |
decorated_output_grob_q <- srv_decorate_teal_data( |
| 526 | ! |
id = "decorator", |
| 527 | ! |
data = output_q, |
| 528 | ! |
decorators = select_decorators(decorators, "plot"), |
| 529 | ! |
expr = {
|
| 530 | ! |
grid::grid.newpage() |
| 531 | ! |
grid::grid.draw(plot) |
| 532 |
} |
|
| 533 |
) |
|
| 534 | ||
| 535 | ! |
plot_r <- reactive({
|
| 536 | ! |
req(iv_r()$is_valid()) |
| 537 | ! |
req(decorated_output_grob_q())[["plot"]] |
| 538 |
}) |
|
| 539 | ||
| 540 | ! |
pws <- teal.widgets::plot_with_settings_srv( |
| 541 | ! |
id = "myplot", |
| 542 | ! |
plot_r = plot_r, |
| 543 | ! |
height = plot_height, |
| 544 | ! |
width = plot_width |
| 545 |
) |
|
| 546 | ||
| 547 | ! |
output$title <- renderText(output_q()[["title"]]) |
| 548 | ||
| 549 |
# Render R code. |
|
| 550 | ! |
source_code_r <- reactive(teal.code::get_code(req(decorated_output_grob_q()))) |
| 551 | ||
| 552 | ! |
teal.widgets::verbatim_popup_srv( |
| 553 | ! |
id = "rcode", |
| 554 | ! |
verbatim_content = source_code_r, |
| 555 | ! |
title = "Association Plot" |
| 556 |
) |
|
| 557 | ||
| 558 |
### REPORTER |
|
| 559 | ! |
if (with_reporter) {
|
| 560 | ! |
card_fun <- function(comment, label) {
|
| 561 | ! |
card <- teal::report_card_template( |
| 562 | ! |
title = "Association Plot", |
| 563 | ! |
label = label, |
| 564 | ! |
with_filter = with_filter, |
| 565 | ! |
filter_panel_api = filter_panel_api |
| 566 |
) |
|
| 567 | ! |
card$append_text("Plot", "header3")
|
| 568 | ! |
card$append_plot(plot_r(), dim = pws$dim()) |
| 569 | ! |
if (!comment == "") {
|
| 570 | ! |
card$append_text("Comment", "header3")
|
| 571 | ! |
card$append_text(comment) |
| 572 |
} |
|
| 573 | ! |
card$append_src(source_code_r()) |
| 574 | ! |
card |
| 575 |
} |
|
| 576 | ! |
teal.reporter::add_card_button_srv("add_reporter", reporter = reporter, card_fun = card_fun)
|
| 577 |
} |
|
| 578 |
### |
|
| 579 |
}) |
|
| 580 |
} |
| 1 |
#' `teal` module: Variable browser |
|
| 2 |
#' |
|
| 3 |
#' Module provides provides a detailed summary and visualization of variable distributions |
|
| 4 |
#' for `data.frame` objects, with interactive features to customize analysis. |
|
| 5 |
#' |
|
| 6 |
#' Numeric columns with fewer than 30 distinct values can be treated as either discrete |
|
| 7 |
#' or continuous with a checkbox allowing users to switch how they are treated(if < 6 unique values |
|
| 8 |
#' then the default is discrete, otherwise it is continuous). |
|
| 9 |
#' |
|
| 10 |
#' @inheritParams teal::module |
|
| 11 |
#' @inheritParams shared_params |
|
| 12 |
#' @param parent_dataname (`character(1)`) string specifying a parent dataset. |
|
| 13 |
#' If it exists in `datanames` then an extra checkbox will be shown to |
|
| 14 |
#' allow users to not show variables in other datasets which exist in this `dataname`. |
|
| 15 |
#' This is typically used to remove `ADSL` columns in `CDISC` data. |
|
| 16 |
#' In non `CDISC` data this can be ignored. Defaults to `"ADSL"`. |
|
| 17 |
#' @param datasets_selected (`character`) `r lifecycle::badge("deprecated")` vector of datasets to show, please
|
|
| 18 |
#' use the `datanames` argument. |
|
| 19 |
#' |
|
| 20 |
#' @inherit shared_params return |
|
| 21 |
#' |
|
| 22 |
#' @examplesShinylive |
|
| 23 |
#' library(teal.modules.general) |
|
| 24 |
#' interactive <- function() TRUE |
|
| 25 |
#' {{ next_example }}
|
|
| 26 |
# nolint start: line_length_linter. |
|
| 27 |
#' @examples |
|
| 28 |
# nolint end: line_length_linter. |
|
| 29 |
#' # general data example |
|
| 30 |
#' data <- teal_data() |
|
| 31 |
#' data <- within(data, {
|
|
| 32 |
#' iris <- iris |
|
| 33 |
#' mtcars <- mtcars |
|
| 34 |
#' women <- women |
|
| 35 |
#' faithful <- faithful |
|
| 36 |
#' CO2 <- CO2 |
|
| 37 |
#' }) |
|
| 38 |
#' |
|
| 39 |
#' app <- init( |
|
| 40 |
#' data = data, |
|
| 41 |
#' modules = modules( |
|
| 42 |
#' tm_variable_browser( |
|
| 43 |
#' label = "Variable browser" |
|
| 44 |
#' ) |
|
| 45 |
#' ) |
|
| 46 |
#' ) |
|
| 47 |
#' if (interactive()) {
|
|
| 48 |
#' shinyApp(app$ui, app$server) |
|
| 49 |
#' } |
|
| 50 |
#' |
|
| 51 |
#' @examplesShinylive |
|
| 52 |
#' library(teal.modules.general) |
|
| 53 |
#' interactive <- function() TRUE |
|
| 54 |
#' {{ next_example }}
|
|
| 55 |
# nolint start: line_length_linter. |
|
| 56 |
#' @examples |
|
| 57 |
# nolint end: line_length_linter. |
|
| 58 |
#' # CDISC example data |
|
| 59 |
#' library(sparkline) |
|
| 60 |
#' data <- teal_data() |
|
| 61 |
#' data <- within(data, {
|
|
| 62 |
#' ADSL <- teal.data::rADSL |
|
| 63 |
#' ADTTE <- teal.data::rADTTE |
|
| 64 |
#' }) |
|
| 65 |
#' join_keys(data) <- default_cdisc_join_keys[names(data)] |
|
| 66 |
#' |
|
| 67 |
#' app <- init( |
|
| 68 |
#' data = data, |
|
| 69 |
#' modules = modules( |
|
| 70 |
#' tm_variable_browser( |
|
| 71 |
#' label = "Variable browser" |
|
| 72 |
#' ) |
|
| 73 |
#' ) |
|
| 74 |
#' ) |
|
| 75 |
#' if (interactive()) {
|
|
| 76 |
#' shinyApp(app$ui, app$server) |
|
| 77 |
#' } |
|
| 78 |
#' |
|
| 79 |
#' @export |
|
| 80 |
#' |
|
| 81 |
tm_variable_browser <- function(label = "Variable Browser", |
|
| 82 |
datasets_selected = deprecated(), |
|
| 83 |
datanames = if (missing(datasets_selected)) "all" else datasets_selected, |
|
| 84 |
parent_dataname = "ADSL", |
|
| 85 |
pre_output = NULL, |
|
| 86 |
post_output = NULL, |
|
| 87 |
ggplot2_args = teal.widgets::ggplot2_args(), |
|
| 88 |
transformators = list()) {
|
|
| 89 | ! |
message("Initializing tm_variable_browser")
|
| 90 | ||
| 91 |
# Start of assertions |
|
| 92 | ! |
checkmate::assert_string(label) |
| 93 | ! |
if (!missing(datasets_selected)) {
|
| 94 | ! |
lifecycle::deprecate_stop( |
| 95 | ! |
when = "0.4.0", |
| 96 | ! |
what = "tm_variable_browser(datasets_selected)", |
| 97 | ! |
with = "tm_variable_browser(datanames)", |
| 98 | ! |
details = c( |
| 99 | ! |
"If both `datasets_selected` and `datanames` are set `datasets_selected` will be silently ignored.", |
| 100 | ! |
i = 'Use `tm_variable_browser(datanames = "all")` to keep the previous behavior and avoid this warning.' |
| 101 |
) |
|
| 102 |
) |
|
| 103 |
} |
|
| 104 | ! |
checkmate::assert_character(datanames, min.len = 0, min.chars = 1, null.ok = TRUE) |
| 105 | ! |
checkmate::assert_character(parent_dataname, min.len = 0, max.len = 1) |
| 106 | ! |
checkmate::assert_multi_class(pre_output, c("shiny.tag", "shiny.tag.list", "html"), null.ok = TRUE)
|
| 107 | ! |
checkmate::assert_multi_class(post_output, c("shiny.tag", "shiny.tag.list", "html"), null.ok = TRUE)
|
| 108 | ! |
checkmate::assert_class(ggplot2_args, "ggplot2_args") |
| 109 |
# End of assertions |
|
| 110 | ||
| 111 | ! |
datanames_module <- if (identical(datanames, "all") || is.null(datanames)) {
|
| 112 | ! |
datanames |
| 113 |
} else {
|
|
| 114 | ! |
union(datanames, parent_dataname) |
| 115 |
} |
|
| 116 | ||
| 117 | ! |
ans <- module( |
| 118 | ! |
label, |
| 119 | ! |
server = srv_variable_browser, |
| 120 | ! |
ui = ui_variable_browser, |
| 121 | ! |
datanames = datanames_module, |
| 122 | ! |
server_args = list( |
| 123 | ! |
datanames = if (is.null(datanames)) "all" else datanames, |
| 124 | ! |
parent_dataname = parent_dataname, |
| 125 | ! |
ggplot2_args = ggplot2_args |
| 126 |
), |
|
| 127 | ! |
ui_args = list( |
| 128 | ! |
pre_output = pre_output, |
| 129 | ! |
post_output = post_output |
| 130 |
), |
|
| 131 | ! |
transformators = transformators |
| 132 |
) |
|
| 133 |
# `shiny` inputs are stored properly but the majority of the module is state of `datatable` which is not stored. |
|
| 134 | ! |
attr(ans, "teal_bookmarkable") <- NULL |
| 135 | ! |
ans |
| 136 |
} |
|
| 137 | ||
| 138 |
# UI function for the variable browser module |
|
| 139 |
ui_variable_browser <- function(id, |
|
| 140 |
pre_output = NULL, |
|
| 141 |
post_output = NULL) {
|
|
| 142 | ! |
ns <- NS(id) |
| 143 | ||
| 144 | ! |
tags$div( |
| 145 | ! |
shinyjs::useShinyjs(), |
| 146 | ! |
teal.widgets::standard_layout( |
| 147 | ! |
output = tags$div( |
| 148 | ! |
htmlwidgets::getDependency("sparkline"), # needed for sparklines to work
|
| 149 | ! |
bslib::layout_column_wrap( |
| 150 | ! |
width = 0.5, |
| 151 | ! |
teal.widgets::white_small_well( |
| 152 | ! |
uiOutput(ns("ui_variable_browser")),
|
| 153 | ! |
shinyjs::hidden({
|
| 154 | ! |
checkboxInput(ns("show_parent_vars"), "Show parent dataset variables", value = FALSE)
|
| 155 |
}) |
|
| 156 |
), |
|
| 157 | ! |
teal.widgets::white_small_well( |
| 158 |
### Reporter |
|
| 159 | ! |
teal.reporter::add_card_button_ui(ns("add_reporter"), label = "Add Report Card"),
|
| 160 | ! |
tags$br(), tags$br(), |
| 161 |
### |
|
| 162 | ! |
uiOutput(ns("ui_histogram_display")),
|
| 163 | ! |
uiOutput(ns("ui_numeric_display")),
|
| 164 | ! |
teal.widgets::plot_with_settings_ui(ns("variable_plot")),
|
| 165 | ! |
tags$br(), |
| 166 | ! |
bslib::accordion( |
| 167 | ! |
open = TRUE, |
| 168 | ! |
bslib::accordion_panel( |
| 169 | ! |
title = "Plot settings", |
| 170 | ! |
collapsed = TRUE, |
| 171 | ! |
selectInput( |
| 172 | ! |
inputId = ns("ggplot_theme"), label = "ggplot2 theme",
|
| 173 | ! |
choices = ggplot_themes, |
| 174 | ! |
selected = "grey" |
| 175 |
), |
|
| 176 | ! |
bslib::layout_columns( |
| 177 | ! |
col_widths = c(6, 6), |
| 178 | ! |
sliderInput( |
| 179 | ! |
inputId = ns("font_size"), label = "font size",
|
| 180 | ! |
min = 5L, max = 30L, value = 15L, step = 1L, ticks = FALSE |
| 181 |
), |
|
| 182 | ! |
sliderInput( |
| 183 | ! |
inputId = ns("label_rotation"), label = "rotate x labels",
|
| 184 | ! |
min = 0L, max = 90L, value = 45L, step = 1, ticks = FALSE |
| 185 |
) |
|
| 186 |
) |
|
| 187 |
) |
|
| 188 |
), |
|
| 189 | ! |
tags$br(), |
| 190 | ! |
teal.widgets::get_dt_rows(ns("variable_summary_table"), ns("variable_summary_table_rows")),
|
| 191 | ! |
DT::dataTableOutput(ns("variable_summary_table"))
|
| 192 |
) |
|
| 193 |
) |
|
| 194 |
), |
|
| 195 | ! |
pre_output = pre_output, |
| 196 | ! |
post_output = post_output |
| 197 |
) |
|
| 198 |
) |
|
| 199 |
} |
|
| 200 | ||
| 201 |
# Server function for the variable browser module |
|
| 202 |
srv_variable_browser <- function(id, |
|
| 203 |
data, |
|
| 204 |
reporter, |
|
| 205 |
filter_panel_api, |
|
| 206 |
datanames, parent_dataname, ggplot2_args) {
|
|
| 207 | ! |
with_reporter <- !missing(reporter) && inherits(reporter, "Reporter") |
| 208 | ! |
with_filter <- !missing(filter_panel_api) && inherits(filter_panel_api, "FilterPanelAPI") |
| 209 | ! |
checkmate::assert_class(data, "reactive") |
| 210 | ! |
checkmate::assert_class(isolate(data()), "teal_data") |
| 211 | ! |
moduleServer(id, function(input, output, session) {
|
| 212 | ! |
teal.logger::log_shiny_input_changes(input, namespace = "teal.modules.general") |
| 213 | ||
| 214 |
# if there are < this number of unique records then a numeric |
|
| 215 |
# variable can be treated as a factor and all factors with < this groups |
|
| 216 |
# have their values plotted |
|
| 217 | ! |
.unique_records_for_factor <- 30 |
| 218 |
# if there are < this number of unique records then a numeric |
|
| 219 |
# variable is by default treated as a factor |
|
| 220 | ! |
.unique_records_default_as_factor <- 6 # nolint: object_length. |
| 221 | ||
| 222 | ! |
varname_numeric_as_factor <- reactiveValues() |
| 223 | ||
| 224 | ! |
datanames <- Filter(function(name) {
|
| 225 | ! |
is.data.frame(isolate(data())[[name]]) |
| 226 | ! |
}, if (identical(datanames, "all")) names(isolate(data())) else datanames) |
| 227 | ||
| 228 | ! |
output$ui_variable_browser <- renderUI({
|
| 229 | ! |
ns <- session$ns |
| 230 | ! |
do.call( |
| 231 | ! |
tabsetPanel, |
| 232 | ! |
c( |
| 233 | ! |
id = ns("tabset_panel"),
|
| 234 | ! |
do.call( |
| 235 | ! |
tagList, |
| 236 | ! |
lapply(datanames, function(dataname) {
|
| 237 | ! |
tabPanel( |
| 238 | ! |
dataname, |
| 239 | ! |
tags$div( |
| 240 | ! |
style = "margin-top: 1rem;", |
| 241 | ! |
textOutput(ns(paste0("dataset_summary_", dataname)))
|
| 242 |
), |
|
| 243 | ! |
tags$div( |
| 244 | ! |
style = "margin-top: 1rem;", |
| 245 | ! |
teal.widgets::get_dt_rows( |
| 246 | ! |
ns(paste0("variable_browser_", dataname)),
|
| 247 | ! |
ns(paste0("variable_browser_", dataname, "_rows"))
|
| 248 |
), |
|
| 249 | ! |
DT::dataTableOutput(ns(paste0("variable_browser_", dataname)), width = "100%")
|
| 250 |
) |
|
| 251 |
) |
|
| 252 |
}) |
|
| 253 |
) |
|
| 254 |
) |
|
| 255 |
) |
|
| 256 |
}) |
|
| 257 | ||
| 258 |
# conditionally display checkbox |
|
| 259 | ! |
shinyjs::toggle( |
| 260 | ! |
id = "show_parent_vars", |
| 261 | ! |
condition = length(parent_dataname) > 0 && parent_dataname %in% datanames |
| 262 |
) |
|
| 263 | ||
| 264 | ! |
columns_names <- new.env() |
| 265 | ||
| 266 |
# plot_var$data holds the name of the currently selected dataset |
|
| 267 |
# plot_var$variable[[<dataset_name>]] holds the name of the currently selected |
|
| 268 |
# variable for dataset <dataset_name> |
|
| 269 | ! |
plot_var <- reactiveValues(data = NULL, variable = list()) |
| 270 | ||
| 271 | ! |
establish_updating_selection(datanames, input, plot_var, columns_names) |
| 272 | ||
| 273 |
# validations |
|
| 274 | ! |
validation_checks <- validate_input(input, plot_var, data) |
| 275 | ||
| 276 |
# data_for_analysis is a list with two elements: a column from a dataset and the column label |
|
| 277 | ! |
plotted_data <- reactive({
|
| 278 | ! |
validation_checks() |
| 279 | ||
| 280 | ! |
get_plotted_data(input, plot_var, data) |
| 281 |
}) |
|
| 282 | ||
| 283 | ! |
treat_numeric_as_factor <- reactive({
|
| 284 | ! |
if (is_num_var_short(.unique_records_for_factor, input, plotted_data)) {
|
| 285 | ! |
input$numeric_as_factor |
| 286 |
} else {
|
|
| 287 | ! |
FALSE |
| 288 |
} |
|
| 289 |
}) |
|
| 290 | ||
| 291 | ! |
render_tabset_panel_content( |
| 292 | ! |
input = input, |
| 293 | ! |
output = output, |
| 294 | ! |
data = data, |
| 295 | ! |
datanames = datanames, |
| 296 | ! |
parent_dataname = parent_dataname, |
| 297 | ! |
columns_names = columns_names, |
| 298 | ! |
plot_var = plot_var |
| 299 |
) |
|
| 300 |
# add used-defined text size to ggplot arguments passed from caller frame |
|
| 301 | ! |
all_ggplot2_args <- reactive({
|
| 302 | ! |
user_text <- teal.widgets::ggplot2_args( |
| 303 | ! |
theme = list( |
| 304 | ! |
"text" = ggplot2::element_text(size = input[["font_size"]]), |
| 305 | ! |
"axis.text.x" = ggplot2::element_text(angle = input[["label_rotation"]], hjust = 1) |
| 306 |
) |
|
| 307 |
) |
|
| 308 | ! |
user_theme <- utils::getFromNamespace(sprintf("theme_%s", input[["ggplot_theme"]]), ns = "ggplot2")
|
| 309 | ! |
user_theme <- user_theme() |
| 310 |
# temporary fix to circumvent assertion issue with resolve_ggplot2_args |
|
| 311 |
# drop problematic elements |
|
| 312 | ! |
user_theme <- user_theme[grep("strip.text.y.left", names(user_theme), fixed = TRUE, invert = TRUE)]
|
| 313 | ||
| 314 | ! |
teal.widgets::resolve_ggplot2_args( |
| 315 | ! |
user_plot = user_text, |
| 316 | ! |
user_default = teal.widgets::ggplot2_args(theme = user_theme), |
| 317 | ! |
module_plot = ggplot2_args |
| 318 |
) |
|
| 319 |
}) |
|
| 320 | ||
| 321 | ! |
output$ui_numeric_display <- renderUI({
|
| 322 | ! |
validation_checks() |
| 323 | ! |
dataname <- input$tabset_panel |
| 324 | ! |
varname <- plot_var$variable[[dataname]] |
| 325 | ! |
df <- data()[[dataname]] |
| 326 | ||
| 327 | ! |
numeric_ui <- bslib::page_fluid( |
| 328 | ! |
bslib::layout_columns( |
| 329 | ! |
col_widths = c(8, 4), |
| 330 | ! |
bslib::layout_columns( |
| 331 | ! |
col_widths = c(6, 6, 12), |
| 332 | ! |
style = bslib::css(grid_row_gap = 0), |
| 333 | ! |
bslib::input_switch( |
| 334 | ! |
id = session$ns("display_density"),
|
| 335 | ! |
label = tags$div( |
| 336 | ! |
"Show density:", |
| 337 | ! |
bslib::tooltip( |
| 338 | ! |
trigger = icon("circle-info"),
|
| 339 | ! |
tags$span( |
| 340 | ! |
"Show kernel density estimation with gaussian kernel and bandwidth function bw.nrd0 (R default)" |
| 341 |
) |
|
| 342 |
) |
|
| 343 |
), |
|
| 344 | ! |
value = `if`(is.null(isolate(input$display_density)), TRUE, isolate(input$display_density)), |
| 345 | ! |
width = "100%" |
| 346 |
), |
|
| 347 | ! |
bslib::input_switch( |
| 348 | ! |
id = session$ns("remove_outliers"),
|
| 349 | ! |
label = "Remove outliers", |
| 350 | ! |
value = `if`(is.null(isolate(input$remove_outliers)), FALSE, isolate(input$remove_outliers)), |
| 351 | ! |
width = "100%" |
| 352 |
), |
|
| 353 | ! |
uiOutput(session$ns("ui_outlier_help"))
|
| 354 |
), |
|
| 355 | ! |
uiOutput(session$ns("outlier_definition_slider_ui"))
|
| 356 |
) |
|
| 357 |
) |
|
| 358 | ||
| 359 | ! |
observeEvent(input$numeric_as_factor, ignoreInit = TRUE, {
|
| 360 | ! |
varname_numeric_as_factor[[plot_var$variable[[dataname]]]] <- input$numeric_as_factor |
| 361 |
}) |
|
| 362 | ||
| 363 | ! |
if (is.numeric(df[[varname]])) {
|
| 364 | ! |
unique_entries <- length(unique(df[[varname]])) |
| 365 | ! |
if (unique_entries < .unique_records_for_factor && unique_entries > 0) {
|
| 366 | ! |
list( |
| 367 | ! |
checkboxInput( |
| 368 | ! |
session$ns("numeric_as_factor"),
|
| 369 | ! |
"Treat variable as factor", |
| 370 | ! |
value = `if`( |
| 371 | ! |
is.null(varname_numeric_as_factor[[varname]]), |
| 372 | ! |
unique_entries < .unique_records_default_as_factor, |
| 373 | ! |
varname_numeric_as_factor[[varname]] |
| 374 |
) |
|
| 375 |
), |
|
| 376 | ! |
conditionalPanel("!input.numeric_as_factor", ns = session$ns, numeric_ui)
|
| 377 |
) |
|
| 378 | ! |
} else if (unique_entries > 0) {
|
| 379 | ! |
numeric_ui |
| 380 |
} |
|
| 381 |
} else {
|
|
| 382 | ! |
NULL |
| 383 |
} |
|
| 384 |
}) |
|
| 385 | ||
| 386 | ! |
output$ui_histogram_display <- renderUI({
|
| 387 | ! |
validation_checks() |
| 388 | ! |
dataname <- input$tabset_panel |
| 389 | ! |
varname <- plot_var$variable[[dataname]] |
| 390 | ! |
df <- data()[[dataname]] |
| 391 | ||
| 392 | ! |
numeric_ui <- bslib::input_switch( |
| 393 | ! |
id = session$ns("remove_NA_hist"),
|
| 394 | ! |
label = "Remove NA values", |
| 395 | ! |
value = FALSE, |
| 396 | ! |
width = "100%" |
| 397 |
) |
|
| 398 | ||
| 399 | ! |
var <- df[[varname]] |
| 400 | ! |
if (anyNA(var) && (is.factor(var) || is.character(var) || is.logical(var))) {
|
| 401 | ! |
groups <- unique(as.character(var)) |
| 402 | ! |
len_groups <- length(groups) |
| 403 | ! |
if (len_groups >= .unique_records_for_factor) {
|
| 404 | ! |
NULL |
| 405 |
} else {
|
|
| 406 | ! |
numeric_ui |
| 407 |
} |
|
| 408 |
} else {
|
|
| 409 | ! |
NULL |
| 410 |
} |
|
| 411 |
}) |
|
| 412 | ||
| 413 | ! |
output$outlier_definition_slider_ui <- renderUI({
|
| 414 | ! |
req(input$remove_outliers) |
| 415 | ! |
sliderInput( |
| 416 | ! |
inputId = session$ns("outlier_definition_slider"),
|
| 417 | ! |
tags$div( |
| 418 | ! |
tagList( |
| 419 | ! |
"Outlier definition:", |
| 420 | ! |
bslib::tooltip( |
| 421 | ! |
icon("circle-info"),
|
| 422 | ! |
tags$span( |
| 423 | ! |
paste( |
| 424 | ! |
"Use the slider to choose the cut-off value to define outliers; the larger the value the", |
| 425 | ! |
"further below Q1/above Q3 points have to be in order to be classed as outliers" |
| 426 |
) |
|
| 427 |
) |
|
| 428 |
) |
|
| 429 |
) |
|
| 430 |
), |
|
| 431 | ! |
min = 1, |
| 432 | ! |
max = 5, |
| 433 | ! |
value = 3, |
| 434 | ! |
step = 0.5 |
| 435 |
) |
|
| 436 |
}) |
|
| 437 | ||
| 438 | ! |
output$ui_outlier_help <- renderUI({
|
| 439 | ! |
req(is.logical(input$remove_outliers), input$outlier_definition_slider) |
| 440 | ! |
if (input$remove_outliers) {
|
| 441 | ! |
tags$small( |
| 442 | ! |
helpText( |
| 443 | ! |
withMathJax(paste0( |
| 444 | ! |
"Outlier data points (\\( X \\lt Q1 - ", input$outlier_definition_slider, "\\times IQR \\) or |
| 445 | ! |
\\(Q3 + ", input$outlier_definition_slider, "\\times IQR \\lt X\\)) |
| 446 | ! |
have not been displayed on the graph and will not be used for any kernel density estimations, ", |
| 447 | ! |
"although their values remain in the statisics table below." |
| 448 |
)) |
|
| 449 |
) |
|
| 450 |
) |
|
| 451 |
} else {
|
|
| 452 | ! |
NULL |
| 453 |
} |
|
| 454 |
}) |
|
| 455 | ||
| 456 | ||
| 457 | ! |
variable_plot_r <- reactive({
|
| 458 | ! |
display_density <- `if`(is.null(input$display_density), FALSE, input$display_density) |
| 459 | ! |
remove_outliers <- `if`(is.null(input$remove_outliers), FALSE, input$remove_outliers) |
| 460 | ||
| 461 | ! |
if (remove_outliers) {
|
| 462 | ! |
req(input$outlier_definition_slider) |
| 463 | ! |
outlier_definition <- as.numeric(input$outlier_definition_slider) |
| 464 |
} else {
|
|
| 465 | ! |
outlier_definition <- 0 |
| 466 |
} |
|
| 467 | ||
| 468 | ! |
plot_var_summary( |
| 469 | ! |
var = plotted_data()$data, |
| 470 | ! |
var_lab = plotted_data()$var_description, |
| 471 | ! |
wrap_character = 15, |
| 472 | ! |
numeric_as_factor = treat_numeric_as_factor(), |
| 473 | ! |
remove_NA_hist = input$remove_NA_hist, |
| 474 | ! |
display_density = display_density, |
| 475 | ! |
outlier_definition = outlier_definition, |
| 476 | ! |
records_for_factor = .unique_records_for_factor, |
| 477 | ! |
ggplot2_args = all_ggplot2_args() |
| 478 |
) |
|
| 479 |
}) |
|
| 480 | ||
| 481 | ! |
pws <- teal.widgets::plot_with_settings_srv( |
| 482 | ! |
id = "variable_plot", |
| 483 | ! |
plot_r = variable_plot_r, |
| 484 | ! |
height = c(500, 200, 2000) |
| 485 |
) |
|
| 486 | ||
| 487 | ! |
output$variable_summary_table <- DT::renderDataTable({
|
| 488 | ! |
var_summary_table( |
| 489 | ! |
plotted_data()$data, |
| 490 | ! |
treat_numeric_as_factor(), |
| 491 | ! |
input$variable_summary_table_rows, |
| 492 | ! |
if (!is.null(input$remove_outliers) && input$remove_outliers) {
|
| 493 | ! |
req(input$outlier_definition_slider) |
| 494 | ! |
as.numeric(input$outlier_definition_slider) |
| 495 |
} else {
|
|
| 496 | ! |
0 |
| 497 |
} |
|
| 498 |
) |
|
| 499 |
}) |
|
| 500 | ||
| 501 |
### REPORTER |
|
| 502 | ! |
if (with_reporter) {
|
| 503 | ! |
card_fun <- function(comment) {
|
| 504 | ! |
card <- teal::TealReportCard$new() |
| 505 | ! |
card$set_name("Variable Browser Plot")
|
| 506 | ! |
card$append_text("Variable Browser Plot", "header2")
|
| 507 | ! |
if (with_filter) card$append_fs(filter_panel_api$get_filter_state()) |
| 508 | ! |
card$append_text("Plot", "header3")
|
| 509 | ! |
card$append_plot(variable_plot_r(), dim = pws$dim()) |
| 510 | ! |
if (!comment == "") {
|
| 511 | ! |
card$append_text("Comment", "header3")
|
| 512 | ! |
card$append_text(comment) |
| 513 |
} |
|
| 514 | ! |
card |
| 515 |
} |
|
| 516 | ! |
teal.reporter::add_card_button_srv("add_reporter", reporter = reporter, card_fun = card_fun)
|
| 517 |
} |
|
| 518 |
### |
|
| 519 |
}) |
|
| 520 |
} |
|
| 521 | ||
| 522 |
#' Summarize NAs. |
|
| 523 |
#' |
|
| 524 |
#' Summarizes occurrence of missing values in vector. |
|
| 525 |
#' @param x vector of any type and length |
|
| 526 |
#' @return Character string describing `NA` occurrence. |
|
| 527 |
#' @keywords internal |
|
| 528 |
var_missings_info <- function(x) {
|
|
| 529 | ! |
sprintf("%s [%s%%]", sum(is.na(x)), round(mean(is.na(x) * 100), 2))
|
| 530 |
} |
|
| 531 | ||
| 532 |
#' Summarizes variable |
|
| 533 |
#' |
|
| 534 |
#' Creates html summary with statistics relevant to data type. For numeric values it returns central |
|
| 535 |
#' tendency measures, for factor returns level counts, for Date date range, for other just |
|
| 536 |
#' number of levels. |
|
| 537 |
#' |
|
| 538 |
#' @param x vector of any type |
|
| 539 |
#' @param numeric_as_factor `logical` should the numeric variable be treated as a factor |
|
| 540 |
#' @param dt_rows `numeric` current/latest `DT` page length |
|
| 541 |
#' @param outlier_definition If 0 no outliers are removed, otherwise |
|
| 542 |
#' outliers (those more than `outlier_definition*IQR below/above Q1/Q3` be removed) |
|
| 543 |
#' @return text with simple statistics. |
|
| 544 |
#' @keywords internal |
|
| 545 |
var_summary_table <- function(x, numeric_as_factor, dt_rows, outlier_definition) {
|
|
| 546 | ! |
if (is.null(dt_rows)) {
|
| 547 | ! |
dt_rows <- 10 |
| 548 |
} |
|
| 549 | ! |
if (is.numeric(x) && !numeric_as_factor) {
|
| 550 | ! |
req(!any(is.infinite(x))) |
| 551 | ||
| 552 | ! |
x <- remove_outliers_from(x, outlier_definition) |
| 553 | ||
| 554 | ! |
qvals <- round(stats::quantile(x, na.rm = TRUE, probs = c(0.25, 0.5, 0.75), type = 2), 2) |
| 555 |
# classical central tendency measures |
|
| 556 | ||
| 557 | ! |
summary <- |
| 558 | ! |
data.frame( |
| 559 | ! |
Statistic = c("min", "Q1", "median", "mean", "Q3", "max", "sd", "n"),
|
| 560 | ! |
Value = c( |
| 561 | ! |
round(min(x, na.rm = TRUE), 2), |
| 562 | ! |
qvals[1], |
| 563 | ! |
qvals[2], |
| 564 | ! |
round(mean(x, na.rm = TRUE), 2), |
| 565 | ! |
qvals[3], |
| 566 | ! |
round(max(x, na.rm = TRUE), 2), |
| 567 | ! |
round(stats::sd(x, na.rm = TRUE), 2), |
| 568 | ! |
length(x[!is.na(x)]) |
| 569 |
) |
|
| 570 |
) |
|
| 571 | ||
| 572 | ! |
DT::datatable(summary, rownames = FALSE, options = list(dom = "<t>", pageLength = dt_rows)) |
| 573 | ! |
} else if (is.factor(x) || is.character(x) || (is.numeric(x) && numeric_as_factor) || is.logical(x)) {
|
| 574 |
# make sure factor is ordered numeric |
|
| 575 | ! |
if (is.numeric(x)) {
|
| 576 | ! |
x <- factor(x, levels = sort(unique(x))) |
| 577 |
} |
|
| 578 | ||
| 579 | ! |
level_counts <- table(x) |
| 580 | ! |
max_levels_signif <- nchar(level_counts) |
| 581 | ||
| 582 | ! |
if (!all(is.na(x))) {
|
| 583 | ! |
levels <- names(level_counts) |
| 584 | ! |
counts <- sprintf( |
| 585 | ! |
"%s [%.2f%%]", |
| 586 | ! |
format(level_counts, width = max_levels_signif), prop.table(level_counts) * 100 |
| 587 |
) |
|
| 588 |
} else {
|
|
| 589 | ! |
levels <- character(0) |
| 590 | ! |
counts <- numeric(0) |
| 591 |
} |
|
| 592 | ||
| 593 | ! |
summary <- data.frame( |
| 594 | ! |
Level = levels, |
| 595 | ! |
Count = counts, |
| 596 | ! |
stringsAsFactors = FALSE |
| 597 |
) |
|
| 598 | ||
| 599 |
# sort the dataset in decreasing order of counts (needed as character variables default to alphabetical) |
|
| 600 | ! |
summary <- summary[order(summary$Count, decreasing = TRUE), ] |
| 601 | ||
| 602 | ! |
dom_opts <- if (nrow(summary) <= 10) {
|
| 603 | ! |
"<t>" |
| 604 |
} else {
|
|
| 605 | ! |
"<lf<t>ip>" |
| 606 |
} |
|
| 607 | ! |
DT::datatable(summary, rownames = FALSE, options = list(dom = dom_opts, pageLength = dt_rows)) |
| 608 | ! |
} else if (inherits(x, "Date") || inherits(x, "POSIXct") || inherits(x, "POSIXlt")) {
|
| 609 | ! |
summary <- |
| 610 | ! |
data.frame( |
| 611 | ! |
Statistic = c("min", "median", "max"),
|
| 612 | ! |
Value = c( |
| 613 | ! |
min(x, na.rm = TRUE), |
| 614 | ! |
stats::median(x, na.rm = TRUE), |
| 615 | ! |
max(x, na.rm = TRUE) |
| 616 |
) |
|
| 617 |
) |
|
| 618 | ! |
DT::datatable(summary, rownames = FALSE, options = list(dom = "<t>", pageLength = dt_rows)) |
| 619 |
} else {
|
|
| 620 | ! |
NULL |
| 621 |
} |
|
| 622 |
} |
|
| 623 | ||
| 624 |
#' Plot variable |
|
| 625 |
#' |
|
| 626 |
#' Creates summary plot with statistics relevant to data type. |
|
| 627 |
#' |
|
| 628 |
#' @inheritParams shared_params |
|
| 629 |
#' @param var vector of any type to be plotted. For numeric variables it produces histogram with |
|
| 630 |
#' density line, for factors it creates frequency plot |
|
| 631 |
#' @param var_lab text describing selected variable to be displayed on the plot |
|
| 632 |
#' @param wrap_character (`numeric`) number of characters at which to wrap text values of `var` |
|
| 633 |
#' @param numeric_as_factor (`logical`) should the numeric variable be treated as a factor |
|
| 634 |
#' @param display_density (`logical`) should density estimation be displayed for numeric values |
|
| 635 |
#' @param remove_NA_hist (`logical`) should `NA` values be removed for histogram of factor like variables |
|
| 636 |
#' @param outlier_definition if 0 no outliers are removed, otherwise |
|
| 637 |
#' outliers (those more than outlier_definition*IQR below/above Q1/Q3 be removed) |
|
| 638 |
#' @param records_for_factor (`numeric`) if the number of factor levels is >= than this value then |
|
| 639 |
#' a graph of the factors isn't shown, only a list of values |
|
| 640 |
#' |
|
| 641 |
#' @return plot |
|
| 642 |
#' @keywords internal |
|
| 643 |
plot_var_summary <- function(var, |
|
| 644 |
var_lab, |
|
| 645 |
wrap_character = NULL, |
|
| 646 |
numeric_as_factor, |
|
| 647 |
display_density = is.numeric(var), |
|
| 648 |
remove_NA_hist = FALSE, # nolint: object_name. |
|
| 649 |
outlier_definition, |
|
| 650 |
records_for_factor, |
|
| 651 |
ggplot2_args) {
|
|
| 652 | ! |
checkmate::assert_character(var_lab) |
| 653 | ! |
checkmate::assert_numeric(wrap_character, null.ok = TRUE) |
| 654 | ! |
checkmate::assert_flag(numeric_as_factor) |
| 655 | ! |
checkmate::assert_flag(display_density) |
| 656 | ! |
checkmate::assert_logical(remove_NA_hist, null.ok = TRUE) |
| 657 | ! |
checkmate::assert_number(outlier_definition, lower = 0, finite = TRUE) |
| 658 | ! |
checkmate::assert_integerish(records_for_factor, lower = 0, len = 1, any.missing = FALSE) |
| 659 | ! |
checkmate::assert_class(ggplot2_args, "ggplot2_args") |
| 660 | ||
| 661 | ! |
grid::grid.newpage() |
| 662 | ||
| 663 | ! |
plot_main <- if (is.factor(var) || is.character(var) || is.logical(var)) {
|
| 664 | ! |
groups <- unique(as.character(var)) |
| 665 | ! |
len_groups <- length(groups) |
| 666 | ! |
if (len_groups >= records_for_factor) {
|
| 667 | ! |
grid::textGrob( |
| 668 | ! |
sprintf( |
| 669 | ! |
"%s unique values\n%s:\n %s\n ...\n %s", |
| 670 | ! |
len_groups, |
| 671 | ! |
var_lab, |
| 672 | ! |
paste(utils::head(groups), collapse = ",\n "), |
| 673 | ! |
paste(utils::tail(groups), collapse = ",\n ") |
| 674 |
), |
|
| 675 | ! |
x = grid::unit(1, "line"), |
| 676 | ! |
y = grid::unit(1, "npc") - grid::unit(1, "line"), |
| 677 | ! |
just = c("left", "top")
|
| 678 |
) |
|
| 679 |
} else {
|
|
| 680 | ! |
if (!is.null(wrap_character)) {
|
| 681 | ! |
var <- stringr::str_wrap(var, width = wrap_character) |
| 682 |
} |
|
| 683 | ! |
var <- if (isTRUE(remove_NA_hist)) as.vector(stats::na.omit(var)) else var |
| 684 | ! |
ggplot2::ggplot(data.frame(var), ggplot2::aes(x = forcats::fct_infreq(as.factor(var)))) + |
| 685 | ! |
ggplot2::geom_bar( |
| 686 | ! |
stat = "count", ggplot2::aes(fill = ifelse(is.na(var), "withcolor", "")), show.legend = FALSE |
| 687 |
) + |
|
| 688 | ! |
ggplot2::scale_fill_manual(values = c("gray50", "tan"))
|
| 689 |
} |
|
| 690 | ! |
} else if (is.numeric(var)) {
|
| 691 | ! |
validate(need(any(!is.na(var)), "No data left to visualize.")) |
| 692 | ||
| 693 |
# Filter out NA |
|
| 694 | ! |
var <- var[which(!is.na(var))] |
| 695 | ||
| 696 | ! |
validate(need(!any(is.infinite(var)), "Cannot display graph when data includes infinite values")) |
| 697 | ||
| 698 | ! |
if (numeric_as_factor) {
|
| 699 | ! |
var <- factor(var) |
| 700 | ! |
ggplot2::ggplot(NULL, ggplot2::aes(x = var)) + |
| 701 | ! |
ggplot2::geom_histogram(stat = "count") |
| 702 |
} else {
|
|
| 703 |
# remove outliers |
|
| 704 | ! |
if (outlier_definition != 0) {
|
| 705 | ! |
number_records <- length(var) |
| 706 | ! |
var <- remove_outliers_from(var, outlier_definition) |
| 707 | ! |
number_outliers <- number_records - length(var) |
| 708 | ! |
outlier_text <- paste0( |
| 709 | ! |
number_outliers, " outliers (",
|
| 710 | ! |
round(number_outliers / number_records * 100, 2), |
| 711 | ! |
"% of non-missing records) not shown" |
| 712 |
) |
|
| 713 | ! |
validate(need( |
| 714 | ! |
length(var) > 1, |
| 715 | ! |
"At least two data points must remain after removing outliers for this graph to be displayed" |
| 716 |
)) |
|
| 717 |
} |
|
| 718 |
## histogram |
|
| 719 | ! |
binwidth <- get_bin_width(var) |
| 720 | ! |
p <- ggplot2::ggplot(data = data.frame(var = var), ggplot2::aes(x = var, y = ggplot2::after_stat(count))) + |
| 721 | ! |
ggplot2::geom_histogram(binwidth = binwidth) + |
| 722 | ! |
ggplot2::scale_y_continuous( |
| 723 | ! |
sec.axis = ggplot2::sec_axis( |
| 724 | ! |
trans = ~ . / nrow(data.frame(var = var)), |
| 725 | ! |
labels = scales::percent, |
| 726 | ! |
name = "proportion (in %)" |
| 727 |
) |
|
| 728 |
) |
|
| 729 | ||
| 730 | ! |
if (display_density) {
|
| 731 | ! |
p <- p + ggplot2::geom_density(ggplot2::aes(y = ggplot2::after_stat(count * binwidth))) |
| 732 |
} |
|
| 733 | ||
| 734 | ! |
if (outlier_definition != 0) {
|
| 735 | ! |
p <- p + ggplot2::annotate( |
| 736 | ! |
geom = "text", |
| 737 | ! |
label = outlier_text, |
| 738 | ! |
x = Inf, y = Inf, |
| 739 | ! |
hjust = 1.02, vjust = 1.2, |
| 740 | ! |
color = "black", |
| 741 |
# explicitly modify geom text size according |
|
| 742 | ! |
size = ggplot2_args[["theme"]][["text"]][["size"]] / 3.5 |
| 743 |
) |
|
| 744 |
} |
|
| 745 | ! |
p |
| 746 |
} |
|
| 747 | ! |
} else if (inherits(var, "Date") || inherits(var, "POSIXct") || inherits(var, "POSIXlt")) {
|
| 748 | ! |
var_num <- as.numeric(var) |
| 749 | ! |
binwidth <- get_bin_width(var_num, 1) |
| 750 | ! |
p <- ggplot2::ggplot(data = data.frame(var = var), ggplot2::aes(x = var, y = ggplot2::after_stat(count))) + |
| 751 | ! |
ggplot2::geom_histogram(binwidth = binwidth) |
| 752 |
} else {
|
|
| 753 | ! |
grid::textGrob( |
| 754 | ! |
paste(strwrap( |
| 755 | ! |
utils::capture.output(utils::str(var)), |
| 756 | ! |
width = .9 * grid::convertWidth(grid::unit(1, "npc"), "char", TRUE) |
| 757 | ! |
), collapse = "\n"), |
| 758 | ! |
x = grid::unit(1, "line"), y = grid::unit(1, "npc") - grid::unit(1, "line"), just = c("left", "top")
|
| 759 |
) |
|
| 760 |
} |
|
| 761 | ||
| 762 | ! |
dev_ggplot2_args <- teal.widgets::ggplot2_args( |
| 763 | ! |
labs = list(x = var_lab) |
| 764 |
) |
|
| 765 |
### |
|
| 766 | ! |
all_ggplot2_args <- teal.widgets::resolve_ggplot2_args( |
| 767 | ! |
ggplot2_args, |
| 768 | ! |
module_plot = dev_ggplot2_args |
| 769 |
) |
|
| 770 | ||
| 771 | ! |
if (is.ggplot(plot_main)) {
|
| 772 | ! |
if (is.numeric(var) && !numeric_as_factor) {
|
| 773 |
# numeric not as factor |
|
| 774 | ! |
plot_main <- plot_main + |
| 775 | ! |
theme_light() + |
| 776 | ! |
list( |
| 777 | ! |
labs = do.call("labs", all_ggplot2_args$labs),
|
| 778 | ! |
theme = do.call("theme", all_ggplot2_args$theme)
|
| 779 |
) |
|
| 780 |
} else {
|
|
| 781 |
# factor low number of levels OR numeric as factor OR Date |
|
| 782 | ! |
plot_main <- plot_main + |
| 783 | ! |
theme_light() + |
| 784 | ! |
list( |
| 785 | ! |
labs = do.call("labs", all_ggplot2_args$labs),
|
| 786 | ! |
theme = do.call("theme", all_ggplot2_args$theme)
|
| 787 |
) |
|
| 788 |
} |
|
| 789 | ! |
plot_main <- ggplot2::ggplotGrob(plot_main) |
| 790 |
} |
|
| 791 | ||
| 792 | ! |
grid::grid.draw(plot_main) |
| 793 | ! |
plot_main |
| 794 |
} |
|
| 795 | ||
| 796 |
is_num_var_short <- function(.unique_records_for_factor, input, data_for_analysis) {
|
|
| 797 | ! |
length(unique(data_for_analysis()$data)) < .unique_records_for_factor && !is.null(input$numeric_as_factor) |
| 798 |
} |
|
| 799 | ||
| 800 |
#' Validates the variable browser inputs |
|
| 801 |
#' |
|
| 802 |
#' @param input (`session$input`) the `shiny` session input |
|
| 803 |
#' @param plot_var (`list`) list of a data frame and an array of variable names |
|
| 804 |
#' @param data (`teal_data`) the datasets passed to the module |
|
| 805 |
#' |
|
| 806 |
#' @returns `logical` TRUE if validations pass; a `shiny` validation error otherwise |
|
| 807 |
#' @keywords internal |
|
| 808 |
validate_input <- function(input, plot_var, data) {
|
|
| 809 | ! |
reactive({
|
| 810 | ! |
dataset_name <- req(input$tabset_panel) |
| 811 | ! |
varname <- plot_var$variable[[dataset_name]] |
| 812 | ||
| 813 | ! |
validate(need(dataset_name, "No data selected")) |
| 814 | ! |
validate(need(varname, "No variable selected")) |
| 815 | ! |
df <- data()[[dataset_name]] |
| 816 | ! |
teal::validate_has_data(df, 1) |
| 817 | ! |
teal::validate_has_variable(varname = varname, data = df, "Variable not available") |
| 818 | ||
| 819 | ! |
TRUE |
| 820 |
}) |
|
| 821 |
} |
|
| 822 | ||
| 823 |
get_plotted_data <- function(input, plot_var, data) {
|
|
| 824 | ! |
dataset_name <- input$tabset_panel |
| 825 | ! |
varname <- plot_var$variable[[dataset_name]] |
| 826 | ! |
df <- data()[[dataset_name]] |
| 827 | ||
| 828 | ! |
var_description <- teal.data::col_labels(df)[[varname]] |
| 829 | ! |
list(data = df[[varname]], var_description = var_description) |
| 830 |
} |
|
| 831 | ||
| 832 |
#' Renders the left-hand side `tabset` panel of the module |
|
| 833 |
#' |
|
| 834 |
#' @param datanames (`character`) the name of the dataset |
|
| 835 |
#' @param parent_dataname (`character`) the name of a parent `dataname` to filter out variables from |
|
| 836 |
#' @param data (`teal_data`) the object containing all datasets |
|
| 837 |
#' @param input (`session$input`) the `shiny` session input |
|
| 838 |
#' @param output (`session$output`) the `shiny` session output |
|
| 839 |
#' @param columns_names (`environment`) the environment containing bindings for each dataset |
|
| 840 |
#' @param plot_var (`list`) the list containing the currently selected dataset (tab) and its column names |
|
| 841 |
#' @keywords internal |
|
| 842 |
render_tabset_panel_content <- function(datanames, parent_dataname, output, data, input, columns_names, plot_var) {
|
|
| 843 | ! |
lapply(datanames, render_single_tab, |
| 844 | ! |
input = input, |
| 845 | ! |
output = output, |
| 846 | ! |
data = data, |
| 847 | ! |
parent_dataname = parent_dataname, |
| 848 | ! |
columns_names = columns_names, |
| 849 | ! |
plot_var = plot_var |
| 850 |
) |
|
| 851 |
} |
|
| 852 | ||
| 853 |
#' Renders a single tab in the left-hand side tabset panel |
|
| 854 |
#' |
|
| 855 |
#' Renders a single tab in the left-hand side tabset panel. The rendered tab contains |
|
| 856 |
#' information about one dataset out of many presented in the module. |
|
| 857 |
#' |
|
| 858 |
#' @param dataset_name (`character`) the name of the dataset contained in the rendered tab |
|
| 859 |
#' @param parent_dataname (`character`) the name of a parent `dataname` to filter out variables from |
|
| 860 |
#' @inheritParams render_tabset_panel_content |
|
| 861 |
#' @keywords internal |
|
| 862 |
render_single_tab <- function(dataset_name, parent_dataname, output, data, input, columns_names, plot_var) {
|
|
| 863 | ! |
render_tab_header(dataset_name, output, data) |
| 864 | ||
| 865 | ! |
render_tab_table( |
| 866 | ! |
dataset_name = dataset_name, |
| 867 | ! |
parent_dataname = parent_dataname, |
| 868 | ! |
output = output, |
| 869 | ! |
data = data, |
| 870 | ! |
input = input, |
| 871 | ! |
columns_names = columns_names, |
| 872 | ! |
plot_var = plot_var |
| 873 |
) |
|
| 874 |
} |
|
| 875 | ||
| 876 |
#' Renders the text headlining a single tab in the left-hand side tabset panel |
|
| 877 |
#' |
|
| 878 |
#' @param dataset_name (`character`) the name of the dataset of the tab |
|
| 879 |
#' @inheritParams render_tabset_panel_content |
|
| 880 |
#' @keywords internal |
|
| 881 |
render_tab_header <- function(dataset_name, output, data) {
|
|
| 882 | ! |
dataset_ui_id <- paste0("dataset_summary_", dataset_name)
|
| 883 | ! |
output[[dataset_ui_id]] <- renderText({
|
| 884 | ! |
df <- data()[[dataset_name]] |
| 885 | ! |
join_keys <- teal.data::join_keys(data()) |
| 886 | ! |
if (!is.null(join_keys)) {
|
| 887 | ! |
key <- teal.data::join_keys(data())[dataset_name, dataset_name] |
| 888 |
} else {
|
|
| 889 | ! |
key <- NULL |
| 890 |
} |
|
| 891 | ! |
sprintf( |
| 892 | ! |
"Dataset with %s unique key rows and %s variables", |
| 893 | ! |
nrow(unique(`if`(length(key) > 0, df[, key, drop = FALSE], df))), |
| 894 | ! |
ncol(df) |
| 895 |
) |
|
| 896 |
}) |
|
| 897 |
} |
|
| 898 | ||
| 899 |
#' Renders the table for a single dataset in the left-hand side tabset panel |
|
| 900 |
#' |
|
| 901 |
#' The table contains column names, column labels, |
|
| 902 |
#' small summary about NA values and `sparkline` (if appropriate). |
|
| 903 |
#' |
|
| 904 |
#' @param dataset_name (`character`) the name of the dataset |
|
| 905 |
#' @param parent_dataname (`character`) the name of a parent `dataname` to filter out variables from |
|
| 906 |
#' @inheritParams render_tabset_panel_content |
|
| 907 |
#' @keywords internal |
|
| 908 |
render_tab_table <- function(dataset_name, parent_dataname, output, data, input, columns_names, plot_var) {
|
|
| 909 | ! |
table_ui_id <- paste0("variable_browser_", dataset_name)
|
| 910 | ||
| 911 | ! |
output[[table_ui_id]] <- DT::renderDataTable({
|
| 912 | ! |
df <- data()[[dataset_name]] |
| 913 | ||
| 914 | ! |
get_vars_df <- function(input, dataset_name, parent_name, data) {
|
| 915 | ! |
data_cols <- colnames(df) |
| 916 | ! |
if (isTRUE(input$show_parent_vars)) {
|
| 917 | ! |
data_cols |
| 918 | ! |
} else if (dataset_name != parent_name && parent_name %in% names(data)) {
|
| 919 | ! |
setdiff(data_cols, colnames(data()[[parent_name]])) |
| 920 |
} else {
|
|
| 921 | ! |
data_cols |
| 922 |
} |
|
| 923 |
} |
|
| 924 | ||
| 925 | ! |
if (length(parent_dataname) > 0) {
|
| 926 | ! |
df_vars <- get_vars_df(input, dataset_name, parent_dataname, data) |
| 927 | ! |
df <- df[df_vars] |
| 928 |
} |
|
| 929 | ||
| 930 | ! |
if (is.null(df) || ncol(df) == 0) {
|
| 931 | ! |
columns_names[[dataset_name]] <- character(0) |
| 932 | ! |
df_output <- data.frame( |
| 933 | ! |
Type = character(0), |
| 934 | ! |
Variable = character(0), |
| 935 | ! |
Label = character(0), |
| 936 | ! |
Missings = character(0), |
| 937 | ! |
Sparklines = character(0), |
| 938 | ! |
stringsAsFactors = FALSE |
| 939 |
) |
|
| 940 |
} else {
|
|
| 941 |
# extract data variable labels |
|
| 942 | ! |
labels <- teal.data::col_labels(df) |
| 943 | ||
| 944 | ! |
columns_names[[dataset_name]] <- names(labels) |
| 945 | ||
| 946 |
# calculate number of missing values |
|
| 947 | ! |
missings <- vapply( |
| 948 | ! |
df, |
| 949 | ! |
var_missings_info, |
| 950 | ! |
FUN.VALUE = character(1), |
| 951 | ! |
USE.NAMES = FALSE |
| 952 |
) |
|
| 953 | ||
| 954 |
# get icons proper for the data types |
|
| 955 | ! |
icons <- vapply(df, function(x) class(x)[1L], character(1L)) |
| 956 | ||
| 957 | ! |
join_keys <- teal.data::join_keys(data()) |
| 958 | ! |
if (!is.null(join_keys)) {
|
| 959 | ! |
icons[intersect(join_keys[dataset_name, dataset_name], colnames(df))] <- "primary_key" |
| 960 |
} |
|
| 961 | ! |
icons <- variable_type_icons(icons) |
| 962 | ||
| 963 |
# generate sparklines |
|
| 964 | ! |
sparklines_html <- vapply( |
| 965 | ! |
df, |
| 966 | ! |
create_sparklines, |
| 967 | ! |
FUN.VALUE = character(1), |
| 968 | ! |
USE.NAMES = FALSE |
| 969 |
) |
|
| 970 | ||
| 971 | ! |
df_output <- data.frame( |
| 972 | ! |
Type = icons, |
| 973 | ! |
Variable = names(labels), |
| 974 | ! |
Label = labels, |
| 975 | ! |
Missings = missings, |
| 976 | ! |
Sparklines = sparklines_html, |
| 977 | ! |
stringsAsFactors = FALSE |
| 978 |
) |
|
| 979 |
} |
|
| 980 | ||
| 981 |
# Select row 1 as default / fallback |
|
| 982 | ! |
selected_ix <- 1 |
| 983 |
# Define starting page index (base-0 index of the first item on page |
|
| 984 |
# note: in many cases it's not the item itself |
|
| 985 | ! |
selected_page_ix <- 0 |
| 986 | ||
| 987 |
# Retrieve current selected variable if any |
|
| 988 | ! |
isolated_variable <- isolate(plot_var$variable[[dataset_name]]) |
| 989 | ||
| 990 | ! |
if (!is.null(isolated_variable)) {
|
| 991 | ! |
index <- which(columns_names[[dataset_name]] == isolated_variable)[1] |
| 992 | ! |
if (!is.null(index) && !is.na(index) && length(index) > 0) selected_ix <- index |
| 993 |
} |
|
| 994 | ||
| 995 |
# Retrieve the index of the first item of the current page |
|
| 996 |
# it works with varying number of entries on the page (10, 25, ...) |
|
| 997 | ! |
table_id_sel <- paste0("variable_browser_", dataset_name, "_state")
|
| 998 | ! |
dt_state <- isolate(input[[table_id_sel]]) |
| 999 | ! |
if (selected_ix != 1 && !is.null(dt_state)) {
|
| 1000 | ! |
selected_page_ix <- floor(selected_ix / dt_state$length) * dt_state$length |
| 1001 |
} |
|
| 1002 | ||
| 1003 | ! |
DT::datatable( |
| 1004 | ! |
df_output, |
| 1005 | ! |
escape = FALSE, |
| 1006 | ! |
rownames = FALSE, |
| 1007 | ! |
selection = list(mode = "single", target = "row", selected = selected_ix), |
| 1008 | ! |
options = list( |
| 1009 | ! |
fnDrawCallback = htmlwidgets::JS("function() { HTMLWidgets.staticRender(); }"),
|
| 1010 | ! |
pageLength = input[[paste0(table_ui_id, "_rows")]], |
| 1011 | ! |
displayStart = selected_page_ix |
| 1012 |
) |
|
| 1013 |
) |
|
| 1014 |
}) |
|
| 1015 |
} |
|
| 1016 | ||
| 1017 |
#' Creates observers updating the currently selected column |
|
| 1018 |
#' |
|
| 1019 |
#' The created observers update the column currently selected in the left-hand side |
|
| 1020 |
#' tabset panel. |
|
| 1021 |
#' |
|
| 1022 |
#' @note |
|
| 1023 |
#' Creates an observer for each dataset (each tab in the tabset panel). |
|
| 1024 |
#' |
|
| 1025 |
#' @inheritParams render_tabset_panel_content |
|
| 1026 |
#' @keywords internal |
|
| 1027 |
establish_updating_selection <- function(datanames, input, plot_var, columns_names) {
|
|
| 1028 | ! |
lapply(datanames, function(dataset_name) {
|
| 1029 | ! |
table_ui_id <- paste0("variable_browser_", dataset_name)
|
| 1030 | ! |
table_id_sel <- paste0(table_ui_id, "_rows_selected") |
| 1031 | ! |
observeEvent(input[[table_id_sel]], {
|
| 1032 | ! |
plot_var$data <- dataset_name |
| 1033 | ! |
plot_var$variable[[dataset_name]] <- columns_names[[dataset_name]][input[[table_id_sel]]] |
| 1034 |
}) |
|
| 1035 |
}) |
|
| 1036 |
} |
|
| 1037 | ||
| 1038 |
get_bin_width <- function(x_vec, scaling_factor = 2) {
|
|
| 1039 | ! |
x_vec <- x_vec[!is.na(x_vec)] |
| 1040 | ! |
qntls <- stats::quantile(x_vec, probs = c(0.1, 0.25, 0.75, 0.9), type = 2) |
| 1041 | ! |
iqr <- qntls[3] - qntls[2] |
| 1042 | ! |
binwidth <- max(scaling_factor * iqr / length(x_vec) ^ (1 / 3), sqrt(qntls[4] - qntls[1])) # styler: off |
| 1043 | ! |
binwidth <- ifelse(binwidth == 0, 1, binwidth) |
| 1044 |
# to ensure at least two bins when variable span is very small |
|
| 1045 | ! |
x_span <- diff(range(x_vec)) |
| 1046 | ! |
if (isTRUE(x_span / binwidth >= 2)) binwidth else x_span / 2 |
| 1047 |
} |
|
| 1048 | ||
| 1049 |
#' Removes the outlier observation from an array |
|
| 1050 |
#' |
|
| 1051 |
#' @param var (`numeric`) a numeric vector |
|
| 1052 |
#' @param outlier_definition (`numeric`) if `0` then no outliers are removed, otherwise |
|
| 1053 |
#' outliers (those more than `outlier_definition*IQR below/above Q1/Q3`) are removed |
|
| 1054 |
#' @returns (`numeric`) vector without the outlier values |
|
| 1055 |
#' @keywords internal |
|
| 1056 |
remove_outliers_from <- function(var, outlier_definition) {
|
|
| 1057 | 3x |
if (outlier_definition == 0) {
|
| 1058 | 1x |
return(var) |
| 1059 |
} |
|
| 1060 | 2x |
q1_q3 <- stats::quantile(var, probs = c(0.25, 0.75), type = 2, na.rm = TRUE) |
| 1061 | 2x |
iqr <- q1_q3[2] - q1_q3[1] |
| 1062 | 2x |
var[var >= q1_q3[1] - outlier_definition * iqr & var <= q1_q3[2] + outlier_definition * iqr] |
| 1063 |
} |
|
| 1064 | ||
| 1065 | ||
| 1066 |
# sparklines ---- |
|
| 1067 | ||
| 1068 |
#' S3 generic for `sparkline` widget HTML |
|
| 1069 |
#' |
|
| 1070 |
#' Generates the `sparkline` HTML code corresponding to the input array. |
|
| 1071 |
#' For numeric variables creates a box plot, for character and factors - bar plot. |
|
| 1072 |
#' Produces an empty string for variables of other types. |
|
| 1073 |
#' |
|
| 1074 |
#' @param arr vector of any type and length |
|
| 1075 |
#' @param width `numeric` the width of the `sparkline` widget (pixels) |
|
| 1076 |
#' @param bar_spacing `numeric` the spacing between the bars (in pixels) |
|
| 1077 |
#' @param bar_width `numeric` the width of the bars (in pixels) |
|
| 1078 |
#' @param ... `list` additional options passed to bar plots of `jquery.sparkline`; |
|
| 1079 |
#' see [`jquery.sparkline docs`](https://omnipotent.net/jquery.sparkline/#common) |
|
| 1080 |
#' |
|
| 1081 |
#' @return Character string containing HTML code of the `sparkline` HTML widget. |
|
| 1082 |
#' @keywords internal |
|
| 1083 |
create_sparklines <- function(arr, width = 150, ...) {
|
|
| 1084 | ! |
if (all(is.null(arr))) {
|
| 1085 | ! |
return("")
|
| 1086 |
} |
|
| 1087 | ! |
UseMethod("create_sparklines")
|
| 1088 |
} |
|
| 1089 | ||
| 1090 |
#' @rdname create_sparklines |
|
| 1091 |
#' @keywords internal |
|
| 1092 |
#' @export |
|
| 1093 |
create_sparklines.logical <- function(arr, ...) {
|
|
| 1094 | ! |
create_sparklines(as.factor(arr)) |
| 1095 |
} |
|
| 1096 | ||
| 1097 |
#' @rdname create_sparklines |
|
| 1098 |
#' @keywords internal |
|
| 1099 |
#' @export |
|
| 1100 |
create_sparklines.numeric <- function(arr, width = 150, ...) {
|
|
| 1101 | ! |
if (any(is.infinite(arr))) {
|
| 1102 | ! |
return(as.character(tags$code("infinite values", class = "text-blue")))
|
| 1103 |
} |
|
| 1104 | ! |
if (length(arr) > 100000) {
|
| 1105 | ! |
return(as.character(tags$code("Too many rows (>100000)", class = "text-blue")))
|
| 1106 |
} |
|
| 1107 | ||
| 1108 | ! |
arr <- arr[!is.na(arr)] |
| 1109 | ! |
sparkline::spk_chr(unname(arr), type = "box", width = width, ...) |
| 1110 |
} |
|
| 1111 | ||
| 1112 |
#' @rdname create_sparklines |
|
| 1113 |
#' @keywords internal |
|
| 1114 |
#' @export |
|
| 1115 |
create_sparklines.character <- function(arr, ...) {
|
|
| 1116 | ! |
create_sparklines(as.factor(arr)) |
| 1117 |
} |
|
| 1118 | ||
| 1119 | ||
| 1120 |
#' @rdname create_sparklines |
|
| 1121 |
#' @keywords internal |
|
| 1122 |
#' @export |
|
| 1123 |
create_sparklines.factor <- function(arr, width = 150, bar_spacing = 5, bar_width = 20, ...) {
|
|
| 1124 | ! |
decreasing_order <- TRUE |
| 1125 | ||
| 1126 | ! |
counts <- table(arr) |
| 1127 | ! |
if (length(counts) >= 100) {
|
| 1128 | ! |
return(as.character(tags$code("> 99 levels", class = "text-blue")))
|
| 1129 | ! |
} else if (length(counts) == 0) {
|
| 1130 | ! |
return(as.character(tags$code("no levels", class = "text-blue")))
|
| 1131 | ! |
} else if (length(counts) == 1) {
|
| 1132 | ! |
return(as.character(tags$code("one level", class = "text-blue")))
|
| 1133 |
} |
|
| 1134 | ||
| 1135 |
# Summarize the occurences of different levels |
|
| 1136 |
# and get the maximum and minimum number of occurences |
|
| 1137 |
# This is needed for the sparkline to correctly display the bar plots |
|
| 1138 |
# Otherwise they are cropped |
|
| 1139 | ! |
counts <- sort(counts, decreasing = decreasing_order, method = "radix") |
| 1140 | ! |
max_value <- if (decreasing_order) counts[1] else counts[length[counts]] |
| 1141 | ! |
max_value <- unname(max_value) |
| 1142 | ||
| 1143 | ! |
sparkline::spk_chr( |
| 1144 | ! |
unname(counts), |
| 1145 | ! |
type = "bar", |
| 1146 | ! |
chartRangeMin = 0, |
| 1147 | ! |
chartRangeMax = max_value, |
| 1148 | ! |
width = width, |
| 1149 | ! |
barWidth = bar_width, |
| 1150 | ! |
barSpacing = bar_spacing, |
| 1151 | ! |
tooltipFormatter = custom_sparkline_formatter(names(counts), as.vector(counts)) |
| 1152 |
) |
|
| 1153 |
} |
|
| 1154 | ||
| 1155 |
#' @rdname create_sparklines |
|
| 1156 |
#' @keywords internal |
|
| 1157 |
#' @export |
|
| 1158 |
create_sparklines.Date <- function(arr, width = 150, bar_spacing = 5, bar_width = 20, ...) {
|
|
| 1159 | ! |
arr_num <- as.numeric(arr) |
| 1160 | ! |
arr_num <- sort(arr_num, decreasing = FALSE, method = "radix") |
| 1161 | ! |
binwidth <- get_bin_width(arr_num, 1) |
| 1162 | ! |
bins <- floor(diff(range(arr_num)) / binwidth) + 1 |
| 1163 | ! |
if (all(is.na(bins))) {
|
| 1164 | ! |
return(as.character(tags$code("only NA", class = "text-blue")))
|
| 1165 | ! |
} else if (bins == 1) {
|
| 1166 | ! |
return(as.character(tags$code("one date", class = "text-blue")))
|
| 1167 |
} |
|
| 1168 | ! |
counts <- as.vector(unname(base::table(cut(arr_num, breaks = bins)))) |
| 1169 | ! |
max_value <- max(counts) |
| 1170 | ||
| 1171 | ! |
start_bins <- as.integer(seq(1, length(arr_num), length.out = bins)) |
| 1172 | ! |
labels_start <- as.character(as.Date(arr_num[start_bins], origin = as.Date("1970-01-01")))
|
| 1173 | ! |
labels <- paste("Start:", labels_start)
|
| 1174 | ||
| 1175 | ! |
sparkline::spk_chr( |
| 1176 | ! |
unname(counts), |
| 1177 | ! |
type = "bar", |
| 1178 | ! |
chartRangeMin = 0, |
| 1179 | ! |
chartRangeMax = max_value, |
| 1180 | ! |
width = width, |
| 1181 | ! |
barWidth = bar_width, |
| 1182 | ! |
barSpacing = bar_spacing, |
| 1183 | ! |
tooltipFormatter = custom_sparkline_formatter(labels, counts) |
| 1184 |
) |
|
| 1185 |
} |
|
| 1186 | ||
| 1187 |
#' @rdname create_sparklines |
|
| 1188 |
#' @keywords internal |
|
| 1189 |
#' @export |
|
| 1190 |
create_sparklines.POSIXct <- function(arr, width = 150, bar_spacing = 5, bar_width = 20, ...) {
|
|
| 1191 | ! |
arr_num <- as.numeric(arr) |
| 1192 | ! |
arr_num <- sort(arr_num, decreasing = FALSE, method = "radix") |
| 1193 | ! |
binwidth <- get_bin_width(arr_num, 1) |
| 1194 | ! |
bins <- floor(diff(range(arr_num)) / binwidth) + 1 |
| 1195 | ! |
if (all(is.na(bins))) {
|
| 1196 | ! |
return(as.character(tags$code("only NA", class = "text-blue")))
|
| 1197 | ! |
} else if (bins == 1) {
|
| 1198 | ! |
return(as.character(tags$code("one date-time", class = "text-blue")))
|
| 1199 |
} |
|
| 1200 | ! |
counts <- as.vector(unname(base::table(cut(arr_num, breaks = bins)))) |
| 1201 | ! |
max_value <- max(counts) |
| 1202 | ||
| 1203 | ! |
start_bins <- as.integer(seq(1, length(arr_num), length.out = bins)) |
| 1204 | ! |
labels_start <- as.character(format(as.POSIXct(arr_num[start_bins], origin = as.Date("1970-01-01")), "%Y-%m-%d"))
|
| 1205 | ! |
labels <- paste("Start:", labels_start)
|
| 1206 | ||
| 1207 | ! |
sparkline::spk_chr( |
| 1208 | ! |
unname(counts), |
| 1209 | ! |
type = "bar", |
| 1210 | ! |
chartRangeMin = 0, |
| 1211 | ! |
chartRangeMax = max_value, |
| 1212 | ! |
width = width, |
| 1213 | ! |
barWidth = bar_width, |
| 1214 | ! |
barSpacing = bar_spacing, |
| 1215 | ! |
tooltipFormatter = custom_sparkline_formatter(labels, counts) |
| 1216 |
) |
|
| 1217 |
} |
|
| 1218 | ||
| 1219 |
#' @rdname create_sparklines |
|
| 1220 |
#' @keywords internal |
|
| 1221 |
#' @export |
|
| 1222 |
create_sparklines.POSIXlt <- function(arr, width = 150, bar_spacing = 5, bar_width = 20, ...) {
|
|
| 1223 | ! |
arr_num <- as.numeric(arr) |
| 1224 | ! |
arr_num <- sort(arr_num, decreasing = FALSE, method = "radix") |
| 1225 | ! |
binwidth <- get_bin_width(arr_num, 1) |
| 1226 | ! |
bins <- floor(diff(range(arr_num)) / binwidth) + 1 |
| 1227 | ! |
if (all(is.na(bins))) {
|
| 1228 | ! |
return(as.character(tags$code("only NA", class = "text-blue")))
|
| 1229 | ! |
} else if (bins == 1) {
|
| 1230 | ! |
return(as.character(tags$code("one date-time", class = "text-blue")))
|
| 1231 |
} |
|
| 1232 | ! |
counts <- as.vector(unname(base::table(cut(arr_num, breaks = bins)))) |
| 1233 | ! |
max_value <- max(counts) |
| 1234 | ||
| 1235 | ! |
start_bins <- as.integer(seq(1, length(arr_num), length.out = bins)) |
| 1236 | ! |
labels_start <- as.character(format(as.POSIXct(arr_num[start_bins], origin = as.Date("1970-01-01")), "%Y-%m-%d"))
|
| 1237 | ! |
labels <- paste("Start:", labels_start)
|
| 1238 | ||
| 1239 | ! |
sparkline::spk_chr( |
| 1240 | ! |
unname(counts), |
| 1241 | ! |
type = "bar", |
| 1242 | ! |
chartRangeMin = 0, |
| 1243 | ! |
chartRangeMax = max_value, |
| 1244 | ! |
width = width, |
| 1245 | ! |
barWidth = bar_width, |
| 1246 | ! |
barSpacing = bar_spacing, |
| 1247 | ! |
tooltipFormatter = custom_sparkline_formatter(labels, counts) |
| 1248 |
) |
|
| 1249 |
} |
|
| 1250 | ||
| 1251 |
#' @rdname create_sparklines |
|
| 1252 |
#' @keywords internal |
|
| 1253 |
#' @export |
|
| 1254 |
create_sparklines.default <- function(arr, width = 150, ...) {
|
|
| 1255 | ! |
as.character(tags$code("unsupported variable type", class = "text-blue"))
|
| 1256 |
} |
|
| 1257 | ||
| 1258 |
custom_sparkline_formatter <- function(labels, counts) {
|
|
| 1259 | ! |
htmlwidgets::JS( |
| 1260 | ! |
sprintf( |
| 1261 | ! |
"function(sparkline, options, field) {
|
| 1262 | ! |
return 'ID: ' + %s[field[0].offset] + '<br>' + 'Count: ' + %s[field[0].offset]; |
| 1263 |
}", |
|
| 1264 | ! |
jsonlite::toJSON(labels), |
| 1265 | ! |
jsonlite::toJSON(counts) |
| 1266 |
) |
|
| 1267 |
) |
|
| 1268 |
} |
| 1 |
#' `teal` module: Outliers analysis |
|
| 2 |
#' |
|
| 3 |
#' Module to analyze and identify outliers using different methods |
|
| 4 |
#' such as IQR, Z-score, and Percentiles, and offers visualizations including |
|
| 5 |
#' box plots, density plots, and cumulative distribution plots to help interpret the outliers. |
|
| 6 |
#' |
|
| 7 |
#' @inheritParams teal::module |
|
| 8 |
#' @inheritParams shared_params |
|
| 9 |
#' |
|
| 10 |
#' @param outlier_var (`data_extract_spec` or `list` of multiple `data_extract_spec`) |
|
| 11 |
#' Specifies variable(s) to be analyzed for outliers. |
|
| 12 |
#' @param categorical_var (`data_extract_spec` or `list` of multiple `data_extract_spec`) optional, |
|
| 13 |
#' specifies the categorical variable(s) to split the selected outlier variables on. |
|
| 14 |
#' @param ggplot2_args `r roxygen_ggplot2_args_param("Boxplot", "Density Plot", "Cumulative Distribution Plot")`
|
|
| 15 |
#' |
|
| 16 |
#' @inherit shared_params return |
|
| 17 |
#' |
|
| 18 |
#' @section Decorating Module: |
|
| 19 |
#' |
|
| 20 |
#' This module generates the following objects, which can be modified in place using decorators: |
|
| 21 |
#' - `box_plot` (`ggplot`) |
|
| 22 |
#' - `density_plot` (`ggplot`) |
|
| 23 |
#' - `cumulative_plot` (`ggplot`) |
|
| 24 |
#' |
|
| 25 |
#' A Decorator is applied to the specific output using a named list of `teal_transform_module` objects. |
|
| 26 |
#' The name of this list corresponds to the name of the output to which the decorator is applied. |
|
| 27 |
#' See code snippet below: |
|
| 28 |
#' |
|
| 29 |
#' ``` |
|
| 30 |
#' tm_outliers( |
|
| 31 |
#' ..., # arguments for module |
|
| 32 |
#' decorators = list( |
|
| 33 |
#' box_plot = teal_transform_module(...), # applied only to `box_plot` output |
|
| 34 |
#' density_plot = teal_transform_module(...), # applied only to `density_plot` output |
|
| 35 |
#' cumulative_plot = teal_transform_module(...) # applied only to `cumulative_plot` output |
|
| 36 |
#' ) |
|
| 37 |
#' ) |
|
| 38 |
#' ``` |
|
| 39 |
#' |
|
| 40 |
#' For additional details and examples of decorators, refer to the vignette |
|
| 41 |
#' `vignette("decorate-module-output", package = "teal.modules.general")`.
|
|
| 42 |
#' |
|
| 43 |
#' To learn more please refer to the vignette |
|
| 44 |
#' `vignette("transform-module-output", package = "teal")` or the [`teal::teal_transform_module()`] documentation.
|
|
| 45 |
#' |
|
| 46 |
#' @examplesShinylive |
|
| 47 |
#' library(teal.modules.general) |
|
| 48 |
#' interactive <- function() TRUE |
|
| 49 |
#' {{ next_example }}
|
|
| 50 |
#' @examples |
|
| 51 |
#' |
|
| 52 |
#' # general data example |
|
| 53 |
#' data <- teal_data() |
|
| 54 |
#' data <- within(data, {
|
|
| 55 |
#' CO2 <- CO2 |
|
| 56 |
#' CO2[["primary_key"]] <- seq_len(nrow(CO2)) |
|
| 57 |
#' }) |
|
| 58 |
#' join_keys(data) <- join_keys(join_key("CO2", "CO2", "primary_key"))
|
|
| 59 |
#' |
|
| 60 |
#' vars <- choices_selected(variable_choices(data[["CO2"]], c("Plant", "Type", "Treatment")))
|
|
| 61 |
#' |
|
| 62 |
#' app <- init( |
|
| 63 |
#' data = data, |
|
| 64 |
#' modules = modules( |
|
| 65 |
#' tm_outliers( |
|
| 66 |
#' outlier_var = list( |
|
| 67 |
#' data_extract_spec( |
|
| 68 |
#' dataname = "CO2", |
|
| 69 |
#' select = select_spec( |
|
| 70 |
#' label = "Select variable:", |
|
| 71 |
#' choices = variable_choices(data[["CO2"]], c("conc", "uptake")),
|
|
| 72 |
#' selected = "uptake", |
|
| 73 |
#' multiple = FALSE, |
|
| 74 |
#' fixed = FALSE |
|
| 75 |
#' ) |
|
| 76 |
#' ) |
|
| 77 |
#' ), |
|
| 78 |
#' categorical_var = list( |
|
| 79 |
#' data_extract_spec( |
|
| 80 |
#' dataname = "CO2", |
|
| 81 |
#' filter = filter_spec( |
|
| 82 |
#' vars = vars, |
|
| 83 |
#' choices = value_choices(data[["CO2"]], vars$selected), |
|
| 84 |
#' selected = value_choices(data[["CO2"]], vars$selected), |
|
| 85 |
#' multiple = TRUE |
|
| 86 |
#' ) |
|
| 87 |
#' ) |
|
| 88 |
#' ) |
|
| 89 |
#' ) |
|
| 90 |
#' ) |
|
| 91 |
#' ) |
|
| 92 |
#' if (interactive()) {
|
|
| 93 |
#' shinyApp(app$ui, app$server) |
|
| 94 |
#' } |
|
| 95 |
#' |
|
| 96 |
#' @examplesShinylive |
|
| 97 |
#' library(teal.modules.general) |
|
| 98 |
#' interactive <- function() TRUE |
|
| 99 |
#' {{ next_example }}
|
|
| 100 |
#' @examples |
|
| 101 |
#' |
|
| 102 |
#' # CDISC data example |
|
| 103 |
#' data <- teal_data() |
|
| 104 |
#' data <- within(data, {
|
|
| 105 |
#' ADSL <- teal.data::rADSL |
|
| 106 |
#' }) |
|
| 107 |
#' join_keys(data) <- default_cdisc_join_keys[names(data)] |
|
| 108 |
#' |
|
| 109 |
#' fact_vars_adsl <- names(Filter(isTRUE, sapply(data[["ADSL"]], is.factor))) |
|
| 110 |
#' vars <- choices_selected(variable_choices(data[["ADSL"]], fact_vars_adsl)) |
|
| 111 |
#' |
|
| 112 |
#' |
|
| 113 |
#' |
|
| 114 |
#' app <- init( |
|
| 115 |
#' data = data, |
|
| 116 |
#' modules = modules( |
|
| 117 |
#' tm_outliers( |
|
| 118 |
#' outlier_var = list( |
|
| 119 |
#' data_extract_spec( |
|
| 120 |
#' dataname = "ADSL", |
|
| 121 |
#' select = select_spec( |
|
| 122 |
#' label = "Select variable:", |
|
| 123 |
#' choices = variable_choices(data[["ADSL"]], c("AGE", "BMRKR1")),
|
|
| 124 |
#' selected = "AGE", |
|
| 125 |
#' multiple = FALSE, |
|
| 126 |
#' fixed = FALSE |
|
| 127 |
#' ) |
|
| 128 |
#' ) |
|
| 129 |
#' ), |
|
| 130 |
#' categorical_var = list( |
|
| 131 |
#' data_extract_spec( |
|
| 132 |
#' dataname = "ADSL", |
|
| 133 |
#' filter = filter_spec( |
|
| 134 |
#' vars = vars, |
|
| 135 |
#' choices = value_choices(data[["ADSL"]], vars$selected), |
|
| 136 |
#' selected = value_choices(data[["ADSL"]], vars$selected), |
|
| 137 |
#' multiple = TRUE |
|
| 138 |
#' ) |
|
| 139 |
#' ) |
|
| 140 |
#' ) |
|
| 141 |
#' ) |
|
| 142 |
#' ) |
|
| 143 |
#' ) |
|
| 144 |
#' if (interactive()) {
|
|
| 145 |
#' shinyApp(app$ui, app$server) |
|
| 146 |
#' } |
|
| 147 |
#' |
|
| 148 |
#' @export |
|
| 149 |
#' |
|
| 150 |
tm_outliers <- function(label = "Outliers Module", |
|
| 151 |
outlier_var, |
|
| 152 |
categorical_var = NULL, |
|
| 153 |
ggtheme = c("gray", "bw", "linedraw", "light", "dark", "minimal", "classic", "void"),
|
|
| 154 |
ggplot2_args = teal.widgets::ggplot2_args(), |
|
| 155 |
plot_height = c(600, 200, 2000), |
|
| 156 |
plot_width = NULL, |
|
| 157 |
pre_output = NULL, |
|
| 158 |
post_output = NULL, |
|
| 159 |
transformators = list(), |
|
| 160 |
decorators = list()) {
|
|
| 161 | ! |
message("Initializing tm_outliers")
|
| 162 | ||
| 163 |
# Normalize the parameters |
|
| 164 | ! |
if (inherits(outlier_var, "data_extract_spec")) outlier_var <- list(outlier_var) |
| 165 | ! |
if (inherits(categorical_var, "data_extract_spec")) categorical_var <- list(categorical_var) |
| 166 | ! |
if (inherits(ggplot2_args, "ggplot2_args")) ggplot2_args <- list(default = ggplot2_args) |
| 167 | ||
| 168 |
# Start of assertions |
|
| 169 | ! |
checkmate::assert_string(label) |
| 170 | ! |
checkmate::assert_list(outlier_var, types = "data_extract_spec") |
| 171 | ||
| 172 | ! |
checkmate::assert_list(categorical_var, types = "data_extract_spec", null.ok = TRUE) |
| 173 | ! |
if (is.list(categorical_var)) {
|
| 174 | ! |
lapply(categorical_var, function(x) {
|
| 175 | ! |
if (length(x$filter) > 1L) {
|
| 176 | ! |
stop("tm_outliers: categorical_var data_extract_specs may only specify one filter_spec", call. = FALSE)
|
| 177 |
} |
|
| 178 |
}) |
|
| 179 |
} |
|
| 180 | ||
| 181 | ! |
ggtheme <- match.arg(ggtheme) |
| 182 | ||
| 183 | ! |
plot_choices <- c("Boxplot", "Density Plot", "Cumulative Distribution Plot")
|
| 184 | ! |
checkmate::assert_list(ggplot2_args, types = "ggplot2_args") |
| 185 | ! |
checkmate::assert_subset(names(ggplot2_args), c("default", plot_choices))
|
| 186 | ||
| 187 | ! |
checkmate::assert_numeric(plot_height, len = 3, any.missing = FALSE, finite = TRUE) |
| 188 | ! |
checkmate::assert_numeric(plot_height[1], lower = plot_height[2], upper = plot_height[3], .var.name = "plot_height") |
| 189 | ! |
checkmate::assert_numeric(plot_width, len = 3, any.missing = FALSE, null.ok = TRUE, finite = TRUE) |
| 190 | ! |
checkmate::assert_numeric( |
| 191 | ! |
plot_width[1], |
| 192 | ! |
lower = plot_width[2], upper = plot_width[3], null.ok = TRUE, .var.name = "plot_width" |
| 193 |
) |
|
| 194 | ||
| 195 | ! |
checkmate::assert_multi_class(pre_output, c("shiny.tag", "shiny.tag.list", "html"), null.ok = TRUE)
|
| 196 | ! |
checkmate::assert_multi_class(post_output, c("shiny.tag", "shiny.tag.list", "html"), null.ok = TRUE)
|
| 197 | ||
| 198 | ! |
assert_decorators(decorators, names = c("box_plot", "density_plot", "cumulative_plot"))
|
| 199 |
# End of assertions |
|
| 200 | ||
| 201 |
# Make UI args |
|
| 202 | ! |
args <- as.list(environment()) |
| 203 | ||
| 204 | ! |
data_extract_list <- list( |
| 205 | ! |
outlier_var = outlier_var, |
| 206 | ! |
categorical_var = categorical_var |
| 207 |
) |
|
| 208 | ||
| 209 | ||
| 210 | ! |
ans <- module( |
| 211 | ! |
label = label, |
| 212 | ! |
server = srv_outliers, |
| 213 | ! |
server_args = c( |
| 214 | ! |
data_extract_list, |
| 215 | ! |
list( |
| 216 | ! |
plot_height = plot_height, plot_width = plot_width, ggplot2_args = ggplot2_args, |
| 217 | ! |
decorators = decorators |
| 218 |
) |
|
| 219 |
), |
|
| 220 | ! |
ui = ui_outliers, |
| 221 | ! |
ui_args = args, |
| 222 | ! |
transformators = transformators, |
| 223 | ! |
datanames = teal.transform::get_extract_datanames(data_extract_list) |
| 224 |
) |
|
| 225 | ! |
attr(ans, "teal_bookmarkable") <- TRUE |
| 226 | ! |
ans |
| 227 |
} |
|
| 228 | ||
| 229 |
# UI function for the outliers module |
|
| 230 |
ui_outliers <- function(id, ...) {
|
|
| 231 | ! |
args <- list(...) |
| 232 | ! |
ns <- NS(id) |
| 233 | ! |
is_single_dataset_value <- teal.transform::is_single_dataset(args$outlier_var, args$categorical_var) |
| 234 | ||
| 235 | ! |
teal.widgets::standard_layout( |
| 236 | ! |
output = teal.widgets::white_small_well( |
| 237 | ! |
uiOutput(ns("total_outliers")),
|
| 238 | ! |
tags$div( |
| 239 | ! |
style = "overflow: auto;", |
| 240 | ! |
DT::dataTableOutput(ns("summary_table"))
|
| 241 |
), |
|
| 242 | ! |
uiOutput(ns("total_missing")),
|
| 243 | ! |
tags$br(), tags$hr(), |
| 244 | ! |
tabsetPanel( |
| 245 | ! |
id = ns("tabs"),
|
| 246 | ! |
tabPanel( |
| 247 | ! |
"Boxplot", |
| 248 | ! |
teal.widgets::plot_with_settings_ui(id = ns("box_plot"))
|
| 249 |
), |
|
| 250 | ! |
tabPanel( |
| 251 | ! |
"Density Plot", |
| 252 | ! |
teal.widgets::plot_with_settings_ui(id = ns("density_plot"))
|
| 253 |
), |
|
| 254 | ! |
tabPanel( |
| 255 | ! |
"Cumulative Distribution Plot", |
| 256 | ! |
teal.widgets::plot_with_settings_ui(id = ns("cum_density_plot"))
|
| 257 |
) |
|
| 258 |
), |
|
| 259 | ! |
tags$br(), tags$hr(), |
| 260 | ! |
uiOutput(ns("table_ui_wrap")),
|
| 261 | ! |
DT::dataTableOutput(ns("table_ui"))
|
| 262 |
), |
|
| 263 | ! |
encoding = tags$div( |
| 264 |
### Reporter |
|
| 265 | ! |
teal.reporter::add_card_button_ui(ns("add_reporter"), label = "Add Report Card"),
|
| 266 | ! |
tags$br(), tags$br(), |
| 267 |
### |
|
| 268 | ! |
tags$label("Encodings", class = "text-primary"),
|
| 269 | ! |
teal.transform::datanames_input(args[c("outlier_var", "categorical_var")]),
|
| 270 | ! |
teal.transform::data_extract_ui( |
| 271 | ! |
id = ns("outlier_var"),
|
| 272 | ! |
label = "Variable", |
| 273 | ! |
data_extract_spec = args$outlier_var, |
| 274 | ! |
is_single_dataset = is_single_dataset_value |
| 275 |
), |
|
| 276 | ! |
if (!is.null(args$categorical_var)) {
|
| 277 | ! |
teal.transform::data_extract_ui( |
| 278 | ! |
id = ns("categorical_var"),
|
| 279 | ! |
label = "Categorical factor", |
| 280 | ! |
data_extract_spec = args$categorical_var, |
| 281 | ! |
is_single_dataset = is_single_dataset_value |
| 282 |
) |
|
| 283 |
}, |
|
| 284 | ! |
conditionalPanel( |
| 285 | ! |
condition = paste0("input['", ns("tabs"), "'] == 'Boxplot'"),
|
| 286 | ! |
teal.widgets::optionalSelectInput( |
| 287 | ! |
inputId = ns("boxplot_alts"),
|
| 288 | ! |
label = "Plot type", |
| 289 | ! |
choices = c("Box plot", "Violin plot"),
|
| 290 | ! |
selected = "Box plot", |
| 291 | ! |
multiple = FALSE |
| 292 |
) |
|
| 293 |
), |
|
| 294 | ! |
shinyjs::hidden(checkboxInput(ns("split_outliers"), "Define outliers based on group splitting", value = FALSE)),
|
| 295 | ! |
shinyjs::hidden(checkboxInput(ns("order_by_outlier"), "Re-order categories by outliers [by %]", value = FALSE)),
|
| 296 | ! |
bslib::accordion( |
| 297 | ! |
open = TRUE, |
| 298 | ! |
bslib::accordion_panel( |
| 299 | ! |
title = "Method parameters", |
| 300 | ! |
collapsed = FALSE, |
| 301 | ! |
teal.widgets::optionalSelectInput( |
| 302 | ! |
inputId = ns("method"),
|
| 303 | ! |
label = "Method", |
| 304 | ! |
choices = c("IQR", "Z-score", "Percentile"),
|
| 305 | ! |
selected = "IQR", |
| 306 | ! |
multiple = FALSE |
| 307 |
), |
|
| 308 | ! |
conditionalPanel( |
| 309 | ! |
condition = |
| 310 | ! |
paste0("input['", ns("method"), "'] == 'IQR'"),
|
| 311 | ! |
sliderInput( |
| 312 | ! |
ns("iqr_slider"),
|
| 313 | ! |
"Outlier range:", |
| 314 | ! |
min = 1, |
| 315 | ! |
max = 5, |
| 316 | ! |
value = 3, |
| 317 | ! |
step = 0.5 |
| 318 |
) |
|
| 319 |
), |
|
| 320 | ! |
conditionalPanel( |
| 321 | ! |
condition = |
| 322 | ! |
paste0("input['", ns("method"), "'] == 'Z-score'"),
|
| 323 | ! |
sliderInput( |
| 324 | ! |
ns("zscore_slider"),
|
| 325 | ! |
"Outlier range:", |
| 326 | ! |
min = 1, |
| 327 | ! |
max = 5, |
| 328 | ! |
value = 3, |
| 329 | ! |
step = 0.5 |
| 330 |
) |
|
| 331 |
), |
|
| 332 | ! |
conditionalPanel( |
| 333 | ! |
condition = |
| 334 | ! |
paste0("input['", ns("method"), "'] == 'Percentile'"),
|
| 335 | ! |
sliderInput( |
| 336 | ! |
ns("percentile_slider"),
|
| 337 | ! |
"Outlier range:", |
| 338 | ! |
min = 0.001, |
| 339 | ! |
max = 0.5, |
| 340 | ! |
value = 0.01, |
| 341 | ! |
step = 0.001 |
| 342 |
) |
|
| 343 |
), |
|
| 344 | ! |
uiOutput(ns("ui_outlier_help"))
|
| 345 |
) |
|
| 346 |
), |
|
| 347 | ! |
conditionalPanel( |
| 348 | ! |
condition = paste0("input['", ns("tabs"), "'] == 'Boxplot'"),
|
| 349 | ! |
ui_decorate_teal_data( |
| 350 | ! |
ns("d_box_plot"),
|
| 351 | ! |
decorators = select_decorators(args$decorators, "box_plot") |
| 352 |
) |
|
| 353 |
), |
|
| 354 | ! |
conditionalPanel( |
| 355 | ! |
condition = paste0("input['", ns("tabs"), "'] == 'Density Plot'"),
|
| 356 | ! |
ui_decorate_teal_data( |
| 357 | ! |
ns("d_density_plot"),
|
| 358 | ! |
decorators = select_decorators(args$decorators, "density_plot") |
| 359 |
) |
|
| 360 |
), |
|
| 361 | ! |
conditionalPanel( |
| 362 | ! |
condition = paste0("input['", ns("tabs"), "'] == 'Cumulative Distribution Plot'"),
|
| 363 | ! |
ui_decorate_teal_data( |
| 364 | ! |
ns("d_cumulative_plot"),
|
| 365 | ! |
decorators = select_decorators(args$decorators, "cumulative_plot") |
| 366 |
) |
|
| 367 |
), |
|
| 368 | ! |
bslib::accordion_panel( |
| 369 | ! |
title = "Plot settings", |
| 370 | ! |
selectInput( |
| 371 | ! |
inputId = ns("ggtheme"),
|
| 372 | ! |
label = "Theme (by ggplot):", |
| 373 | ! |
choices = ggplot_themes, |
| 374 | ! |
selected = args$ggtheme, |
| 375 | ! |
multiple = FALSE |
| 376 |
) |
|
| 377 |
) |
|
| 378 |
), |
|
| 379 | ! |
forms = tagList( |
| 380 | ! |
teal.widgets::verbatim_popup_ui(ns("rcode"), "Show R code")
|
| 381 |
), |
|
| 382 | ! |
pre_output = args$pre_output, |
| 383 | ! |
post_output = args$post_output |
| 384 |
) |
|
| 385 |
} |
|
| 386 | ||
| 387 |
# Server function for the outliers module |
|
| 388 |
# Server function for the outliers module |
|
| 389 |
srv_outliers <- function(id, data, reporter, filter_panel_api, outlier_var, |
|
| 390 |
categorical_var, plot_height, plot_width, ggplot2_args, decorators) {
|
|
| 391 | ! |
with_reporter <- !missing(reporter) && inherits(reporter, "Reporter") |
| 392 | ! |
with_filter <- !missing(filter_panel_api) && inherits(filter_panel_api, "FilterPanelAPI") |
| 393 | ! |
checkmate::assert_class(data, "reactive") |
| 394 | ! |
checkmate::assert_class(isolate(data()), "teal_data") |
| 395 | ! |
moduleServer(id, function(input, output, session) {
|
| 396 | ! |
teal.logger::log_shiny_input_changes(input, namespace = "teal.modules.general") |
| 397 | ||
| 398 | ! |
ns <- session$ns |
| 399 | ||
| 400 | ! |
vars <- list(outlier_var = outlier_var, categorical_var = categorical_var) |
| 401 | ||
| 402 | ! |
rule_diff <- function(other) {
|
| 403 | ! |
function(value) {
|
| 404 | ! |
othervalue <- tryCatch(selector_list()[[other]]()[["select"]], error = function(e) NULL) |
| 405 | ! |
if (!is.null(othervalue) && identical(othervalue, value)) {
|
| 406 | ! |
"`Variable` and `Categorical factor` cannot be the same" |
| 407 |
} |
|
| 408 |
} |
|
| 409 |
} |
|
| 410 | ||
| 411 | ! |
selector_list <- teal.transform::data_extract_multiple_srv( |
| 412 | ! |
data_extract = vars, |
| 413 | ! |
datasets = data, |
| 414 | ! |
select_validation_rule = list( |
| 415 | ! |
outlier_var = shinyvalidate::compose_rules( |
| 416 | ! |
shinyvalidate::sv_required("Please select a variable"),
|
| 417 | ! |
rule_diff("categorical_var")
|
| 418 |
), |
|
| 419 | ! |
categorical_var = rule_diff("outlier_var")
|
| 420 |
) |
|
| 421 |
) |
|
| 422 | ||
| 423 | ! |
iv_r <- reactive({
|
| 424 | ! |
iv <- shinyvalidate::InputValidator$new() |
| 425 | ! |
iv$add_rule("method", shinyvalidate::sv_required("Please select a method"))
|
| 426 | ! |
iv$add_rule("boxplot_alts", shinyvalidate::sv_required("Please select Plot Type"))
|
| 427 | ! |
teal.transform::compose_and_enable_validators(iv, selector_list) |
| 428 |
}) |
|
| 429 | ||
| 430 | ! |
reactive_select_input <- reactive({
|
| 431 | ! |
if (is.null(selector_list()$categorical_var) || length(selector_list()$categorical_var()$select) == 0) {
|
| 432 | ! |
selector_list()[names(selector_list()) != "categorical_var"] |
| 433 |
} else {
|
|
| 434 | ! |
selector_list() |
| 435 |
} |
|
| 436 |
}) |
|
| 437 | ||
| 438 |
# Used to create outlier table and the dropdown with additional columns |
|
| 439 | ! |
dataname_first <- isolate(names(data())[[1]]) |
| 440 | ||
| 441 | ! |
data_obj <- reactive({
|
| 442 | ! |
obj <- data() |
| 443 | ! |
if (length(teal.data::join_keys(obj)) == 0) {
|
| 444 | ! |
if (!".row_id" %in% names(obj[[dataname_first]])) {
|
| 445 | ! |
obj[[dataname_first]]$.row_id <- seq_len(nrow(obj[[dataname_first]])) |
| 446 |
} |
|
| 447 | ! |
teal.data::join_keys(obj) <- |
| 448 | ! |
teal.data::join_keys(teal.data::join_key(dataname_first, dataname_first, ".row_id")) |
| 449 |
} |
|
| 450 | ! |
obj |
| 451 |
}) |
|
| 452 | ||
| 453 | ! |
anl_merged_input <- teal.transform::merge_expression_srv( |
| 454 | ! |
selector_list = reactive_select_input, |
| 455 | ! |
datasets = data_obj, |
| 456 | ! |
merge_function = "dplyr::inner_join" |
| 457 |
) |
|
| 458 | ||
| 459 | ! |
anl_merged_q <- reactive({
|
| 460 | ! |
req(anl_merged_input()) |
| 461 | ! |
teal.code::eval_code( |
| 462 | ! |
data_obj(), |
| 463 | ! |
paste0( |
| 464 | ! |
'library("dplyr");library("tidyr");', # nolint quotes
|
| 465 | ! |
'library("tibble");library("ggplot2");'
|
| 466 |
) |
|
| 467 | ! |
) %>% # nolint quotes |
| 468 | ! |
teal.code::eval_code(as.expression(anl_merged_input()$expr)) |
| 469 |
}) |
|
| 470 | ||
| 471 | ! |
merged <- list( |
| 472 | ! |
anl_input_r = anl_merged_input, |
| 473 | ! |
anl_q_r = anl_merged_q |
| 474 |
) |
|
| 475 | ||
| 476 | ! |
n_outlier_missing <- reactive({
|
| 477 | ! |
req(iv_r()$is_valid()) |
| 478 | ! |
outlier_var <- as.vector(merged$anl_input_r()$columns_source$outlier_var) |
| 479 | ! |
ANL <- merged$anl_q_r()[["ANL"]] |
| 480 | ! |
sum(is.na(ANL[[outlier_var]])) |
| 481 |
}) |
|
| 482 | ||
| 483 | ! |
common_code_q <- reactive({
|
| 484 | ! |
req(iv_r()$is_valid()) |
| 485 | ||
| 486 | ! |
ANL <- merged$anl_q_r()[["ANL"]] |
| 487 | ! |
qenv <- merged$anl_q_r() |
| 488 | ||
| 489 | ! |
outlier_var <- as.vector(merged$anl_input_r()$columns_source$outlier_var) |
| 490 | ! |
categorical_var <- as.vector(merged$anl_input_r()$columns_source$categorical_var) |
| 491 | ! |
order_by_outlier <- input$order_by_outlier |
| 492 | ! |
method <- input$method |
| 493 | ! |
split_outliers <- input$split_outliers |
| 494 | ! |
teal::validate_has_data( |
| 495 |
# missing values in the categorical variable may be used to form a category of its own |
|
| 496 | ! |
`if`( |
| 497 | ! |
length(categorical_var) == 0, |
| 498 | ! |
ANL, |
| 499 | ! |
ANL[, names(ANL) != categorical_var, drop = FALSE] |
| 500 |
), |
|
| 501 | ! |
min_nrow = 10, |
| 502 | ! |
complete = TRUE, |
| 503 | ! |
allow_inf = FALSE |
| 504 |
) |
|
| 505 | ! |
validate(need(is.numeric(ANL[[outlier_var]]), "`Variable` is not numeric")) |
| 506 | ! |
validate(need(length(unique(ANL[[outlier_var]])) > 1, "Variable has no variation, i.e. only one unique value")) |
| 507 | ||
| 508 |
# show/hide split_outliers |
|
| 509 | ! |
if (length(categorical_var) == 0) {
|
| 510 | ! |
shinyjs::hide("split_outliers")
|
| 511 | ! |
if (n_outlier_missing() > 0) {
|
| 512 | ! |
qenv <- teal.code::eval_code( |
| 513 | ! |
qenv, |
| 514 | ! |
substitute( |
| 515 | ! |
expr = ANL <- ANL %>% dplyr::filter(!is.na(outlier_var_name)), |
| 516 | ! |
env = list(outlier_var_name = as.name(outlier_var)) |
| 517 |
) |
|
| 518 |
) |
|
| 519 |
} |
|
| 520 |
} else {
|
|
| 521 | ! |
validate(need( |
| 522 | ! |
is.factor(ANL[[categorical_var]]) || |
| 523 | ! |
is.character(ANL[[categorical_var]]) || |
| 524 | ! |
is.integer(ANL[[categorical_var]]), |
| 525 | ! |
"`Categorical factor` must be `factor`, `character`, or `integer`" |
| 526 |
)) |
|
| 527 | ||
| 528 | ! |
if (n_outlier_missing() > 0) {
|
| 529 | ! |
qenv <- teal.code::eval_code( |
| 530 | ! |
qenv, |
| 531 | ! |
substitute( |
| 532 | ! |
expr = ANL <- ANL %>% dplyr::filter(!is.na(outlier_var_name)), |
| 533 | ! |
env = list(outlier_var_name = as.name(outlier_var)) |
| 534 |
) |
|
| 535 |
) |
|
| 536 |
} |
|
| 537 | ! |
shinyjs::show("split_outliers")
|
| 538 |
} |
|
| 539 | ||
| 540 |
# slider |
|
| 541 | ! |
outlier_definition_param <- if (method == "IQR") {
|
| 542 | ! |
input$iqr_slider |
| 543 | ! |
} else if (method == "Z-score") {
|
| 544 | ! |
input$zscore_slider |
| 545 | ! |
} else if (method == "Percentile") {
|
| 546 | ! |
input$percentile_slider |
| 547 |
} |
|
| 548 | ||
| 549 |
# this is utils function that converts a %>% NULL %>% b into a %>% b |
|
| 550 | ! |
remove_pipe_null <- function(x) {
|
| 551 | ! |
if (length(x) == 1) {
|
| 552 | ! |
x |
| 553 | ! |
} else if (identical(x[[1]], as.name("%>%")) && is.null(x[[3]])) {
|
| 554 | ! |
remove_pipe_null(x[[2]]) |
| 555 |
} else {
|
|
| 556 | ! |
as.call(c(x[[1]], lapply(x[-1], remove_pipe_null))) |
| 557 |
} |
|
| 558 |
} |
|
| 559 | ||
| 560 | ! |
qenv <- teal.code::eval_code( |
| 561 | ! |
qenv, |
| 562 | ! |
substitute( |
| 563 | ! |
expr = {
|
| 564 | ! |
ANL_OUTLIER <- ANL %>% |
| 565 | ! |
group_expr %>% # styler: off |
| 566 | ! |
dplyr::mutate(is_outlier = {
|
| 567 | ! |
q1_q3 <- stats::quantile(outlier_var_name, probs = c(0.25, 0.75)) |
| 568 | ! |
iqr <- q1_q3[2] - q1_q3[1] |
| 569 | ! |
!(outlier_var_name >= q1_q3[1] - 1.5 * iqr & outlier_var_name <= q1_q3[2] + 1.5 * iqr) |
| 570 |
}) %>% |
|
| 571 | ! |
calculate_outliers %>% # styler: off |
| 572 | ! |
ungroup_expr %>% # styler: off |
| 573 | ! |
dplyr::filter(is_outlier | is_outlier_selected) %>% |
| 574 | ! |
dplyr::select(-is_outlier) |
| 575 |
}, |
|
| 576 | ! |
env = list( |
| 577 | ! |
calculate_outliers = if (method == "IQR") {
|
| 578 | ! |
substitute( |
| 579 | ! |
expr = dplyr::mutate(is_outlier_selected = {
|
| 580 | ! |
q1_q3 <- stats::quantile(outlier_var_name, probs = c(0.25, 0.75)) |
| 581 | ! |
iqr <- q1_q3[2] - q1_q3[1] |
| 582 |
!( |
|
| 583 | ! |
outlier_var_name >= q1_q3[1] - outlier_definition_param * iqr & |
| 584 | ! |
outlier_var_name <= q1_q3[2] + outlier_definition_param * iqr |
| 585 |
) |
|
| 586 |
}), |
|
| 587 | ! |
env = list( |
| 588 | ! |
outlier_var_name = as.name(outlier_var), |
| 589 | ! |
outlier_definition_param = outlier_definition_param |
| 590 |
) |
|
| 591 |
) |
|
| 592 | ! |
} else if (method == "Z-score") {
|
| 593 | ! |
substitute( |
| 594 | ! |
expr = dplyr::mutate( |
| 595 | ! |
is_outlier_selected = abs(outlier_var_name - mean(outlier_var_name)) / |
| 596 | ! |
stats::sd(outlier_var_name) > outlier_definition_param |
| 597 |
), |
|
| 598 | ! |
env = list( |
| 599 | ! |
outlier_var_name = as.name(outlier_var), |
| 600 | ! |
outlier_definition_param = outlier_definition_param |
| 601 |
) |
|
| 602 |
) |
|
| 603 | ! |
} else if (method == "Percentile") {
|
| 604 | ! |
substitute( |
| 605 | ! |
expr = dplyr::mutate( |
| 606 | ! |
is_outlier_selected = outlier_var_name < stats::quantile(outlier_var_name, outlier_definition_param) | |
| 607 | ! |
outlier_var_name > stats::quantile(outlier_var_name, 1 - outlier_definition_param) |
| 608 |
), |
|
| 609 | ! |
env = list( |
| 610 | ! |
outlier_var_name = as.name(outlier_var), |
| 611 | ! |
outlier_definition_param = outlier_definition_param |
| 612 |
) |
|
| 613 |
) |
|
| 614 |
}, |
|
| 615 | ! |
outlier_var_name = as.name(outlier_var), |
| 616 | ! |
group_expr = if (isTRUE(split_outliers) && length(categorical_var) != 0) {
|
| 617 | ! |
substitute(dplyr::group_by(x), list(x = as.name(categorical_var))) |
| 618 |
}, |
|
| 619 | ! |
ungroup_expr = if (isTRUE(split_outliers) && length(categorical_var) != 0) {
|
| 620 | ! |
substitute(dplyr::ungroup()) |
| 621 |
} |
|
| 622 |
) |
|
| 623 |
) %>% |
|
| 624 | ! |
remove_pipe_null() |
| 625 |
) |
|
| 626 | ||
| 627 |
# ANL_OUTLIER_EXTENDED is the base table |
|
| 628 | ! |
join_keys <- as.character(teal.data::join_keys(data_obj())[dataname_first, dataname_first]) |
| 629 | ||
| 630 | ! |
if (length(join_keys) == 1 && join_keys == ".row_id") {
|
| 631 |
# Dummy join key - single dataset, no join needed |
|
| 632 | ! |
qenv <- teal.code::eval_code(qenv, quote(ANL_OUTLIER_EXTENDED <- ANL_OUTLIER)) |
| 633 |
} else {
|
|
| 634 |
# Join keys exist - perform left join |
|
| 635 | ! |
qenv <- teal.code::eval_code( |
| 636 | ! |
qenv, |
| 637 | ! |
substitute( |
| 638 | ! |
expr = {
|
| 639 | ! |
ANL_OUTLIER_EXTENDED <- dplyr::left_join( |
| 640 | ! |
ANL_OUTLIER, |
| 641 | ! |
dplyr::select( |
| 642 | ! |
dataname, |
| 643 | ! |
dplyr::setdiff(names(dataname), dplyr::setdiff(names(ANL_OUTLIER), join_keys)) |
| 644 |
), |
|
| 645 | ! |
by = join_keys |
| 646 |
) |
|
| 647 |
}, |
|
| 648 | ! |
env = list( |
| 649 | ! |
dataname = as.name(dataname_first), |
| 650 | ! |
join_keys = join_keys |
| 651 |
) |
|
| 652 |
) |
|
| 653 |
) |
|
| 654 |
} |
|
| 655 | ||
| 656 | ! |
qenv <- if (length(categorical_var) > 0) {
|
| 657 | ! |
qenv <- teal.code::eval_code( |
| 658 | ! |
qenv, |
| 659 | ! |
substitute( |
| 660 | ! |
expr = summary_data_pre <- ANL_OUTLIER %>% |
| 661 | ! |
dplyr::filter(is_outlier_selected) %>% |
| 662 | ! |
dplyr::select(outlier_var_name, categorical_var_name) %>% |
| 663 | ! |
dplyr::group_by(categorical_var_name) %>% |
| 664 | ! |
dplyr::summarise(n_outliers = dplyr::n()) %>% |
| 665 | ! |
dplyr::right_join( |
| 666 | ! |
ANL %>% |
| 667 | ! |
dplyr::select(outlier_var_name, categorical_var_name) %>% |
| 668 | ! |
dplyr::group_by(categorical_var_name) %>% |
| 669 | ! |
dplyr::summarise( |
| 670 | ! |
total_in_cat = dplyr::n(), |
| 671 | ! |
n_na = sum(is.na(outlier_var_name) | is.na(categorical_var_name)) |
| 672 |
), |
|
| 673 | ! |
by = categorical_var |
| 674 |
) %>% |
|
| 675 |
# This is important as there may be categorical variables with natural orderings, e.g. AGE. |
|
| 676 |
# The plots should be displayed by default in increasing order in these situations. |
|
| 677 |
# dplyr::arrange will sort integer, factor, and character data types in the expected way. |
|
| 678 | ! |
dplyr::arrange(categorical_var_name) %>% |
| 679 | ! |
dplyr::mutate( |
| 680 | ! |
n_outliers = dplyr::if_else(is.na(n_outliers), 0, as.numeric(n_outliers)), |
| 681 | ! |
display_str = dplyr::if_else( |
| 682 | ! |
n_outliers > 0, |
| 683 | ! |
sprintf("%d [%.02f%%]", n_outliers, 100 * n_outliers / total_in_cat),
|
| 684 | ! |
"0" |
| 685 |
), |
|
| 686 | ! |
display_str_na = dplyr::if_else( |
| 687 | ! |
n_na > 0, |
| 688 | ! |
sprintf("%d [%.02f%%]", n_na, 100 * n_na / total_in_cat),
|
| 689 | ! |
"0" |
| 690 |
), |
|
| 691 | ! |
order = seq_along(n_outliers) |
| 692 |
), |
|
| 693 | ! |
env = list( |
| 694 | ! |
categorical_var = categorical_var, |
| 695 | ! |
categorical_var_name = as.name(categorical_var), |
| 696 | ! |
outlier_var_name = as.name(outlier_var) |
| 697 |
) |
|
| 698 |
) |
|
| 699 |
) |
|
| 700 |
# now to handle when user chooses to order based on amount of outliers |
|
| 701 | ! |
if (order_by_outlier) {
|
| 702 | ! |
qenv <- teal.code::eval_code( |
| 703 | ! |
qenv, |
| 704 | ! |
quote( |
| 705 | ! |
summary_data_pre <- summary_data_pre %>% |
| 706 | ! |
dplyr::arrange(desc(n_outliers / total_in_cat)) %>% |
| 707 | ! |
dplyr::mutate(order = seq_len(nrow(summary_data_pre))) |
| 708 |
) |
|
| 709 |
) |
|
| 710 |
} |
|
| 711 | ||
| 712 | ! |
teal.code::eval_code( |
| 713 | ! |
qenv, |
| 714 | ! |
substitute( |
| 715 | ! |
expr = {
|
| 716 |
# In order for geom_rug to work properly when reordering takes place inside facet_grid, |
|
| 717 |
# all tables must have the column used for reording. |
|
| 718 |
# In this case, the column used for reordering is `order`. |
|
| 719 | ! |
ANL_OUTLIER <- dplyr::left_join( |
| 720 | ! |
ANL_OUTLIER, |
| 721 | ! |
summary_data_pre[, c("order", categorical_var)],
|
| 722 | ! |
by = categorical_var |
| 723 |
) |
|
| 724 |
# so that x axis of plot aligns with columns of summary table, from most outliers to least by percentage |
|
| 725 | ! |
ANL <- ANL %>% |
| 726 | ! |
dplyr::left_join( |
| 727 | ! |
dplyr::select(summary_data_pre, categorical_var_name, order), |
| 728 | ! |
by = categorical_var |
| 729 |
) %>% |
|
| 730 | ! |
dplyr::arrange(order) |
| 731 | ! |
summary_data <- summary_data_pre %>% |
| 732 | ! |
dplyr::select( |
| 733 | ! |
categorical_var_name, |
| 734 | ! |
Outliers = display_str, Missings = display_str_na, Total = total_in_cat |
| 735 |
) %>% |
|
| 736 | ! |
dplyr::mutate_all(as.character) %>% |
| 737 | ! |
tidyr::pivot_longer(-categorical_var_name) %>% |
| 738 | ! |
tidyr::pivot_wider(names_from = categorical_var, values_from = value) %>% |
| 739 | ! |
tibble::column_to_rownames("name")
|
| 740 |
}, |
|
| 741 | ! |
env = list( |
| 742 | ! |
categorical_var = categorical_var, |
| 743 | ! |
categorical_var_name = as.name(categorical_var) |
| 744 |
) |
|
| 745 |
) |
|
| 746 |
) |
|
| 747 |
} else {
|
|
| 748 | ! |
within(qenv, summary_data <- data.frame()) |
| 749 |
} |
|
| 750 | ||
| 751 |
# Generate decoratable object from data |
|
| 752 | ! |
qenv <- within(qenv, {
|
| 753 | ! |
table <- rtables::df_to_tt(summary_data) |
| 754 | ! |
table |
| 755 |
}) |
|
| 756 | ||
| 757 | ! |
if (length(categorical_var) > 0 && nrow(qenv[["ANL_OUTLIER"]]) > 0) {
|
| 758 | ! |
shinyjs::show("order_by_outlier")
|
| 759 |
} else {
|
|
| 760 | ! |
shinyjs::hide("order_by_outlier")
|
| 761 |
} |
|
| 762 | ||
| 763 | ! |
qenv |
| 764 |
}) |
|
| 765 | ||
| 766 |
# boxplot/violinplot # nolint commented_code |
|
| 767 | ! |
box_plot_q <- reactive({
|
| 768 | ! |
req(common_code_q()) |
| 769 | ! |
ANL <- common_code_q()[["ANL"]] |
| 770 | ! |
ANL_OUTLIER <- common_code_q()[["ANL_OUTLIER"]] |
| 771 | ||
| 772 | ! |
outlier_var <- as.vector(merged$anl_input_r()$columns_source$outlier_var) |
| 773 | ! |
categorical_var <- as.vector(merged$anl_input_r()$columns_source$categorical_var) |
| 774 | ||
| 775 |
# validation |
|
| 776 | ! |
teal::validate_has_data(ANL, 1) |
| 777 | ||
| 778 |
# boxplot |
|
| 779 | ! |
plot_call <- quote(ANL %>% ggplot()) |
| 780 | ||
| 781 | ! |
plot_call <- if (input$boxplot_alts == "Box plot") {
|
| 782 | ! |
substitute(expr = plot_call + ggplot2::geom_boxplot(outlier.shape = NA), env = list(plot_call = plot_call)) |
| 783 | ! |
} else if (input$boxplot_alts == "Violin plot") {
|
| 784 | ! |
substitute(expr = plot_call + ggplot2::geom_violin(), env = list(plot_call = plot_call)) |
| 785 |
} else {
|
|
| 786 | ! |
NULL |
| 787 |
} |
|
| 788 | ||
| 789 | ! |
plot_call <- if (identical(categorical_var, character(0)) || is.null(categorical_var)) {
|
| 790 | ! |
inner_call <- substitute( |
| 791 | ! |
expr = plot_call + |
| 792 | ! |
ggplot2::aes(x = "Entire dataset", y = outlier_var_name) + |
| 793 | ! |
ggplot2::scale_x_discrete(), |
| 794 | ! |
env = list(plot_call = plot_call, outlier_var_name = as.name(outlier_var)) |
| 795 |
) |
|
| 796 | ! |
if (nrow(ANL_OUTLIER) > 0) {
|
| 797 | ! |
substitute( |
| 798 | ! |
expr = inner_call + ggplot2::geom_point( |
| 799 | ! |
data = ANL_OUTLIER, |
| 800 | ! |
ggplot2::aes(x = "Entire dataset", y = outlier_var_name, color = is_outlier_selected) |
| 801 |
), |
|
| 802 | ! |
env = list(inner_call = inner_call, outlier_var_name = as.name(outlier_var)) |
| 803 |
) |
|
| 804 |
} else {
|
|
| 805 | ! |
inner_call |
| 806 |
} |
|
| 807 |
} else {
|
|
| 808 | ! |
substitute( |
| 809 | ! |
expr = plot_call + |
| 810 | ! |
ggplot2::aes(y = outlier_var_name, x = reorder(categorical_var_name, order)) + |
| 811 | ! |
ggplot2::xlab(categorical_var) + |
| 812 | ! |
ggplot2::scale_x_discrete() + |
| 813 | ! |
ggplot2::geom_point( |
| 814 | ! |
data = ANL_OUTLIER, |
| 815 | ! |
ggplot2::aes(x = as.factor(categorical_var_name), y = outlier_var_name, color = is_outlier_selected) |
| 816 |
), |
|
| 817 | ! |
env = list( |
| 818 | ! |
plot_call = plot_call, |
| 819 | ! |
outlier_var_name = as.name(outlier_var), |
| 820 | ! |
categorical_var_name = as.name(categorical_var), |
| 821 | ! |
categorical_var = categorical_var |
| 822 |
) |
|
| 823 |
) |
|
| 824 |
} |
|
| 825 | ||
| 826 | ! |
dev_ggplot2_args <- teal.widgets::ggplot2_args( |
| 827 | ! |
labs = list(color = "Is outlier?"), |
| 828 | ! |
theme = list(legend.position = "top") |
| 829 |
) |
|
| 830 | ||
| 831 | ! |
all_ggplot2_args <- teal.widgets::resolve_ggplot2_args( |
| 832 | ! |
user_plot = ggplot2_args[["Boxplot"]], |
| 833 | ! |
user_default = ggplot2_args$default, |
| 834 | ! |
module_plot = dev_ggplot2_args |
| 835 |
) |
|
| 836 | ||
| 837 | ! |
parsed_ggplot2_args <- teal.widgets::parse_ggplot2_args( |
| 838 | ! |
all_ggplot2_args, |
| 839 | ! |
ggtheme = input$ggtheme |
| 840 |
) |
|
| 841 | ||
| 842 | ! |
teal.code::eval_code( |
| 843 | ! |
common_code_q(), |
| 844 | ! |
substitute( |
| 845 | ! |
expr = box_plot <- plot_call + |
| 846 | ! |
ggplot2::scale_color_manual(values = c("TRUE" = "red", "FALSE" = "black")) +
|
| 847 | ! |
labs + ggthemes + themes, |
| 848 | ! |
env = list( |
| 849 | ! |
plot_call = plot_call, |
| 850 | ! |
labs = parsed_ggplot2_args$labs, |
| 851 | ! |
ggthemes = parsed_ggplot2_args$ggtheme, |
| 852 | ! |
themes = parsed_ggplot2_args$theme |
| 853 |
) |
|
| 854 |
) |
|
| 855 |
) |
|
| 856 |
}) |
|
| 857 | ||
| 858 |
# density plot |
|
| 859 | ! |
density_plot_q <- reactive({
|
| 860 | ! |
ANL <- common_code_q()[["ANL"]] |
| 861 | ! |
ANL_OUTLIER <- common_code_q()[["ANL_OUTLIER"]] |
| 862 | ||
| 863 | ! |
outlier_var <- as.vector(merged$anl_input_r()$columns_source$outlier_var) |
| 864 | ! |
categorical_var <- as.vector(merged$anl_input_r()$columns_source$categorical_var) |
| 865 | ||
| 866 |
# validation |
|
| 867 | ! |
teal::validate_has_data(ANL, 1) |
| 868 |
# plot |
|
| 869 | ! |
plot_call <- substitute( |
| 870 | ! |
expr = ANL %>% |
| 871 | ! |
ggplot2::ggplot(ggplot2::aes(x = outlier_var_name)) + |
| 872 | ! |
ggplot2::geom_density() + |
| 873 | ! |
ggplot2::geom_rug(data = ANL_OUTLIER, ggplot2::aes(x = outlier_var_name, color = is_outlier_selected)) + |
| 874 | ! |
ggplot2::scale_color_manual(values = c("TRUE" = "red", "FALSE" = "black")),
|
| 875 | ! |
env = list(outlier_var_name = as.name(outlier_var)) |
| 876 |
) |
|
| 877 | ||
| 878 | ! |
plot_call <- if (identical(categorical_var, character(0)) || is.null(categorical_var)) {
|
| 879 | ! |
substitute(expr = plot_call, env = list(plot_call = plot_call)) |
| 880 |
} else {
|
|
| 881 | ! |
substitute( |
| 882 | ! |
expr = plot_call + ggplot2::facet_grid(~ reorder(categorical_var_name, order)), |
| 883 | ! |
env = list(plot_call = plot_call, categorical_var_name = as.name(categorical_var)) |
| 884 |
) |
|
| 885 |
} |
|
| 886 | ||
| 887 | ! |
dev_ggplot2_args <- teal.widgets::ggplot2_args( |
| 888 | ! |
labs = list(color = "Is outlier?"), |
| 889 | ! |
theme = list(legend.position = "top") |
| 890 |
) |
|
| 891 | ||
| 892 | ! |
all_ggplot2_args <- teal.widgets::resolve_ggplot2_args( |
| 893 | ! |
user_plot = ggplot2_args[["Density Plot"]], |
| 894 | ! |
user_default = ggplot2_args$default, |
| 895 | ! |
module_plot = dev_ggplot2_args |
| 896 |
) |
|
| 897 | ||
| 898 | ! |
parsed_ggplot2_args <- teal.widgets::parse_ggplot2_args( |
| 899 | ! |
all_ggplot2_args, |
| 900 | ! |
ggtheme = input$ggtheme |
| 901 |
) |
|
| 902 | ||
| 903 | ! |
teal.code::eval_code( |
| 904 | ! |
common_code_q(), |
| 905 | ! |
substitute( |
| 906 | ! |
expr = density_plot <- plot_call + labs + ggthemes + themes, |
| 907 | ! |
env = list( |
| 908 | ! |
plot_call = plot_call, |
| 909 | ! |
labs = parsed_ggplot2_args$labs, |
| 910 | ! |
themes = parsed_ggplot2_args$theme, |
| 911 | ! |
ggthemes = parsed_ggplot2_args$ggtheme |
| 912 |
) |
|
| 913 |
) |
|
| 914 |
) |
|
| 915 |
}) |
|
| 916 | ||
| 917 |
# Cumulative distribution plot |
|
| 918 | ! |
cumulative_plot_q <- reactive({
|
| 919 | ! |
qenv <- common_code_q() |
| 920 | ||
| 921 | ! |
ANL <- qenv[["ANL"]] |
| 922 | ! |
ANL_OUTLIER <- qenv[["ANL_OUTLIER"]] |
| 923 | ||
| 924 | ! |
outlier_var <- as.vector(merged$anl_input_r()$columns_source$outlier_var) |
| 925 | ! |
categorical_var <- as.vector(merged$anl_input_r()$columns_source$categorical_var) |
| 926 | ||
| 927 |
# validation |
|
| 928 | ! |
teal::validate_has_data(ANL, 1) |
| 929 | ||
| 930 |
# plot |
|
| 931 | ! |
plot_call <- substitute( |
| 932 | ! |
expr = ANL %>% ggplot2::ggplot(ggplot2::aes(x = outlier_var_name)) + |
| 933 | ! |
ggplot2::stat_ecdf(), |
| 934 | ! |
env = list(outlier_var_name = as.name(outlier_var)) |
| 935 |
) |
|
| 936 | ! |
if (length(categorical_var) == 0) {
|
| 937 | ! |
qenv <- teal.code::eval_code( |
| 938 | ! |
qenv, |
| 939 | ! |
substitute( |
| 940 | ! |
expr = {
|
| 941 | ! |
ecdf_df <- ANL %>% |
| 942 | ! |
dplyr::mutate( |
| 943 | ! |
y = stats::ecdf(ANL[[outlier_var]])(ANL[[outlier_var]]) |
| 944 |
) |
|
| 945 | ||
| 946 | ! |
outlier_points <- dplyr::left_join( |
| 947 | ! |
ecdf_df, |
| 948 | ! |
ANL_OUTLIER, |
| 949 | ! |
by = dplyr::setdiff(names(ecdf_df), "y") |
| 950 |
) %>% |
|
| 951 | ! |
dplyr::filter(!is.na(is_outlier_selected)) |
| 952 |
}, |
|
| 953 | ! |
env = list(outlier_var = outlier_var) |
| 954 |
) |
|
| 955 |
) |
|
| 956 |
} else {
|
|
| 957 | ! |
qenv <- teal.code::eval_code( |
| 958 | ! |
qenv, |
| 959 | ! |
substitute( |
| 960 | ! |
expr = {
|
| 961 | ! |
all_categories <- lapply( |
| 962 | ! |
unique(ANL[[categorical_var]]), |
| 963 | ! |
function(x) {
|
| 964 | ! |
ANL <- ANL %>% dplyr::filter(get(categorical_var) == x) |
| 965 | ! |
anl_outlier2 <- ANL_OUTLIER %>% dplyr::filter(get(categorical_var) == x) |
| 966 | ! |
ecdf_df <- ANL %>% |
| 967 | ! |
dplyr::mutate(y = stats::ecdf(ANL[[outlier_var]])(ANL[[outlier_var]])) |
| 968 | ||
| 969 | ! |
dplyr::left_join( |
| 970 | ! |
ecdf_df, |
| 971 | ! |
anl_outlier2, |
| 972 | ! |
by = dplyr::setdiff(names(ecdf_df), "y") |
| 973 |
) %>% |
|
| 974 | ! |
dplyr::filter(!is.na(is_outlier_selected)) |
| 975 |
} |
|
| 976 |
) |
|
| 977 | ! |
outlier_points <- do.call(rbind, all_categories) |
| 978 |
}, |
|
| 979 | ! |
env = list(categorical_var = categorical_var, outlier_var = outlier_var) |
| 980 |
) |
|
| 981 |
) |
|
| 982 | ! |
plot_call <- substitute( |
| 983 | ! |
expr = plot_call + ggplot2::facet_grid(~ reorder(categorical_var_name, order)), |
| 984 | ! |
env = list(plot_call = plot_call, categorical_var_name = as.name(categorical_var)) |
| 985 |
) |
|
| 986 |
} |
|
| 987 | ||
| 988 | ! |
dev_ggplot2_args <- teal.widgets::ggplot2_args( |
| 989 | ! |
labs = list(color = "Is outlier?"), |
| 990 | ! |
theme = list(legend.position = "top") |
| 991 |
) |
|
| 992 | ||
| 993 | ! |
all_ggplot2_args <- teal.widgets::resolve_ggplot2_args( |
| 994 | ! |
user_plot = ggplot2_args[["Cumulative Distribution Plot"]], |
| 995 | ! |
user_default = ggplot2_args$default, |
| 996 | ! |
module_plot = dev_ggplot2_args |
| 997 |
) |
|
| 998 | ||
| 999 | ! |
parsed_ggplot2_args <- teal.widgets::parse_ggplot2_args( |
| 1000 | ! |
all_ggplot2_args, |
| 1001 | ! |
ggtheme = input$ggtheme |
| 1002 |
) |
|
| 1003 | ||
| 1004 | ! |
teal.code::eval_code( |
| 1005 | ! |
qenv, |
| 1006 | ! |
substitute( |
| 1007 | ! |
expr = cumulative_plot <- plot_call + |
| 1008 | ! |
ggplot2::geom_point( |
| 1009 | ! |
data = outlier_points, |
| 1010 | ! |
ggplot2::aes(x = outlier_var_name, y = y, color = is_outlier_selected) |
| 1011 |
) + |
|
| 1012 | ! |
ggplot2::scale_color_manual(values = c("TRUE" = "red", "FALSE" = "black")) +
|
| 1013 | ! |
labs + ggthemes + themes, |
| 1014 | ! |
env = list( |
| 1015 | ! |
plot_call = plot_call, |
| 1016 | ! |
outlier_var_name = as.name(outlier_var), |
| 1017 | ! |
labs = parsed_ggplot2_args$labs, |
| 1018 | ! |
themes = parsed_ggplot2_args$theme, |
| 1019 | ! |
ggthemes = parsed_ggplot2_args$ggtheme |
| 1020 |
) |
|
| 1021 |
) |
|
| 1022 |
) |
|
| 1023 |
}) |
|
| 1024 | ||
| 1025 | ! |
current_tab_r <- reactive({
|
| 1026 | ! |
switch(req(input$tabs), |
| 1027 | ! |
"Boxplot" = "box_plot", |
| 1028 | ! |
"Density Plot" = "density_plot", |
| 1029 | ! |
"Cumulative Distribution Plot" = "cumulative_plot" |
| 1030 |
) |
|
| 1031 |
}) |
|
| 1032 | ||
| 1033 | ! |
decorated_q <- mapply( |
| 1034 | ! |
function(obj_name, q) {
|
| 1035 | ! |
srv_decorate_teal_data( |
| 1036 | ! |
id = sprintf("d_%s", obj_name),
|
| 1037 | ! |
data = q, |
| 1038 | ! |
decorators = select_decorators(decorators, obj_name), |
| 1039 | ! |
expr = reactive({
|
| 1040 | ! |
substitute( |
| 1041 | ! |
expr = {
|
| 1042 | ! |
columns_index <- union( |
| 1043 | ! |
setdiff(names(ANL_OUTLIER), c("is_outlier_selected", "order")),
|
| 1044 | ! |
table_columns |
| 1045 |
) |
|
| 1046 | ! |
ANL_OUTLIER_EXTENDED[ANL_OUTLIER_EXTENDED$is_outlier_selected, columns_index] |
| 1047 | ! |
print(.plot) |
| 1048 |
}, |
|
| 1049 | ! |
env = list(table_columns = input$table_ui_columns, .plot = as.name(obj_name)) |
| 1050 |
) |
|
| 1051 |
}), |
|
| 1052 | ! |
expr_is_reactive = TRUE |
| 1053 |
) |
|
| 1054 |
}, |
|
| 1055 | ! |
stats::setNames(nm = c("box_plot", "density_plot", "cumulative_plot")),
|
| 1056 | ! |
c(box_plot_q, density_plot_q, cumulative_plot_q) |
| 1057 |
) |
|
| 1058 | ||
| 1059 | ! |
decorated_final_q <- reactive(decorated_q[[req(current_tab_r())]]()) |
| 1060 | ||
| 1061 | ! |
summary_table_r <- reactive({
|
| 1062 | ! |
q <- req(decorated_final_q()) |
| 1063 | ||
| 1064 | ! |
list( |
| 1065 | ! |
html = DT::datatable( |
| 1066 | ! |
data = if (iv_r()$is_valid()) {
|
| 1067 | ! |
categorical_var <- as.vector(merged$anl_input_r()$columns_source$categorical_var) |
| 1068 | ! |
if (!is.null(categorical_var)) q[["summary_data"]] |
| 1069 |
}, |
|
| 1070 | ! |
option = list( |
| 1071 | ! |
dom = "t", |
| 1072 | ! |
autoWidth = TRUE, |
| 1073 | ! |
columnDefs = list(list(width = "200px", targets = "_all")) |
| 1074 |
) |
|
| 1075 |
), |
|
| 1076 | ! |
report = q[["table"]] |
| 1077 |
) |
|
| 1078 |
}) |
|
| 1079 | ||
| 1080 | ! |
output$summary_table <- DT::renderDataTable(summary_table_r()[["html"]]) |
| 1081 | ||
| 1082 |
# slider text |
|
| 1083 | ! |
output$ui_outlier_help <- renderUI({
|
| 1084 | ! |
req(input$method) |
| 1085 | ! |
if (input$method == "IQR") {
|
| 1086 | ! |
req(input$iqr_slider) |
| 1087 | ! |
tags$small( |
| 1088 | ! |
withMathJax( |
| 1089 | ! |
helpText( |
| 1090 | ! |
"Outlier data points (\\(x \\lt Q1 - ", input$iqr_slider, "\\times IQR\\) or \\( |
| 1091 | ! |
Q3 + ", input$iqr_slider, "\\times IQR \\lt x\\)) |
| 1092 | ! |
are displayed in red on the plot and can be visualized in the table below." |
| 1093 |
), |
|
| 1094 | ! |
if (input$split_outliers) {
|
| 1095 | ! |
withMathJax(helpText("Note: Quantiles are calculated per group."))
|
| 1096 |
} |
|
| 1097 |
) |
|
| 1098 |
) |
|
| 1099 | ! |
} else if (input$method == "Z-score") {
|
| 1100 | ! |
req(input$zscore_slider) |
| 1101 | ! |
tags$small( |
| 1102 | ! |
withMathJax( |
| 1103 | ! |
helpText( |
| 1104 | ! |
"Outlier data points (\\(Zscore(x) < -", input$zscore_slider, |
| 1105 | ! |
"\\) or \\(", input$zscore_slider, "< Zscore(x) \\))
|
| 1106 | ! |
are displayed in red on the plot and can be visualized in the table below." |
| 1107 |
), |
|
| 1108 | ! |
if (input$split_outliers) {
|
| 1109 | ! |
withMathJax(helpText(" Note: Z-scores are calculated per group."))
|
| 1110 |
} |
|
| 1111 |
) |
|
| 1112 |
) |
|
| 1113 | ! |
} else if (input$method == "Percentile") {
|
| 1114 | ! |
req(input$percentile_slider) |
| 1115 | ! |
tags$small( |
| 1116 | ! |
withMathJax( |
| 1117 | ! |
helpText( |
| 1118 | ! |
"Outlier/extreme data points (\\( Percentile(x) <", input$percentile_slider, |
| 1119 | ! |
"\\) or \\(", 1 - input$percentile_slider, " < Percentile(x) \\))
|
| 1120 | ! |
are displayed in red on the plot and can be visualized in the table below." |
| 1121 |
), |
|
| 1122 | ! |
if (input$split_outliers) {
|
| 1123 | ! |
withMathJax(helpText("Note: Percentiles are calculated per group."))
|
| 1124 |
} |
|
| 1125 |
) |
|
| 1126 |
) |
|
| 1127 |
} |
|
| 1128 |
}) |
|
| 1129 | ||
| 1130 | ! |
box_plot_r <- reactive({
|
| 1131 | ! |
teal::validate_inputs(iv_r()) |
| 1132 | ! |
req(decorated_q$box_plot())[["box_plot"]] |
| 1133 |
}) |
|
| 1134 | ! |
density_plot_r <- reactive({
|
| 1135 | ! |
teal::validate_inputs(iv_r()) |
| 1136 | ! |
req(decorated_q$density_plot())[["density_plot"]] |
| 1137 |
}) |
|
| 1138 | ! |
cumulative_plot_r <- reactive({
|
| 1139 | ! |
teal::validate_inputs(iv_r()) |
| 1140 | ! |
req(decorated_q$cumulative_plot())[["cumulative_plot"]] |
| 1141 |
}) |
|
| 1142 | ||
| 1143 | ! |
box_pws <- teal.widgets::plot_with_settings_srv( |
| 1144 | ! |
id = "box_plot", |
| 1145 | ! |
plot_r = box_plot_r, |
| 1146 | ! |
height = plot_height, |
| 1147 | ! |
width = plot_width, |
| 1148 | ! |
brushing = TRUE |
| 1149 |
) |
|
| 1150 | ||
| 1151 | ! |
density_pws <- teal.widgets::plot_with_settings_srv( |
| 1152 | ! |
id = "density_plot", |
| 1153 | ! |
plot_r = density_plot_r, |
| 1154 | ! |
height = plot_height, |
| 1155 | ! |
width = plot_width, |
| 1156 | ! |
brushing = TRUE |
| 1157 |
) |
|
| 1158 | ||
| 1159 | ! |
cum_density_pws <- teal.widgets::plot_with_settings_srv( |
| 1160 | ! |
id = "cum_density_plot", |
| 1161 | ! |
plot_r = cumulative_plot_r, |
| 1162 | ! |
height = plot_height, |
| 1163 | ! |
width = plot_width, |
| 1164 | ! |
brushing = TRUE |
| 1165 |
) |
|
| 1166 | ||
| 1167 | ! |
choices <- reactive(teal.transform::variable_choices(data_obj()[[dataname_first]])) |
| 1168 | ||
| 1169 | ! |
observeEvent(common_code_q(), {
|
| 1170 | ! |
ANL_OUTLIER <- common_code_q()[["ANL_OUTLIER"]] |
| 1171 | ! |
teal.widgets::updateOptionalSelectInput( |
| 1172 | ! |
session, |
| 1173 | ! |
inputId = "table_ui_columns", |
| 1174 | ! |
choices = dplyr::setdiff(choices(), names(ANL_OUTLIER)), |
| 1175 | ! |
selected = restoreInput(ns("table_ui_columns"), isolate(input$table_ui_columns))
|
| 1176 |
) |
|
| 1177 |
}) |
|
| 1178 | ||
| 1179 | ! |
output$table_ui <- DT::renderDataTable( |
| 1180 | ! |
expr = {
|
| 1181 | ! |
tab <- input$tabs |
| 1182 | ! |
req(tab) # tab is NULL upon app launch, hence will crash without this statement |
| 1183 | ! |
req(iv_r()$is_valid()) # Same validation as output$table_ui_wrap |
| 1184 | ! |
outlier_var <- as.vector(merged$anl_input_r()$columns_source$outlier_var) |
| 1185 | ! |
categorical_var <- as.vector(merged$anl_input_r()$columns_source$categorical_var) |
| 1186 | ||
| 1187 | ! |
ANL_OUTLIER <- common_code_q()[["ANL_OUTLIER"]] |
| 1188 | ! |
ANL_OUTLIER_EXTENDED <- common_code_q()[["ANL_OUTLIER_EXTENDED"]] |
| 1189 | ! |
ANL <- common_code_q()[["ANL"]] |
| 1190 | ||
| 1191 | ! |
plot_brush <- switch(current_tab_r(), |
| 1192 | ! |
box_plot = {
|
| 1193 | ! |
box_plot_r() |
| 1194 | ! |
box_pws$brush() |
| 1195 |
}, |
|
| 1196 | ! |
density_plot = {
|
| 1197 | ! |
density_plot_r() |
| 1198 | ! |
density_pws$brush() |
| 1199 |
}, |
|
| 1200 | ! |
cumulative_plot = {
|
| 1201 | ! |
cumulative_plot_r() |
| 1202 | ! |
cum_density_pws$brush() |
| 1203 |
} |
|
| 1204 |
) |
|
| 1205 | ||
| 1206 |
# removing unused column ASAP |
|
| 1207 | ! |
ANL_OUTLIER$order <- ANL$order <- NULL |
| 1208 | ||
| 1209 | ! |
display_table <- if (!is.null(plot_brush)) {
|
| 1210 | ! |
if (length(categorical_var) > 0) {
|
| 1211 |
# due to reordering, the x-axis label may be changed to something like "reorder(categorical_var, order)" |
|
| 1212 | ! |
if (tab == "Boxplot") {
|
| 1213 | ! |
plot_brush$mapping$x <- categorical_var |
| 1214 |
} else {
|
|
| 1215 |
# the other plots use facetting |
|
| 1216 |
# so it is panelvar1 that gets relabelled to "reorder(categorical_var, order)" |
|
| 1217 | ! |
plot_brush$mapping$panelvar1 <- categorical_var |
| 1218 |
} |
|
| 1219 |
} else {
|
|
| 1220 | ! |
if (tab == "Boxplot") {
|
| 1221 |
# in boxplot with no categorical variable, there is no column in ANL that would correspond to x-axis |
|
| 1222 |
# so a column needs to be inserted with the value "Entire dataset" because that's the label used in plot |
|
| 1223 | ! |
ANL[[plot_brush$mapping$x]] <- "Entire dataset" |
| 1224 |
} |
|
| 1225 |
} |
|
| 1226 | ||
| 1227 |
# in density and cumulative plots, ANL does not have a column corresponding to y-axis. |
|
| 1228 |
# so they need to be computed and attached to ANL |
|
| 1229 | ! |
if (tab == "Density Plot") {
|
| 1230 | ! |
plot_brush$mapping$y <- "density" |
| 1231 | ! |
ANL$density <- plot_brush$ymin |
| 1232 |
# either ymin or ymax will work |
|
| 1233 | ! |
} else if (tab == "Cumulative Distribution Plot") {
|
| 1234 | ! |
plot_brush$mapping$y <- "cdf" |
| 1235 | ! |
if (length(categorical_var) > 0) {
|
| 1236 | ! |
ANL <- ANL %>% |
| 1237 | ! |
dplyr::group_by(!!as.name(plot_brush$mapping$panelvar1)) %>% |
| 1238 | ! |
dplyr::mutate(cdf = stats::ecdf(!!as.name(outlier_var))(!!as.name(outlier_var))) |
| 1239 |
} else {
|
|
| 1240 | ! |
ANL$cdf <- stats::ecdf(ANL[[outlier_var]])(ANL[[outlier_var]]) |
| 1241 |
} |
|
| 1242 |
} |
|
| 1243 | ||
| 1244 | ! |
brushed_rows <- brushedPoints(ANL, plot_brush) |
| 1245 | ! |
if (nrow(brushed_rows) > 0) {
|
| 1246 |
# now we need to remove extra column from ANL so that it will have the same columns as ANL_OUTLIER |
|
| 1247 |
# so that dplyr::intersect will work |
|
| 1248 | ! |
if (tab == "Density Plot") {
|
| 1249 | ! |
brushed_rows$density <- NULL |
| 1250 | ! |
} else if (tab == "Cumulative Distribution Plot") {
|
| 1251 | ! |
brushed_rows$cdf <- NULL |
| 1252 | ! |
} else if (tab == "Boxplot" && length(categorical_var) == 0) {
|
| 1253 | ! |
brushed_rows[[plot_brush$mapping$x]] <- NULL |
| 1254 |
} |
|
| 1255 |
# is_outlier_selected is part of ANL_OUTLIER so needed here |
|
| 1256 | ! |
brushed_rows$is_outlier_selected <- TRUE |
| 1257 | ! |
dplyr::intersect(ANL_OUTLIER, brushed_rows) |
| 1258 |
} else {
|
|
| 1259 | ! |
ANL_OUTLIER[0, ] |
| 1260 |
} |
|
| 1261 |
} else {
|
|
| 1262 | ! |
ANL_OUTLIER[ANL_OUTLIER$is_outlier_selected, ] |
| 1263 |
} |
|
| 1264 | ||
| 1265 | ! |
display_table$is_outlier_selected <- NULL |
| 1266 | ||
| 1267 |
# Extend the brushed ANL_OUTLIER with additional columns |
|
| 1268 | ! |
dplyr::left_join( |
| 1269 | ! |
display_table, |
| 1270 | ! |
dplyr::select(ANL_OUTLIER_EXTENDED, -"is_outlier_selected"), |
| 1271 | ! |
by = names(display_table) |
| 1272 |
) %>% |
|
| 1273 | ! |
dplyr::select(union(names(display_table), input$table_ui_columns)) |
| 1274 |
}, |
|
| 1275 | ! |
options = list( |
| 1276 | ! |
searching = FALSE, language = list( |
| 1277 | ! |
zeroRecords = "The brushed area does not contain outlier observations for the currently defined threshold" |
| 1278 |
), |
|
| 1279 | ! |
pageLength = input$table_ui_rows |
| 1280 |
) |
|
| 1281 |
) |
|
| 1282 | ||
| 1283 | ! |
output$total_outliers <- renderUI({
|
| 1284 | ! |
req(iv_r()$is_valid()) |
| 1285 | ! |
ANL <- merged$anl_q_r()[["ANL"]] |
| 1286 | ! |
ANL_OUTLIER <- common_code_q()[["ANL_OUTLIER"]] |
| 1287 | ! |
teal::validate_has_data(ANL, 1) |
| 1288 | ! |
ANL_OUTLIER_SELECTED <- ANL_OUTLIER[ANL_OUTLIER$is_outlier_selected, ] |
| 1289 | ! |
tags$h5( |
| 1290 | ! |
sprintf( |
| 1291 | ! |
"%s %d / %d [%.02f%%]", |
| 1292 | ! |
"Total number of outlier(s):", |
| 1293 | ! |
nrow(ANL_OUTLIER_SELECTED), |
| 1294 | ! |
nrow(ANL), |
| 1295 | ! |
100 * nrow(ANL_OUTLIER_SELECTED) / nrow(ANL) |
| 1296 |
) |
|
| 1297 |
) |
|
| 1298 |
}) |
|
| 1299 | ||
| 1300 | ! |
output$total_missing <- renderUI({
|
| 1301 | ! |
if (n_outlier_missing() > 0) {
|
| 1302 | ! |
ANL <- merged$anl_q_r()[["ANL"]] |
| 1303 | ! |
helpText( |
| 1304 | ! |
sprintf( |
| 1305 | ! |
"%s %d / %d [%.02f%%]", |
| 1306 | ! |
"Total number of row(s) with missing values:", |
| 1307 | ! |
n_outlier_missing(), |
| 1308 | ! |
nrow(ANL), |
| 1309 | ! |
100 * (n_outlier_missing()) / nrow(ANL) |
| 1310 |
) |
|
| 1311 |
) |
|
| 1312 |
} |
|
| 1313 |
}) |
|
| 1314 | ||
| 1315 | ! |
output$table_ui_wrap <- renderUI({
|
| 1316 | ! |
req(iv_r()$is_valid()) |
| 1317 | ! |
tagList( |
| 1318 | ! |
teal.widgets::optionalSelectInput( |
| 1319 | ! |
inputId = ns("table_ui_columns"),
|
| 1320 | ! |
label = "Choose additional columns", |
| 1321 | ! |
choices = NULL, |
| 1322 | ! |
selected = NULL, |
| 1323 | ! |
multiple = TRUE |
| 1324 |
), |
|
| 1325 | ! |
tags$h4("Outlier Table"),
|
| 1326 | ! |
teal.widgets::get_dt_rows(ns("table_ui"), ns("table_ui_rows"))
|
| 1327 |
) |
|
| 1328 |
}) |
|
| 1329 | ||
| 1330 |
# Render R code. |
|
| 1331 | ! |
source_code_r <- reactive(teal.code::get_code(req(decorated_final_q()))) |
| 1332 | ||
| 1333 | ! |
teal.widgets::verbatim_popup_srv( |
| 1334 | ! |
id = "rcode", |
| 1335 | ! |
verbatim_content = source_code_r, |
| 1336 | ! |
title = "Show R Code for Outlier" |
| 1337 |
) |
|
| 1338 | ||
| 1339 |
### REPORTER |
|
| 1340 | ! |
if (with_reporter) {
|
| 1341 | ! |
card_fun <- function(comment, label) {
|
| 1342 | ! |
tab_type <- input$tabs |
| 1343 | ! |
card <- teal::report_card_template( |
| 1344 | ! |
title = paste0("Outliers - ", tab_type),
|
| 1345 | ! |
label = label, |
| 1346 | ! |
with_filter = with_filter, |
| 1347 | ! |
filter_panel_api = filter_panel_api |
| 1348 |
) |
|
| 1349 | ! |
categorical_var <- as.vector(merged$anl_input_r()$columns_source$categorical_var) |
| 1350 | ! |
if (length(categorical_var) > 0) {
|
| 1351 | ! |
card$append_text("Summary Table", "header3")
|
| 1352 | ! |
card$append_table(summary_table_r()[["report"]]) |
| 1353 |
} |
|
| 1354 | ! |
card$append_text("Plot", "header3")
|
| 1355 | ! |
if (tab_type == "Boxplot") {
|
| 1356 | ! |
card$append_plot(box_plot_r(), dim = box_pws$dim()) |
| 1357 | ! |
} else if (tab_type == "Density Plot") {
|
| 1358 | ! |
card$append_plot(density_plot_r(), dim = density_pws$dim()) |
| 1359 | ! |
} else if (tab_type == "Cumulative Distribution Plot") {
|
| 1360 | ! |
card$append_plot(cumulative_plot_r(), dim = cum_density_pws$dim()) |
| 1361 |
} |
|
| 1362 | ! |
if (!comment == "") {
|
| 1363 | ! |
card$append_text("Comment", "header3")
|
| 1364 | ! |
card$append_text(comment) |
| 1365 |
} |
|
| 1366 | ! |
card$append_src(source_code_r()) |
| 1367 | ! |
card |
| 1368 |
} |
|
| 1369 | ! |
teal.reporter::add_card_button_srv("add_reporter", reporter = reporter, card_fun = card_fun)
|
| 1370 |
} |
|
| 1371 |
### |
|
| 1372 |
}) |
|
| 1373 |
} |
| 1 |
#' `teal` module: Scatterplot |
|
| 2 |
#' |
|
| 3 |
#' Generates a customizable scatterplot using `ggplot2`. |
|
| 4 |
#' This module allows users to select variables for the x and y axes, |
|
| 5 |
#' color and size encodings, faceting options, and more. It supports log transformations, |
|
| 6 |
#' trend line additions, and dynamic adjustments of point opacity and size through UI controls. |
|
| 7 |
#' |
|
| 8 |
#' @note For more examples, please see the vignette "Using scatterplot" via |
|
| 9 |
#' `vignette("using-scatterplot", package = "teal.modules.general")`.
|
|
| 10 |
#' |
|
| 11 |
#' @inheritParams teal::module |
|
| 12 |
#' @inheritParams shared_params |
|
| 13 |
#' @param x (`data_extract_spec` or `list` of multiple `data_extract_spec`) Specifies |
|
| 14 |
#' variable names selected to plot along the x-axis by default. |
|
| 15 |
#' @param y (`data_extract_spec` or `list` of multiple `data_extract_spec`) Specifies |
|
| 16 |
#' variable names selected to plot along the y-axis by default. |
|
| 17 |
#' @param color_by (`data_extract_spec` or `list` of multiple `data_extract_spec`) optional, |
|
| 18 |
#' defines the color encoding. If `NULL` then no color encoding option will be displayed. |
|
| 19 |
#' @param size_by (`data_extract_spec` or `list` of multiple `data_extract_spec`) optional, |
|
| 20 |
#' defines the point size encoding. If `NULL` then no size encoding option will be displayed. |
|
| 21 |
#' @param row_facet (`data_extract_spec` or `list` of multiple `data_extract_spec`) optional, |
|
| 22 |
#' specifies the variable(s) for faceting rows. |
|
| 23 |
#' @param col_facet (`data_extract_spec` or `list` of multiple `data_extract_spec`) optional, |
|
| 24 |
#' specifies the variable(s) for faceting columns. |
|
| 25 |
#' @param shape (`character`) optional, character vector with the names of the |
|
| 26 |
#' shape, e.g. `c("triangle", "square", "circle")`. It defaults to `shape_names`. This is a complete list from
|
|
| 27 |
#' `vignette("ggplot2-specs", package="ggplot2")`.
|
|
| 28 |
#' @param max_deg (`integer`) optional, maximum degree for the polynomial trend line. Must not be less than 1. |
|
| 29 |
#' @param table_dec (`integer`) optional, number of decimal places used to round numeric values in the table. |
|
| 30 |
#' |
|
| 31 |
#' @inherit shared_params return |
|
| 32 |
#' |
|
| 33 |
#' @section Decorating Module: |
|
| 34 |
#' |
|
| 35 |
#' This module generates the following objects, which can be modified in place using decorators: |
|
| 36 |
#' - `plot` (`ggplot`) |
|
| 37 |
#' |
|
| 38 |
#' A Decorator is applied to the specific output using a named list of `teal_transform_module` objects. |
|
| 39 |
#' The name of this list corresponds to the name of the output to which the decorator is applied. |
|
| 40 |
#' See code snippet below: |
|
| 41 |
#' |
|
| 42 |
#' ``` |
|
| 43 |
#' tm_g_scatterplot( |
|
| 44 |
#' ..., # arguments for module |
|
| 45 |
#' decorators = list( |
|
| 46 |
#' plot = teal_transform_module(...) # applied to the `plot` output |
|
| 47 |
#' ) |
|
| 48 |
#' ) |
|
| 49 |
#' ``` |
|
| 50 |
#' |
|
| 51 |
#' For additional details and examples of decorators, refer to the vignette |
|
| 52 |
#' `vignette("decorate-module-output", package = "teal.modules.general")`.
|
|
| 53 |
#' |
|
| 54 |
#' To learn more please refer to the vignette |
|
| 55 |
#' `vignette("transform-module-output", package = "teal")` or the [`teal::teal_transform_module()`] documentation.
|
|
| 56 |
#' |
|
| 57 |
#' |
|
| 58 |
#' @examplesShinylive |
|
| 59 |
#' library(teal.modules.general) |
|
| 60 |
#' interactive <- function() TRUE |
|
| 61 |
#' {{ next_example }}
|
|
| 62 |
# nolint start: line_length_linter. |
|
| 63 |
#' @examples |
|
| 64 |
# nolint end: line_length_linter. |
|
| 65 |
#' # general data example |
|
| 66 |
#' data <- teal_data() |
|
| 67 |
#' data <- within(data, {
|
|
| 68 |
#' require(nestcolor) |
|
| 69 |
#' CO2 <- CO2 |
|
| 70 |
#' }) |
|
| 71 |
#' |
|
| 72 |
#' app <- init( |
|
| 73 |
#' data = data, |
|
| 74 |
#' modules = modules( |
|
| 75 |
#' tm_g_scatterplot( |
|
| 76 |
#' label = "Scatterplot Choices", |
|
| 77 |
#' x = data_extract_spec( |
|
| 78 |
#' dataname = "CO2", |
|
| 79 |
#' select = select_spec( |
|
| 80 |
#' label = "Select variable:", |
|
| 81 |
#' choices = variable_choices(data[["CO2"]], c("conc", "uptake")),
|
|
| 82 |
#' selected = "conc", |
|
| 83 |
#' multiple = FALSE, |
|
| 84 |
#' fixed = FALSE |
|
| 85 |
#' ) |
|
| 86 |
#' ), |
|
| 87 |
#' y = data_extract_spec( |
|
| 88 |
#' dataname = "CO2", |
|
| 89 |
#' select = select_spec( |
|
| 90 |
#' label = "Select variable:", |
|
| 91 |
#' choices = variable_choices(data[["CO2"]], c("conc", "uptake")),
|
|
| 92 |
#' selected = "uptake", |
|
| 93 |
#' multiple = FALSE, |
|
| 94 |
#' fixed = FALSE |
|
| 95 |
#' ) |
|
| 96 |
#' ), |
|
| 97 |
#' color_by = data_extract_spec( |
|
| 98 |
#' dataname = "CO2", |
|
| 99 |
#' select = select_spec( |
|
| 100 |
#' label = "Select variable:", |
|
| 101 |
#' choices = variable_choices( |
|
| 102 |
#' data[["CO2"]], |
|
| 103 |
#' c("Plant", "Type", "Treatment", "conc", "uptake")
|
|
| 104 |
#' ), |
|
| 105 |
#' selected = NULL, |
|
| 106 |
#' multiple = FALSE, |
|
| 107 |
#' fixed = FALSE |
|
| 108 |
#' ) |
|
| 109 |
#' ), |
|
| 110 |
#' size_by = data_extract_spec( |
|
| 111 |
#' dataname = "CO2", |
|
| 112 |
#' select = select_spec( |
|
| 113 |
#' label = "Select variable:", |
|
| 114 |
#' choices = variable_choices(data[["CO2"]], c("conc", "uptake")),
|
|
| 115 |
#' selected = "uptake", |
|
| 116 |
#' multiple = FALSE, |
|
| 117 |
#' fixed = FALSE |
|
| 118 |
#' ) |
|
| 119 |
#' ), |
|
| 120 |
#' row_facet = data_extract_spec( |
|
| 121 |
#' dataname = "CO2", |
|
| 122 |
#' select = select_spec( |
|
| 123 |
#' label = "Select variable:", |
|
| 124 |
#' choices = variable_choices(data[["CO2"]], c("Plant", "Type", "Treatment")),
|
|
| 125 |
#' selected = NULL, |
|
| 126 |
#' multiple = FALSE, |
|
| 127 |
#' fixed = FALSE |
|
| 128 |
#' ) |
|
| 129 |
#' ), |
|
| 130 |
#' col_facet = data_extract_spec( |
|
| 131 |
#' dataname = "CO2", |
|
| 132 |
#' select = select_spec( |
|
| 133 |
#' label = "Select variable:", |
|
| 134 |
#' choices = variable_choices(data[["CO2"]], c("Plant", "Type", "Treatment")),
|
|
| 135 |
#' selected = NULL, |
|
| 136 |
#' multiple = FALSE, |
|
| 137 |
#' fixed = FALSE |
|
| 138 |
#' ) |
|
| 139 |
#' ) |
|
| 140 |
#' ) |
|
| 141 |
#' ) |
|
| 142 |
#' ) |
|
| 143 |
#' if (interactive()) {
|
|
| 144 |
#' shinyApp(app$ui, app$server) |
|
| 145 |
#' } |
|
| 146 |
#' |
|
| 147 |
#' @examplesShinylive |
|
| 148 |
#' library(teal.modules.general) |
|
| 149 |
#' interactive <- function() TRUE |
|
| 150 |
#' {{ next_example }}
|
|
| 151 |
# nolint start: line_length_linter. |
|
| 152 |
#' @examples |
|
| 153 |
# nolint end: line_length_linter. |
|
| 154 |
#' # CDISC data example |
|
| 155 |
#' data <- teal_data() |
|
| 156 |
#' data <- within(data, {
|
|
| 157 |
#' require(nestcolor) |
|
| 158 |
#' ADSL <- teal.data::rADSL |
|
| 159 |
#' }) |
|
| 160 |
#' join_keys(data) <- default_cdisc_join_keys[names(data)] |
|
| 161 |
#' |
|
| 162 |
#' app <- init( |
|
| 163 |
#' data = data, |
|
| 164 |
#' modules = modules( |
|
| 165 |
#' tm_g_scatterplot( |
|
| 166 |
#' label = "Scatterplot Choices", |
|
| 167 |
#' x = data_extract_spec( |
|
| 168 |
#' dataname = "ADSL", |
|
| 169 |
#' select = select_spec( |
|
| 170 |
#' label = "Select variable:", |
|
| 171 |
#' choices = variable_choices(data[["ADSL"]], c("AGE", "BMRKR1", "BMRKR2")),
|
|
| 172 |
#' selected = "AGE", |
|
| 173 |
#' multiple = FALSE, |
|
| 174 |
#' fixed = FALSE |
|
| 175 |
#' ) |
|
| 176 |
#' ), |
|
| 177 |
#' y = data_extract_spec( |
|
| 178 |
#' dataname = "ADSL", |
|
| 179 |
#' select = select_spec( |
|
| 180 |
#' label = "Select variable:", |
|
| 181 |
#' choices = variable_choices(data[["ADSL"]], c("AGE", "BMRKR1", "BMRKR2")),
|
|
| 182 |
#' selected = "BMRKR1", |
|
| 183 |
#' multiple = FALSE, |
|
| 184 |
#' fixed = FALSE |
|
| 185 |
#' ) |
|
| 186 |
#' ), |
|
| 187 |
#' color_by = data_extract_spec( |
|
| 188 |
#' dataname = "ADSL", |
|
| 189 |
#' select = select_spec( |
|
| 190 |
#' label = "Select variable:", |
|
| 191 |
#' choices = variable_choices( |
|
| 192 |
#' data[["ADSL"]], |
|
| 193 |
#' c("AGE", "BMRKR1", "BMRKR2", "RACE", "REGION1")
|
|
| 194 |
#' ), |
|
| 195 |
#' selected = NULL, |
|
| 196 |
#' multiple = FALSE, |
|
| 197 |
#' fixed = FALSE |
|
| 198 |
#' ) |
|
| 199 |
#' ), |
|
| 200 |
#' size_by = data_extract_spec( |
|
| 201 |
#' dataname = "ADSL", |
|
| 202 |
#' select = select_spec( |
|
| 203 |
#' label = "Select variable:", |
|
| 204 |
#' choices = variable_choices(data[["ADSL"]], c("AGE", "BMRKR1")),
|
|
| 205 |
#' selected = "AGE", |
|
| 206 |
#' multiple = FALSE, |
|
| 207 |
#' fixed = FALSE |
|
| 208 |
#' ) |
|
| 209 |
#' ), |
|
| 210 |
#' row_facet = data_extract_spec( |
|
| 211 |
#' dataname = "ADSL", |
|
| 212 |
#' select = select_spec( |
|
| 213 |
#' label = "Select variable:", |
|
| 214 |
#' choices = variable_choices(data[["ADSL"]], c("BMRKR2", "RACE", "REGION1")),
|
|
| 215 |
#' selected = NULL, |
|
| 216 |
#' multiple = FALSE, |
|
| 217 |
#' fixed = FALSE |
|
| 218 |
#' ) |
|
| 219 |
#' ), |
|
| 220 |
#' col_facet = data_extract_spec( |
|
| 221 |
#' dataname = "ADSL", |
|
| 222 |
#' select = select_spec( |
|
| 223 |
#' label = "Select variable:", |
|
| 224 |
#' choices = variable_choices(data[["ADSL"]], c("BMRKR2", "RACE", "REGION1")),
|
|
| 225 |
#' selected = NULL, |
|
| 226 |
#' multiple = FALSE, |
|
| 227 |
#' fixed = FALSE |
|
| 228 |
#' ) |
|
| 229 |
#' ) |
|
| 230 |
#' ) |
|
| 231 |
#' ) |
|
| 232 |
#' ) |
|
| 233 |
#' if (interactive()) {
|
|
| 234 |
#' shinyApp(app$ui, app$server) |
|
| 235 |
#' } |
|
| 236 |
#' |
|
| 237 |
#' @export |
|
| 238 |
#' |
|
| 239 |
tm_g_scatterplot <- function(label = "Scatterplot", |
|
| 240 |
x, |
|
| 241 |
y, |
|
| 242 |
color_by = NULL, |
|
| 243 |
size_by = NULL, |
|
| 244 |
row_facet = NULL, |
|
| 245 |
col_facet = NULL, |
|
| 246 |
plot_height = c(600, 200, 2000), |
|
| 247 |
plot_width = NULL, |
|
| 248 |
alpha = c(1, 0, 1), |
|
| 249 |
shape = shape_names, |
|
| 250 |
size = c(5, 1, 15), |
|
| 251 |
max_deg = 5L, |
|
| 252 |
rotate_xaxis_labels = FALSE, |
|
| 253 |
ggtheme = c("gray", "bw", "linedraw", "light", "dark", "minimal", "classic", "void"),
|
|
| 254 |
pre_output = NULL, |
|
| 255 |
post_output = NULL, |
|
| 256 |
table_dec = 4, |
|
| 257 |
ggplot2_args = teal.widgets::ggplot2_args(), |
|
| 258 |
transformators = list(), |
|
| 259 |
decorators = list()) {
|
|
| 260 | ! |
message("Initializing tm_g_scatterplot")
|
| 261 | ||
| 262 |
# Normalize the parameters |
|
| 263 | ! |
if (inherits(x, "data_extract_spec")) x <- list(x) |
| 264 | ! |
if (inherits(y, "data_extract_spec")) y <- list(y) |
| 265 | ! |
if (inherits(color_by, "data_extract_spec")) color_by <- list(color_by) |
| 266 | ! |
if (inherits(size_by, "data_extract_spec")) size_by <- list(size_by) |
| 267 | ! |
if (inherits(row_facet, "data_extract_spec")) row_facet <- list(row_facet) |
| 268 | ! |
if (inherits(col_facet, "data_extract_spec")) col_facet <- list(col_facet) |
| 269 | ! |
if (is.double(max_deg)) max_deg <- as.integer(max_deg) |
| 270 | ||
| 271 |
# Start of assertions |
|
| 272 | ! |
checkmate::assert_string(label) |
| 273 | ! |
checkmate::assert_list(x, types = "data_extract_spec") |
| 274 | ! |
checkmate::assert_list(y, types = "data_extract_spec") |
| 275 | ! |
checkmate::assert_list(color_by, types = "data_extract_spec", null.ok = TRUE) |
| 276 | ! |
checkmate::assert_list(size_by, types = "data_extract_spec", null.ok = TRUE) |
| 277 | ||
| 278 | ! |
checkmate::assert_list(row_facet, types = "data_extract_spec", null.ok = TRUE) |
| 279 | ! |
assert_single_selection(row_facet) |
| 280 | ||
| 281 | ! |
checkmate::assert_list(col_facet, types = "data_extract_spec", null.ok = TRUE) |
| 282 | ! |
assert_single_selection(col_facet) |
| 283 | ||
| 284 | ! |
checkmate::assert_numeric(plot_height, len = 3, any.missing = FALSE, finite = TRUE) |
| 285 | ! |
checkmate::assert_numeric(plot_height[1], lower = plot_height[2], upper = plot_height[3], .var.name = "plot_height") |
| 286 | ! |
checkmate::assert_numeric(plot_width, len = 3, any.missing = FALSE, null.ok = TRUE, finite = TRUE) |
| 287 | ! |
checkmate::assert_numeric( |
| 288 | ! |
plot_width[1], |
| 289 | ! |
lower = plot_width[2], upper = plot_width[3], null.ok = TRUE, .var.name = "plot_width" |
| 290 |
) |
|
| 291 | ||
| 292 | ! |
if (length(alpha) == 1) {
|
| 293 | ! |
checkmate::assert_numeric(alpha, any.missing = FALSE, finite = TRUE) |
| 294 |
} else {
|
|
| 295 | ! |
checkmate::assert_numeric(alpha, len = 3, any.missing = FALSE, finite = TRUE) |
| 296 | ! |
checkmate::assert_numeric(alpha[1], lower = alpha[2], upper = alpha[3], .var.name = "alpha") |
| 297 |
} |
|
| 298 | ||
| 299 | ! |
checkmate::assert_character(shape) |
| 300 | ||
| 301 | ! |
if (length(size) == 1) {
|
| 302 | ! |
checkmate::assert_numeric(size, any.missing = FALSE, finite = TRUE) |
| 303 |
} else {
|
|
| 304 | ! |
checkmate::assert_numeric(size, len = 3, any.missing = FALSE, finite = TRUE) |
| 305 | ! |
checkmate::assert_numeric(size[1], lower = size[2], upper = size[3], .var.name = "size") |
| 306 |
} |
|
| 307 | ||
| 308 | ! |
checkmate::assert_int(max_deg, lower = 1L) |
| 309 | ! |
checkmate::assert_flag(rotate_xaxis_labels) |
| 310 | ! |
ggtheme <- match.arg(ggtheme) |
| 311 | ||
| 312 | ! |
checkmate::assert_multi_class(pre_output, c("shiny.tag", "shiny.tag.list", "html"), null.ok = TRUE)
|
| 313 | ! |
checkmate::assert_multi_class(post_output, c("shiny.tag", "shiny.tag.list", "html"), null.ok = TRUE)
|
| 314 | ||
| 315 | ! |
checkmate::assert_scalar(table_dec) |
| 316 | ! |
checkmate::assert_class(ggplot2_args, "ggplot2_args") |
| 317 | ||
| 318 | ! |
assert_decorators(decorators, "plot") |
| 319 | ||
| 320 |
# End of assertions |
|
| 321 | ||
| 322 |
# Make UI args |
|
| 323 | ! |
args <- as.list(environment()) |
| 324 | ||
| 325 | ! |
data_extract_list <- list( |
| 326 | ! |
x = x, |
| 327 | ! |
y = y, |
| 328 | ! |
color_by = color_by, |
| 329 | ! |
size_by = size_by, |
| 330 | ! |
row_facet = row_facet, |
| 331 | ! |
col_facet = col_facet |
| 332 |
) |
|
| 333 | ||
| 334 | ! |
ans <- module( |
| 335 | ! |
label = label, |
| 336 | ! |
server = srv_g_scatterplot, |
| 337 | ! |
ui = ui_g_scatterplot, |
| 338 | ! |
ui_args = args, |
| 339 | ! |
server_args = c( |
| 340 | ! |
data_extract_list, |
| 341 | ! |
list( |
| 342 | ! |
plot_height = plot_height, |
| 343 | ! |
plot_width = plot_width, |
| 344 | ! |
table_dec = table_dec, |
| 345 | ! |
ggplot2_args = ggplot2_args, |
| 346 | ! |
decorators = decorators |
| 347 |
) |
|
| 348 |
), |
|
| 349 | ! |
transformators = transformators, |
| 350 | ! |
datanames = teal.transform::get_extract_datanames(data_extract_list) |
| 351 |
) |
|
| 352 | ! |
attr(ans, "teal_bookmarkable") <- TRUE |
| 353 | ! |
ans |
| 354 |
} |
|
| 355 | ||
| 356 |
# UI function for the scatterplot module |
|
| 357 |
ui_g_scatterplot <- function(id, ...) {
|
|
| 358 | ! |
args <- list(...) |
| 359 | ! |
ns <- NS(id) |
| 360 | ! |
is_single_dataset_value <- teal.transform::is_single_dataset( |
| 361 | ! |
args$x, args$y, args$color_by, args$size_by, args$row_facet, args$col_facet |
| 362 |
) |
|
| 363 | ||
| 364 | ! |
tagList( |
| 365 | ! |
teal.widgets::standard_layout( |
| 366 | ! |
output = teal.widgets::white_small_well( |
| 367 | ! |
teal.widgets::plot_with_settings_ui(id = ns("scatter_plot")),
|
| 368 | ! |
tags$br(), |
| 369 | ! |
tags$h1(tags$strong("Selected points:"), style = "font-size: 150%;"),
|
| 370 | ! |
teal.widgets::get_dt_rows(ns("data_table"), ns("data_table_rows")),
|
| 371 | ! |
DT::dataTableOutput(ns("data_table"), width = "100%")
|
| 372 |
), |
|
| 373 | ! |
encoding = tags$div( |
| 374 |
### Reporter |
|
| 375 | ! |
teal.reporter::add_card_button_ui(ns("add_reporter"), label = "Add Report Card"),
|
| 376 | ! |
tags$br(), tags$br(), |
| 377 |
### |
|
| 378 | ! |
tags$label("Encodings", class = "text-primary"),
|
| 379 | ! |
teal.transform::datanames_input(args[c("x", "y", "color_by", "size_by", "row_facet", "col_facet")]),
|
| 380 | ! |
teal.transform::data_extract_ui( |
| 381 | ! |
id = ns("x"),
|
| 382 | ! |
label = "X variable", |
| 383 | ! |
data_extract_spec = args$x, |
| 384 | ! |
is_single_dataset = is_single_dataset_value |
| 385 |
), |
|
| 386 | ! |
checkboxInput(ns("log_x"), "Use log transformation", value = FALSE),
|
| 387 | ! |
conditionalPanel( |
| 388 | ! |
condition = paste0("input['", ns("log_x"), "'] == true"),
|
| 389 | ! |
radioButtons( |
| 390 | ! |
ns("log_x_base"),
|
| 391 | ! |
label = NULL, |
| 392 | ! |
inline = TRUE, |
| 393 | ! |
choices = c("Natural" = "log", "Base 10" = "log10", "Base 2" = "log2")
|
| 394 |
) |
|
| 395 |
), |
|
| 396 | ! |
teal.transform::data_extract_ui( |
| 397 | ! |
id = ns("y"),
|
| 398 | ! |
label = "Y variable", |
| 399 | ! |
data_extract_spec = args$y, |
| 400 | ! |
is_single_dataset = is_single_dataset_value |
| 401 |
), |
|
| 402 | ! |
checkboxInput(ns("log_y"), "Use log transformation", value = FALSE),
|
| 403 | ! |
conditionalPanel( |
| 404 | ! |
condition = paste0("input['", ns("log_y"), "'] == true"),
|
| 405 | ! |
radioButtons( |
| 406 | ! |
ns("log_y_base"),
|
| 407 | ! |
label = NULL, |
| 408 | ! |
inline = TRUE, |
| 409 | ! |
choices = c("Natural" = "log", "Base 10" = "log10", "Base 2" = "log2")
|
| 410 |
) |
|
| 411 |
), |
|
| 412 | ! |
if (!is.null(args$color_by)) {
|
| 413 | ! |
teal.transform::data_extract_ui( |
| 414 | ! |
id = ns("color_by"),
|
| 415 | ! |
label = "Color by variable", |
| 416 | ! |
data_extract_spec = args$color_by, |
| 417 | ! |
is_single_dataset = is_single_dataset_value |
| 418 |
) |
|
| 419 |
}, |
|
| 420 | ! |
if (!is.null(args$size_by)) {
|
| 421 | ! |
teal.transform::data_extract_ui( |
| 422 | ! |
id = ns("size_by"),
|
| 423 | ! |
label = "Size by variable", |
| 424 | ! |
data_extract_spec = args$size_by, |
| 425 | ! |
is_single_dataset = is_single_dataset_value |
| 426 |
) |
|
| 427 |
}, |
|
| 428 | ! |
if (!is.null(args$row_facet)) {
|
| 429 | ! |
teal.transform::data_extract_ui( |
| 430 | ! |
id = ns("row_facet"),
|
| 431 | ! |
label = "Row facetting", |
| 432 | ! |
data_extract_spec = args$row_facet, |
| 433 | ! |
is_single_dataset = is_single_dataset_value |
| 434 |
) |
|
| 435 |
}, |
|
| 436 | ! |
if (!is.null(args$col_facet)) {
|
| 437 | ! |
teal.transform::data_extract_ui( |
| 438 | ! |
id = ns("col_facet"),
|
| 439 | ! |
label = "Column facetting", |
| 440 | ! |
data_extract_spec = args$col_facet, |
| 441 | ! |
is_single_dataset = is_single_dataset_value |
| 442 |
) |
|
| 443 |
}, |
|
| 444 | ! |
ui_decorate_teal_data(ns("decorator"), decorators = select_decorators(args$decorators, "plot")),
|
| 445 | ! |
bslib::accordion( |
| 446 | ! |
open = TRUE, |
| 447 | ! |
bslib::accordion_panel( |
| 448 | ! |
title = "Plot settings", |
| 449 | ! |
teal.widgets::optionalSliderInputValMinMax(ns("alpha"), "Opacity:", args$alpha, ticks = FALSE),
|
| 450 | ! |
teal.widgets::optionalSelectInput( |
| 451 | ! |
inputId = ns("shape"),
|
| 452 | ! |
label = "Points shape:", |
| 453 | ! |
choices = args$shape, |
| 454 | ! |
selected = args$shape[1], |
| 455 | ! |
multiple = FALSE |
| 456 |
), |
|
| 457 | ! |
colourpicker::colourInput(ns("color"), "Points color:", "black"),
|
| 458 | ! |
teal.widgets::optionalSliderInputValMinMax(ns("size"), "Points size:", args$size, ticks = FALSE, step = .1),
|
| 459 | ! |
checkboxInput(ns("rotate_xaxis_labels"), "Rotate X axis labels", value = args$rotate_xaxis_labels),
|
| 460 | ! |
checkboxInput(ns("add_density"), "Add marginal density", value = FALSE),
|
| 461 | ! |
checkboxInput(ns("rug_plot"), "Include rug plot", value = FALSE),
|
| 462 | ! |
checkboxInput(ns("show_count"), "Show N (number of observations)", value = FALSE),
|
| 463 | ! |
shinyjs::hidden(helpText(id = ns("line_msg"), "Trendline needs numeric X and Y variables")),
|
| 464 | ! |
teal.widgets::optionalSelectInput(ns("smoothing_degree"), "Smoothing degree", seq_len(args$max_deg)),
|
| 465 | ! |
shinyjs::hidden(teal.widgets::optionalSelectInput(ns("color_sub"), label = "", multiple = TRUE)),
|
| 466 | ! |
teal.widgets::optionalSliderInputValMinMax(ns("ci"), "Confidence", c(.95, .8, .99), ticks = FALSE),
|
| 467 | ! |
shinyjs::hidden(checkboxInput(ns("show_form"), "Show formula", value = TRUE)),
|
| 468 | ! |
shinyjs::hidden(checkboxInput(ns("show_r2"), "Show adj-R Squared", value = TRUE)),
|
| 469 | ! |
uiOutput(ns("num_na_removed")),
|
| 470 | ! |
tags$div( |
| 471 | ! |
id = ns("label_pos"),
|
| 472 | ! |
tags$div(tags$strong("Stats position")),
|
| 473 | ! |
tags$div(style = "display: inline-block; width: 70%;", helpText("Left")),
|
| 474 | ! |
tags$div( |
| 475 | ! |
style = "display: inline-block; width: 70%;", |
| 476 | ! |
teal.widgets::optionalSliderInput( |
| 477 | ! |
ns("pos"),
|
| 478 | ! |
label = NULL, |
| 479 | ! |
min = 0, max = 1, value = .99, ticks = FALSE, step = .01 |
| 480 |
) |
|
| 481 |
), |
|
| 482 | ! |
tags$div(style = "display: inline-block; width: 10%;", helpText("Right"))
|
| 483 |
), |
|
| 484 | ! |
teal.widgets::optionalSliderInput( |
| 485 | ! |
ns("label_size"), "Stats font size",
|
| 486 | ! |
min = 3, max = 10, value = 5, ticks = FALSE, step = .1 |
| 487 |
), |
|
| 488 | ! |
if (!is.null(args$row_facet) || !is.null(args$col_facet)) {
|
| 489 | ! |
checkboxInput(ns("free_scales"), "Free scales", value = FALSE)
|
| 490 |
}, |
|
| 491 | ! |
selectInput( |
| 492 | ! |
inputId = ns("ggtheme"),
|
| 493 | ! |
label = "Theme (by ggplot):", |
| 494 | ! |
choices = ggplot_themes, |
| 495 | ! |
selected = args$ggtheme, |
| 496 | ! |
multiple = FALSE |
| 497 |
) |
|
| 498 |
) |
|
| 499 |
) |
|
| 500 |
), |
|
| 501 | ! |
forms = tagList( |
| 502 | ! |
teal.widgets::verbatim_popup_ui(ns("rcode"), "Show R code")
|
| 503 |
), |
|
| 504 | ! |
pre_output = args$pre_output, |
| 505 | ! |
post_output = args$post_output |
| 506 |
) |
|
| 507 |
) |
|
| 508 |
} |
|
| 509 | ||
| 510 |
# Server function for the scatterplot module |
|
| 511 |
srv_g_scatterplot <- function(id, |
|
| 512 |
data, |
|
| 513 |
reporter, |
|
| 514 |
filter_panel_api, |
|
| 515 |
x, |
|
| 516 |
y, |
|
| 517 |
color_by, |
|
| 518 |
size_by, |
|
| 519 |
row_facet, |
|
| 520 |
col_facet, |
|
| 521 |
plot_height, |
|
| 522 |
plot_width, |
|
| 523 |
table_dec, |
|
| 524 |
ggplot2_args, |
|
| 525 |
decorators) {
|
|
| 526 | ! |
with_reporter <- !missing(reporter) && inherits(reporter, "Reporter") |
| 527 | ! |
with_filter <- !missing(filter_panel_api) && inherits(filter_panel_api, "FilterPanelAPI") |
| 528 | ! |
checkmate::assert_class(data, "reactive") |
| 529 | ! |
checkmate::assert_class(isolate(data()), "teal_data") |
| 530 | ! |
moduleServer(id, function(input, output, session) {
|
| 531 | ! |
teal.logger::log_shiny_input_changes(input, namespace = "teal.modules.general") |
| 532 | ||
| 533 | ! |
data_extract <- list( |
| 534 | ! |
x = x, |
| 535 | ! |
y = y, |
| 536 | ! |
color_by = color_by, |
| 537 | ! |
size_by = size_by, |
| 538 | ! |
row_facet = row_facet, |
| 539 | ! |
col_facet = col_facet |
| 540 |
) |
|
| 541 | ||
| 542 | ! |
rule_diff <- function(other) {
|
| 543 | ! |
function(value) {
|
| 544 | ! |
othervalue <- selector_list()[[other]]()[["select"]] |
| 545 | ! |
if (!is.null(othervalue)) {
|
| 546 | ! |
if (identical(value, othervalue)) {
|
| 547 | ! |
"Row and column facetting variables must be different." |
| 548 |
} |
|
| 549 |
} |
|
| 550 |
} |
|
| 551 |
} |
|
| 552 | ||
| 553 | ! |
selector_list <- teal.transform::data_extract_multiple_srv( |
| 554 | ! |
data_extract = data_extract, |
| 555 | ! |
datasets = data, |
| 556 | ! |
select_validation_rule = list( |
| 557 | ! |
x = ~ if (length(.) != 1) "Please select exactly one x var.", |
| 558 | ! |
y = ~ if (length(.) != 1) "Please select exactly one y var.", |
| 559 | ! |
color_by = ~ if (length(.) > 1) "There cannot be more than 1 color variable.", |
| 560 | ! |
size_by = ~ if (length(.) > 1) "There cannot be more than 1 size variable.", |
| 561 | ! |
row_facet = shinyvalidate::compose_rules( |
| 562 | ! |
shinyvalidate::sv_optional(), |
| 563 | ! |
rule_diff("col_facet")
|
| 564 |
), |
|
| 565 | ! |
col_facet = shinyvalidate::compose_rules( |
| 566 | ! |
shinyvalidate::sv_optional(), |
| 567 | ! |
rule_diff("row_facet")
|
| 568 |
) |
|
| 569 |
) |
|
| 570 |
) |
|
| 571 | ||
| 572 | ! |
iv_r <- reactive({
|
| 573 | ! |
iv_facet <- shinyvalidate::InputValidator$new() |
| 574 | ! |
iv <- shinyvalidate::InputValidator$new() |
| 575 | ! |
teal.transform::compose_and_enable_validators(iv, selector_list) |
| 576 |
}) |
|
| 577 | ! |
iv_facet <- shinyvalidate::InputValidator$new() |
| 578 | ! |
iv_facet$add_rule("add_density", ~ if (
|
| 579 | ! |
isTRUE(.) && |
| 580 |
( |
|
| 581 | ! |
length(selector_list()$row_facet()$select) > 0L || |
| 582 | ! |
length(selector_list()$col_facet()$select) > 0L |
| 583 |
) |
|
| 584 |
) {
|
|
| 585 | ! |
"Cannot add marginal density when Row or Column facetting has been selected" |
| 586 |
}) |
|
| 587 | ! |
iv_facet$enable() |
| 588 | ||
| 589 | ! |
anl_merged_input <- teal.transform::merge_expression_srv( |
| 590 | ! |
selector_list = selector_list, |
| 591 | ! |
datasets = data, |
| 592 | ! |
merge_function = "dplyr::inner_join" |
| 593 |
) |
|
| 594 | ! |
qenv <- reactive( |
| 595 | ! |
teal.code::eval_code(data(), 'library("ggplot2");library("dplyr")') # nolint quotes
|
| 596 |
) |
|
| 597 | ||
| 598 | ! |
anl_merged_q <- reactive({
|
| 599 | ! |
req(anl_merged_input()) |
| 600 | ! |
qenv() %>% |
| 601 | ! |
teal.code::eval_code(as.expression(anl_merged_input()$expr)) %>% |
| 602 | ! |
teal.code::eval_code(quote(ANL)) # used to display table when running show-r-code code |
| 603 |
}) |
|
| 604 | ||
| 605 | ! |
merged <- list( |
| 606 | ! |
anl_input_r = anl_merged_input, |
| 607 | ! |
anl_q_r = anl_merged_q |
| 608 |
) |
|
| 609 | ||
| 610 | ! |
trend_line_is_applicable <- reactive({
|
| 611 | ! |
ANL <- merged$anl_q_r()[["ANL"]] |
| 612 | ! |
x_var <- as.vector(merged$anl_input_r()$columns_source$x) |
| 613 | ! |
y_var <- as.vector(merged$anl_input_r()$columns_source$y) |
| 614 | ! |
length(x_var) > 0 && length(y_var) > 0 && is.numeric(ANL[[x_var]]) && is.numeric(ANL[[y_var]]) |
| 615 |
}) |
|
| 616 | ||
| 617 | ! |
add_trend_line <- reactive({
|
| 618 | ! |
smoothing_degree <- as.integer(input$smoothing_degree) |
| 619 | ! |
trend_line_is_applicable() && length(smoothing_degree) > 0 |
| 620 |
}) |
|
| 621 | ||
| 622 | ! |
if (!is.null(color_by)) {
|
| 623 | ! |
observeEvent( |
| 624 | ! |
eventExpr = merged$anl_input_r()$columns_source$color_by, |
| 625 | ! |
handlerExpr = {
|
| 626 | ! |
color_by_var <- as.vector(merged$anl_input_r()$columns_source$color_by) |
| 627 | ! |
if (length(color_by_var) > 0) {
|
| 628 | ! |
shinyjs::hide("color")
|
| 629 |
} else {
|
|
| 630 | ! |
shinyjs::show("color")
|
| 631 |
} |
|
| 632 |
} |
|
| 633 |
) |
|
| 634 |
} |
|
| 635 | ||
| 636 | ! |
output$num_na_removed <- renderUI({
|
| 637 | ! |
if (add_trend_line()) {
|
| 638 | ! |
ANL <- merged$anl_q_r()[["ANL"]] |
| 639 | ! |
x_var <- as.vector(merged$anl_input_r()$columns_source$x) |
| 640 | ! |
y_var <- as.vector(merged$anl_input_r()$columns_source$y) |
| 641 | ! |
if ((num_total_na <- nrow(ANL) - nrow(stats::na.omit(ANL[, c(x_var, y_var)]))) > 0) {
|
| 642 | ! |
tags$div(paste(num_total_na, "row(s) with missing values were removed"), tags$hr()) |
| 643 |
} |
|
| 644 |
} |
|
| 645 |
}) |
|
| 646 | ||
| 647 | ! |
observeEvent( |
| 648 | ! |
eventExpr = merged$anl_input_r()$columns_source[c("col_facet", "row_facet")],
|
| 649 | ! |
handlerExpr = {
|
| 650 | ! |
if ( |
| 651 | ! |
length(merged$anl_input_r()$columns_source$col_facet) == 0 && |
| 652 | ! |
length(merged$anl_input_r()$columns_source$row_facet) == 0 |
| 653 |
) {
|
|
| 654 | ! |
shinyjs::hide("free_scales")
|
| 655 |
} else {
|
|
| 656 | ! |
shinyjs::show("free_scales")
|
| 657 |
} |
|
| 658 |
} |
|
| 659 |
) |
|
| 660 | ||
| 661 | ! |
output_q <- reactive({
|
| 662 | ! |
teal::validate_inputs(iv_r(), iv_facet) |
| 663 | ||
| 664 | ! |
ANL <- merged$anl_q_r()[["ANL"]] |
| 665 | ||
| 666 | ! |
x_var <- as.vector(merged$anl_input_r()$columns_source$x) |
| 667 | ! |
y_var <- as.vector(merged$anl_input_r()$columns_source$y) |
| 668 | ! |
color_by_var <- as.vector(merged$anl_input_r()$columns_source$color_by) |
| 669 | ! |
size_by_var <- as.vector(merged$anl_input_r()$columns_source$size_by) |
| 670 | ! |
row_facet_name <- if (length(merged$anl_input_r()$columns_source$row_facet) == 0) {
|
| 671 | ! |
character(0) |
| 672 |
} else {
|
|
| 673 | ! |
as.vector(merged$anl_input_r()$columns_source$row_facet) |
| 674 |
} |
|
| 675 | ! |
col_facet_name <- if (length(merged$anl_input_r()$columns_source$col_facet) == 0) {
|
| 676 | ! |
character(0) |
| 677 |
} else {
|
|
| 678 | ! |
as.vector(merged$anl_input_r()$columns_source$col_facet) |
| 679 |
} |
|
| 680 | ! |
alpha <- input$alpha |
| 681 | ! |
size <- input$size |
| 682 | ! |
rotate_xaxis_labels <- input$rotate_xaxis_labels |
| 683 | ! |
add_density <- input$add_density |
| 684 | ! |
ggtheme <- input$ggtheme |
| 685 | ! |
rug_plot <- input$rug_plot |
| 686 | ! |
color <- input$color |
| 687 | ! |
shape <- `if`(is.null(input$shape) || identical(input$shape, ""), "circle", input$shape) |
| 688 | ! |
smoothing_degree <- as.integer(input$smoothing_degree) |
| 689 | ! |
ci <- input$ci |
| 690 | ||
| 691 | ! |
log_x <- input$log_x |
| 692 | ! |
log_y <- input$log_y |
| 693 | ||
| 694 | ! |
validate(need( |
| 695 | ! |
length(row_facet_name) == 0 || inherits(ANL[[row_facet_name]], c("character", "factor", "Date", "integer")),
|
| 696 | ! |
"`Row facetting` variable must be of class `character`, `factor`, `Date`, or `integer`" |
| 697 |
)) |
|
| 698 | ! |
validate(need( |
| 699 | ! |
length(col_facet_name) == 0 || inherits(ANL[[col_facet_name]], c("character", "factor", "Date", "integer")),
|
| 700 | ! |
"`Column facetting` variable must be of class `character`, `factor`, `Date`, or `integer`" |
| 701 |
)) |
|
| 702 | ||
| 703 | ! |
if (add_density && length(color_by_var) > 0) {
|
| 704 | ! |
validate(need( |
| 705 | ! |
!is.numeric(ANL[[color_by_var]]), |
| 706 | ! |
"Marginal plots cannot be produced when the points are colored by numeric variables. |
| 707 | ! |
\n Uncheck the 'Add marginal density' checkbox to display the plot." |
| 708 |
)) |
|
| 709 | ! |
validate(need( |
| 710 |
!( |
|
| 711 | ! |
inherits(ANL[[color_by_var]], "Date") || |
| 712 | ! |
inherits(ANL[[color_by_var]], "POSIXct") || |
| 713 | ! |
inherits(ANL[[color_by_var]], "POSIXlt") |
| 714 |
), |
|
| 715 | ! |
"Marginal plots cannot be produced when the points are colored by Date or POSIX variables. |
| 716 | ! |
\n Uncheck the 'Add marginal density' checkbox to display the plot." |
| 717 |
)) |
|
| 718 |
} |
|
| 719 | ||
| 720 | ! |
teal::validate_has_data(ANL[, c(x_var, y_var)], 1, complete = TRUE, allow_inf = FALSE) |
| 721 | ||
| 722 | ! |
if (log_x) {
|
| 723 | ! |
validate( |
| 724 | ! |
need( |
| 725 | ! |
is.numeric(ANL[[x_var]]) && all( |
| 726 | ! |
ANL[[x_var]] > 0 | is.na(ANL[[x_var]]) |
| 727 |
), |
|
| 728 | ! |
"X variable can only be log transformed if variable is numeric and all values are positive." |
| 729 |
) |
|
| 730 |
) |
|
| 731 |
} |
|
| 732 | ! |
if (log_y) {
|
| 733 | ! |
validate( |
| 734 | ! |
need( |
| 735 | ! |
is.numeric(ANL[[y_var]]) && all( |
| 736 | ! |
ANL[[y_var]] > 0 | is.na(ANL[[y_var]]) |
| 737 |
), |
|
| 738 | ! |
"Y variable can only be log transformed if variable is numeric and all values are positive." |
| 739 |
) |
|
| 740 |
) |
|
| 741 |
} |
|
| 742 | ||
| 743 | ! |
facet_cl <- facet_ggplot_call( |
| 744 | ! |
row_facet_name, |
| 745 | ! |
col_facet_name, |
| 746 | ! |
free_x_scales = isTRUE(input$free_scales), |
| 747 | ! |
free_y_scales = isTRUE(input$free_scales) |
| 748 |
) |
|
| 749 | ||
| 750 | ! |
point_sizes <- if (length(size_by_var) > 0) {
|
| 751 | ! |
validate(need(is.numeric(ANL[[size_by_var]]), "Variable to size by must be numeric")) |
| 752 | ! |
substitute( |
| 753 | ! |
expr = size * ANL[[size_by_var]] / max(ANL[[size_by_var]], na.rm = TRUE), |
| 754 | ! |
env = list(size = size, size_by_var = size_by_var) |
| 755 |
) |
|
| 756 |
} else {
|
|
| 757 | ! |
size |
| 758 |
} |
|
| 759 | ||
| 760 | ! |
plot_q <- merged$anl_q_r() |
| 761 | ||
| 762 | ! |
if (log_x) {
|
| 763 | ! |
log_x_fn <- input$log_x_base |
| 764 | ! |
plot_q <- teal.code::eval_code( |
| 765 | ! |
object = plot_q, |
| 766 | ! |
code = substitute( |
| 767 | ! |
expr = ANL[, log_x_var] <- log_x_fn(ANL[, x_var]), |
| 768 | ! |
env = list( |
| 769 | ! |
x_var = x_var, |
| 770 | ! |
log_x_fn = as.name(log_x_fn), |
| 771 | ! |
log_x_var = paste0(log_x_fn, "_", x_var) |
| 772 |
) |
|
| 773 |
) |
|
| 774 |
) |
|
| 775 |
} |
|
| 776 | ||
| 777 | ! |
if (log_y) {
|
| 778 | ! |
log_y_fn <- input$log_y_base |
| 779 | ! |
plot_q <- teal.code::eval_code( |
| 780 | ! |
object = plot_q, |
| 781 | ! |
code = substitute( |
| 782 | ! |
expr = ANL[, log_y_var] <- log_y_fn(ANL[, y_var]), |
| 783 | ! |
env = list( |
| 784 | ! |
y_var = y_var, |
| 785 | ! |
log_y_fn = as.name(log_y_fn), |
| 786 | ! |
log_y_var = paste0(log_y_fn, "_", y_var) |
| 787 |
) |
|
| 788 |
) |
|
| 789 |
) |
|
| 790 |
} |
|
| 791 | ||
| 792 | ! |
pre_pro_anl <- if (input$show_count) {
|
| 793 | ! |
paste0( |
| 794 | ! |
"ANL %>% dplyr::group_by(",
|
| 795 | ! |
paste( |
| 796 | ! |
c( |
| 797 | ! |
if (length(color_by_var) > 0 && inherits(ANL[[color_by_var]], c("factor", "character"))) color_by_var,
|
| 798 | ! |
row_facet_name, |
| 799 | ! |
col_facet_name |
| 800 |
), |
|
| 801 | ! |
collapse = ", " |
| 802 |
), |
|
| 803 | ! |
") %>% dplyr::mutate(n = dplyr::n()) %>% dplyr::ungroup()" |
| 804 |
) |
|
| 805 |
} else {
|
|
| 806 | ! |
"ANL" |
| 807 |
} |
|
| 808 | ||
| 809 | ! |
plot_call <- substitute(expr = pre_pro_anl %>% ggplot2::ggplot(), env = list(pre_pro_anl = str2lang(pre_pro_anl))) |
| 810 | ||
| 811 | ! |
plot_call <- if (length(color_by_var) == 0) {
|
| 812 | ! |
substitute( |
| 813 | ! |
expr = plot_call + |
| 814 | ! |
ggplot2::aes(x = x_name, y = y_name) + |
| 815 | ! |
ggplot2::geom_point(alpha = alpha_value, size = point_sizes, shape = shape_value, color = color_value), |
| 816 | ! |
env = list( |
| 817 | ! |
plot_call = plot_call, |
| 818 | ! |
x_name = if (log_x) as.name(paste0(log_x_fn, "_", x_var)) else as.name(x_var), |
| 819 | ! |
y_name = if (log_y) as.name(paste0(log_y_fn, "_", y_var)) else as.name(y_var), |
| 820 | ! |
alpha_value = alpha, |
| 821 | ! |
point_sizes = point_sizes, |
| 822 | ! |
shape_value = shape, |
| 823 | ! |
color_value = color |
| 824 |
) |
|
| 825 |
) |
|
| 826 |
} else {
|
|
| 827 | ! |
substitute( |
| 828 | ! |
expr = plot_call + |
| 829 | ! |
ggplot2::aes(x = x_name, y = y_name, color = color_by_var_name) + |
| 830 | ! |
ggplot2::geom_point(alpha = alpha_value, size = point_sizes, shape = shape_value), |
| 831 | ! |
env = list( |
| 832 | ! |
plot_call = plot_call, |
| 833 | ! |
x_name = if (log_x) as.name(paste0(log_x_fn, "_", x_var)) else as.name(x_var), |
| 834 | ! |
y_name = if (log_y) as.name(paste0(log_y_fn, "_", y_var)) else as.name(y_var), |
| 835 | ! |
color_by_var_name = as.name(color_by_var), |
| 836 | ! |
alpha_value = alpha, |
| 837 | ! |
point_sizes = point_sizes, |
| 838 | ! |
shape_value = shape |
| 839 |
) |
|
| 840 |
) |
|
| 841 |
} |
|
| 842 | ||
| 843 | ! |
if (rug_plot) plot_call <- substitute(expr = plot_call + geom_rug(), env = list(plot_call = plot_call)) |
| 844 | ||
| 845 | ! |
plot_label_generator <- function(rhs_formula = quote(y ~ 1), |
| 846 | ! |
show_form = input$show_form, |
| 847 | ! |
show_r2 = input$show_r2, |
| 848 | ! |
show_count = input$show_count, |
| 849 | ! |
pos = input$pos, |
| 850 | ! |
label_size = input$label_size) {
|
| 851 | ! |
stopifnot(sum(show_form, show_r2, show_count) >= 1) |
| 852 | ! |
aes_label <- paste0( |
| 853 | ! |
"aes(",
|
| 854 | ! |
if (show_count) "n = n, ", |
| 855 | ! |
"label = ", |
| 856 | ! |
if (sum(show_form, show_r2, show_count) > 1) "paste(",
|
| 857 | ! |
paste( |
| 858 | ! |
c( |
| 859 | ! |
if (show_form) "stat(eq.label)", |
| 860 | ! |
if (show_r2) "stat(adj.rr.label)", |
| 861 | ! |
if (show_count) "paste('N ~`=`~', n)"
|
| 862 |
), |
|
| 863 | ! |
collapse = ", " |
| 864 |
), |
|
| 865 | ! |
if (sum(show_form, show_r2, show_count) > 1) ", sep = '*\", \"*'))" else ")" |
| 866 |
) |
|
| 867 | ! |
label_geom <- substitute( |
| 868 | ! |
expr = ggpmisc::stat_poly_eq( |
| 869 | ! |
mapping = aes_label, |
| 870 | ! |
formula = rhs_formula, |
| 871 | ! |
parse = TRUE, |
| 872 | ! |
label.x = pos, |
| 873 | ! |
size = label_size |
| 874 |
), |
|
| 875 | ! |
env = list( |
| 876 | ! |
rhs_formula = rhs_formula, |
| 877 | ! |
pos = pos, |
| 878 | ! |
aes_label = str2lang(aes_label), |
| 879 | ! |
label_size = label_size |
| 880 |
) |
|
| 881 |
) |
|
| 882 | ! |
substitute( |
| 883 | ! |
expr = plot_call + label_geom, |
| 884 | ! |
env = list( |
| 885 | ! |
plot_call = plot_call, |
| 886 | ! |
label_geom = label_geom |
| 887 |
) |
|
| 888 |
) |
|
| 889 |
} |
|
| 890 | ||
| 891 | ! |
if (trend_line_is_applicable()) {
|
| 892 | ! |
shinyjs::hide("line_msg")
|
| 893 | ! |
shinyjs::show("smoothing_degree")
|
| 894 | ! |
if (!add_trend_line()) {
|
| 895 | ! |
shinyjs::hide("ci")
|
| 896 | ! |
shinyjs::hide("color_sub")
|
| 897 | ! |
shinyjs::hide("show_form")
|
| 898 | ! |
shinyjs::hide("show_r2")
|
| 899 | ! |
if (input$show_count) {
|
| 900 | ! |
plot_call <- plot_label_generator(show_form = FALSE, show_r2 = FALSE) |
| 901 | ! |
shinyjs::show("label_pos")
|
| 902 | ! |
shinyjs::show("label_size")
|
| 903 |
} else {
|
|
| 904 | ! |
shinyjs::hide("label_pos")
|
| 905 | ! |
shinyjs::hide("label_size")
|
| 906 |
} |
|
| 907 |
} else {
|
|
| 908 | ! |
shinyjs::show("ci")
|
| 909 | ! |
shinyjs::show("show_form")
|
| 910 | ! |
shinyjs::show("show_r2")
|
| 911 | ! |
if (nrow(ANL) - nrow(stats::na.omit(ANL[, c(x_var, y_var)])) > 0) {
|
| 912 | ! |
plot_q <- teal.code::eval_code( |
| 913 | ! |
plot_q, |
| 914 | ! |
substitute( |
| 915 | ! |
expr = ANL <- dplyr::filter(ANL, !is.na(x_var) & !is.na(y_var)), |
| 916 | ! |
env = list(x_var = as.name(x_var), y_var = as.name(y_var)) |
| 917 |
) |
|
| 918 |
) |
|
| 919 |
} |
|
| 920 | ! |
rhs_formula <- substitute( |
| 921 | ! |
expr = y ~ poly(x, smoothing_degree, raw = TRUE), |
| 922 | ! |
env = list(smoothing_degree = smoothing_degree) |
| 923 |
) |
|
| 924 | ! |
if (input$show_form || input$show_r2 || input$show_count) {
|
| 925 | ! |
plot_call <- plot_label_generator(rhs_formula = rhs_formula) |
| 926 | ! |
shinyjs::show("label_pos")
|
| 927 | ! |
shinyjs::show("label_size")
|
| 928 |
} else {
|
|
| 929 | ! |
shinyjs::hide("label_pos")
|
| 930 | ! |
shinyjs::hide("label_size")
|
| 931 |
} |
|
| 932 | ! |
plot_call <- substitute( |
| 933 | ! |
expr = plot_call + ggplot2::geom_smooth(formula = rhs_formula, se = TRUE, level = ci, method = "lm"), |
| 934 | ! |
env = list(plot_call = plot_call, rhs_formula = rhs_formula, ci = ci) |
| 935 |
) |
|
| 936 |
} |
|
| 937 |
} else {
|
|
| 938 | ! |
shinyjs::hide("smoothing_degree")
|
| 939 | ! |
shinyjs::hide("ci")
|
| 940 | ! |
shinyjs::hide("color_sub")
|
| 941 | ! |
shinyjs::hide("show_form")
|
| 942 | ! |
shinyjs::hide("show_r2")
|
| 943 | ! |
if (input$show_count) {
|
| 944 | ! |
plot_call <- plot_label_generator(show_form = FALSE, show_r2 = FALSE) |
| 945 | ! |
shinyjs::show("label_pos")
|
| 946 | ! |
shinyjs::show("label_size")
|
| 947 |
} else {
|
|
| 948 | ! |
shinyjs::hide("label_pos")
|
| 949 | ! |
shinyjs::hide("label_size")
|
| 950 |
} |
|
| 951 | ! |
shinyjs::show("line_msg")
|
| 952 |
} |
|
| 953 | ||
| 954 | ! |
if (!is.null(facet_cl)) {
|
| 955 | ! |
plot_call <- substitute(expr = plot_call + facet_cl, env = list(plot_call = plot_call, facet_cl = facet_cl)) |
| 956 |
} |
|
| 957 | ||
| 958 | ! |
y_label <- varname_w_label( |
| 959 | ! |
y_var, |
| 960 | ! |
ANL, |
| 961 | ! |
prefix = if (log_y) paste(log_y_fn, "(") else NULL,
|
| 962 | ! |
suffix = if (log_y) ")" else NULL |
| 963 |
) |
|
| 964 | ! |
x_label <- varname_w_label( |
| 965 | ! |
x_var, |
| 966 | ! |
ANL, |
| 967 | ! |
prefix = if (log_x) paste(log_x_fn, "(") else NULL,
|
| 968 | ! |
suffix = if (log_x) ")" else NULL |
| 969 |
) |
|
| 970 | ||
| 971 | ! |
dev_ggplot2_args <- teal.widgets::ggplot2_args( |
| 972 | ! |
labs = list(y = y_label, x = x_label), |
| 973 | ! |
theme = list(legend.position = "bottom") |
| 974 |
) |
|
| 975 | ||
| 976 | ! |
if (rotate_xaxis_labels) {
|
| 977 | ! |
dev_ggplot2_args$theme[["axis.text.x"]] <- quote(ggplot2::element_text(angle = 45, hjust = 1)) |
| 978 |
} |
|
| 979 | ||
| 980 | ! |
all_ggplot2_args <- teal.widgets::resolve_ggplot2_args( |
| 981 | ! |
user_plot = ggplot2_args, |
| 982 | ! |
module_plot = dev_ggplot2_args |
| 983 |
) |
|
| 984 | ||
| 985 | ! |
parsed_ggplot2_args <- teal.widgets::parse_ggplot2_args(all_ggplot2_args, ggtheme = ggtheme) |
| 986 | ||
| 987 | ||
| 988 | ! |
if (add_density) {
|
| 989 | ! |
plot_call <- substitute( |
| 990 | ! |
expr = ggExtra::ggMarginal( |
| 991 | ! |
plot_call + labs + ggthemes + themes, |
| 992 | ! |
type = "density", |
| 993 | ! |
groupColour = group_colour |
| 994 |
), |
|
| 995 | ! |
env = list( |
| 996 | ! |
plot_call = plot_call, |
| 997 | ! |
group_colour = if (length(color_by_var) > 0) TRUE else FALSE, |
| 998 | ! |
labs = parsed_ggplot2_args$labs, |
| 999 | ! |
ggthemes = parsed_ggplot2_args$ggtheme, |
| 1000 | ! |
themes = parsed_ggplot2_args$theme |
| 1001 |
) |
|
| 1002 |
) |
|
| 1003 |
} else {
|
|
| 1004 | ! |
plot_call <- substitute( |
| 1005 | ! |
expr = plot_call + |
| 1006 | ! |
labs + |
| 1007 | ! |
ggthemes + |
| 1008 | ! |
themes, |
| 1009 | ! |
env = list( |
| 1010 | ! |
plot_call = plot_call, |
| 1011 | ! |
labs = parsed_ggplot2_args$labs, |
| 1012 | ! |
ggthemes = parsed_ggplot2_args$ggtheme, |
| 1013 | ! |
themes = parsed_ggplot2_args$theme |
| 1014 |
) |
|
| 1015 |
) |
|
| 1016 |
} |
|
| 1017 | ||
| 1018 | ! |
plot_call <- substitute(expr = plot <- plot_call, env = list(plot_call = plot_call)) |
| 1019 | ||
| 1020 | ! |
teal.code::eval_code(plot_q, plot_call) |
| 1021 |
}) |
|
| 1022 | ||
| 1023 | ! |
decorated_output_plot_q <- srv_decorate_teal_data( |
| 1024 | ! |
id = "decorator", |
| 1025 | ! |
data = output_q, |
| 1026 | ! |
decorators = select_decorators(decorators, "plot"), |
| 1027 | ! |
expr = print(plot) |
| 1028 |
) |
|
| 1029 | ||
| 1030 | ! |
plot_r <- reactive(req(decorated_output_plot_q())[["plot"]]) |
| 1031 | ||
| 1032 |
# Insert the plot into a plot_with_settings module from teal.widgets |
|
| 1033 | ! |
pws <- teal.widgets::plot_with_settings_srv( |
| 1034 | ! |
id = "scatter_plot", |
| 1035 | ! |
plot_r = plot_r, |
| 1036 | ! |
height = plot_height, |
| 1037 | ! |
width = plot_width, |
| 1038 | ! |
brushing = TRUE |
| 1039 |
) |
|
| 1040 | ||
| 1041 | ! |
output$data_table <- DT::renderDataTable({
|
| 1042 | ! |
plot_brush <- pws$brush() |
| 1043 | ||
| 1044 | ! |
if (!is.null(plot_brush)) {
|
| 1045 | ! |
validate(need(!input$add_density, "Brushing feature is currently not supported when plot has marginal density")) |
| 1046 |
} |
|
| 1047 | ||
| 1048 | ! |
merged_data <- isolate(output_q()[["ANL"]]) |
| 1049 | ||
| 1050 | ! |
brushed_df <- teal.widgets::clean_brushedPoints(merged_data, plot_brush) |
| 1051 | ! |
numeric_cols <- names(brushed_df)[ |
| 1052 | ! |
vapply(brushed_df, function(x) is.numeric(x) && !is.integer(x), FUN.VALUE = logical(1)) |
| 1053 |
] |
|
| 1054 | ||
| 1055 | ! |
if (length(numeric_cols) > 0) {
|
| 1056 | ! |
DT::formatRound( |
| 1057 | ! |
DT::datatable(brushed_df, |
| 1058 | ! |
rownames = FALSE, |
| 1059 | ! |
options = list(scrollX = TRUE, pageLength = input$data_table_rows) |
| 1060 |
), |
|
| 1061 | ! |
numeric_cols, |
| 1062 | ! |
table_dec |
| 1063 |
) |
|
| 1064 |
} else {
|
|
| 1065 | ! |
DT::datatable(brushed_df, rownames = FALSE, options = list(scrollX = TRUE, pageLength = input$data_table_rows)) |
| 1066 |
} |
|
| 1067 |
}) |
|
| 1068 | ||
| 1069 |
# Render R code. |
|
| 1070 | ! |
source_code_r <- reactive(teal.code::get_code(req(decorated_output_plot_q()))) |
| 1071 | ||
| 1072 | ! |
teal.widgets::verbatim_popup_srv( |
| 1073 | ! |
id = "rcode", |
| 1074 | ! |
verbatim_content = source_code_r, |
| 1075 | ! |
title = "R Code for scatterplot" |
| 1076 |
) |
|
| 1077 | ||
| 1078 |
### REPORTER |
|
| 1079 | ! |
if (with_reporter) {
|
| 1080 | ! |
card_fun <- function(comment, label) {
|
| 1081 | ! |
card <- teal::report_card_template( |
| 1082 | ! |
title = "Scatter Plot", |
| 1083 | ! |
label = label, |
| 1084 | ! |
with_filter = with_filter, |
| 1085 | ! |
filter_panel_api = filter_panel_api |
| 1086 |
) |
|
| 1087 | ! |
card$append_text("Plot", "header3")
|
| 1088 | ! |
card$append_plot(plot_r(), dim = pws$dim()) |
| 1089 | ! |
if (!comment == "") {
|
| 1090 | ! |
card$append_text("Comment", "header3")
|
| 1091 | ! |
card$append_text(comment) |
| 1092 |
} |
|
| 1093 | ! |
card$append_src(source_code_r()) |
| 1094 | ! |
card |
| 1095 |
} |
|
| 1096 | ! |
teal.reporter::add_card_button_srv("add_reporter", reporter = reporter, card_fun = card_fun)
|
| 1097 |
} |
|
| 1098 |
### |
|
| 1099 |
}) |
|
| 1100 |
} |
| 1 |
#' `teal` module: Univariate and bivariate visualizations |
|
| 2 |
#' |
|
| 3 |
#' Module enables the creation of univariate and bivariate plots, |
|
| 4 |
#' facilitating the exploration of data distributions and relationships between two variables. |
|
| 5 |
#' |
|
| 6 |
#' This is a general module to visualize 1 & 2 dimensional data. |
|
| 7 |
#' |
|
| 8 |
#' @note |
|
| 9 |
#' For more examples, please see the vignette "Using bivariate plot" via |
|
| 10 |
#' `vignette("using-bivariate-plot", package = "teal.modules.general")`.
|
|
| 11 |
#' |
|
| 12 |
#' @inheritParams teal::module |
|
| 13 |
#' @inheritParams shared_params |
|
| 14 |
#' @param x (`data_extract_spec` or `list` of multiple `data_extract_spec`) |
|
| 15 |
#' Variable names selected to plot along the x-axis by default. |
|
| 16 |
#' Can be numeric, factor or character. |
|
| 17 |
#' No empty selections are allowed. |
|
| 18 |
#' @param y (`data_extract_spec` or `list` of multiple `data_extract_spec`) |
|
| 19 |
#' Variable names selected to plot along the y-axis by default. |
|
| 20 |
#' Can be numeric, factor or character. |
|
| 21 |
#' @param use_density (`logical`) optional, indicates whether to plot density (`TRUE`) or frequency (`FALSE`). |
|
| 22 |
#' Defaults to frequency (`FALSE`). |
|
| 23 |
#' @param row_facet (`data_extract_spec` or `list` of multiple `data_extract_spec`) optional, |
|
| 24 |
#' specification of the data variable(s) to use for faceting rows. |
|
| 25 |
#' @param col_facet (`data_extract_spec` or `list` of multiple `data_extract_spec`) optional, |
|
| 26 |
#' specification of the data variable(s) to use for faceting columns. |
|
| 27 |
#' @param facet (`logical`) optional, specifies whether the facet encodings `ui` elements are toggled |
|
| 28 |
#' on and shown to the user by default. Defaults to `TRUE` if either `row_facet` or `column_facet` |
|
| 29 |
#' are supplied. |
|
| 30 |
#' @param color_settings (`logical`) Whether coloring, filling and size should be applied |
|
| 31 |
#' and `UI` tool offered to the user. |
|
| 32 |
#' @param color (`data_extract_spec` or `list` of multiple `data_extract_spec`) optional, |
|
| 33 |
#' specification of the data variable(s) selected for the outline color inside the coloring settings. |
|
| 34 |
#' It will be applied when `color_settings` is set to `TRUE`. |
|
| 35 |
#' @param fill (`data_extract_spec` or `list` of multiple `data_extract_spec`) optional, |
|
| 36 |
#' specification of the data variable(s) selected for the fill color inside the coloring settings. |
|
| 37 |
#' It will be applied when `color_settings` is set to `TRUE`. |
|
| 38 |
#' @param size (`data_extract_spec` or `list` of multiple `data_extract_spec`) optional, |
|
| 39 |
#' specification of the data variable(s) selected for the size of `geom_point` plots inside the coloring settings. |
|
| 40 |
#' It will be applied when `color_settings` is set to `TRUE`. |
|
| 41 |
#' @param free_x_scales (`logical`) optional, whether X scaling shall be changeable. |
|
| 42 |
#' Does not allow scaling to be changed by default (`FALSE`). |
|
| 43 |
#' @param free_y_scales (`logical`) optional, whether Y scaling shall be changeable. |
|
| 44 |
#' Does not allow scaling to be changed by default (`FALSE`). |
|
| 45 |
#' @param swap_axes (`logical`) optional, whether to swap X and Y axes. Defaults to `FALSE`. |
|
| 46 |
#' |
|
| 47 |
#' @inherit shared_params return |
|
| 48 |
#' |
|
| 49 |
#' @section Decorating Module: |
|
| 50 |
#' |
|
| 51 |
#' This module generates the following objects, which can be modified in place using decorators: |
|
| 52 |
#' - `plot` (`ggplot`) |
|
| 53 |
#' |
|
| 54 |
#' A Decorator is applied to the specific output using a named list of `teal_transform_module` objects. |
|
| 55 |
#' The name of this list corresponds to the name of the output to which the decorator is applied. |
|
| 56 |
#' See code snippet below: |
|
| 57 |
#' |
|
| 58 |
#' ``` |
|
| 59 |
#' tm_g_bivariate( |
|
| 60 |
#' ..., # arguments for module |
|
| 61 |
#' decorators = list( |
|
| 62 |
#' plot = teal_transform_module(...) # applied to the `plot` output |
|
| 63 |
#' ) |
|
| 64 |
#' ) |
|
| 65 |
#' ``` |
|
| 66 |
#' |
|
| 67 |
#' For additional details and examples of decorators, refer to the vignette |
|
| 68 |
#' `vignette("decorate-module-output", package = "teal.modules.general")`.
|
|
| 69 |
#' |
|
| 70 |
#' To learn more please refer to the vignette |
|
| 71 |
#' `vignette("transform-module-output", package = "teal")` or the [`teal::teal_transform_module()`] documentation.
|
|
| 72 |
#' |
|
| 73 |
#' |
|
| 74 |
#' @examplesShinylive |
|
| 75 |
#' library(teal.modules.general) |
|
| 76 |
#' interactive <- function() TRUE |
|
| 77 |
#' {{ next_example }}
|
|
| 78 |
#' @examples |
|
| 79 |
#' # general data example |
|
| 80 |
#' data <- teal_data() |
|
| 81 |
#' data <- within(data, {
|
|
| 82 |
#' require(nestcolor) |
|
| 83 |
#' CO2 <- data.frame(CO2) |
|
| 84 |
#' }) |
|
| 85 |
#' |
|
| 86 |
#' app <- init( |
|
| 87 |
#' data = data, |
|
| 88 |
#' modules = tm_g_bivariate( |
|
| 89 |
#' x = data_extract_spec( |
|
| 90 |
#' dataname = "CO2", |
|
| 91 |
#' select = select_spec( |
|
| 92 |
#' label = "Select variable:", |
|
| 93 |
#' choices = variable_choices(data[["CO2"]]), |
|
| 94 |
#' selected = "conc", |
|
| 95 |
#' fixed = FALSE |
|
| 96 |
#' ) |
|
| 97 |
#' ), |
|
| 98 |
#' y = data_extract_spec( |
|
| 99 |
#' dataname = "CO2", |
|
| 100 |
#' select = select_spec( |
|
| 101 |
#' label = "Select variable:", |
|
| 102 |
#' choices = variable_choices(data[["CO2"]]), |
|
| 103 |
#' selected = "uptake", |
|
| 104 |
#' multiple = FALSE, |
|
| 105 |
#' fixed = FALSE |
|
| 106 |
#' ) |
|
| 107 |
#' ), |
|
| 108 |
#' row_facet = data_extract_spec( |
|
| 109 |
#' dataname = "CO2", |
|
| 110 |
#' select = select_spec( |
|
| 111 |
#' label = "Select variable:", |
|
| 112 |
#' choices = variable_choices(data[["CO2"]]), |
|
| 113 |
#' selected = "Type", |
|
| 114 |
#' fixed = FALSE |
|
| 115 |
#' ) |
|
| 116 |
#' ), |
|
| 117 |
#' col_facet = data_extract_spec( |
|
| 118 |
#' dataname = "CO2", |
|
| 119 |
#' select = select_spec( |
|
| 120 |
#' label = "Select variable:", |
|
| 121 |
#' choices = variable_choices(data[["CO2"]]), |
|
| 122 |
#' selected = "Treatment", |
|
| 123 |
#' fixed = FALSE |
|
| 124 |
#' ) |
|
| 125 |
#' ) |
|
| 126 |
#' ) |
|
| 127 |
#' ) |
|
| 128 |
#' if (interactive()) {
|
|
| 129 |
#' shinyApp(app$ui, app$server) |
|
| 130 |
#' } |
|
| 131 |
#' |
|
| 132 |
#' @examplesShinylive |
|
| 133 |
#' library(teal.modules.general) |
|
| 134 |
#' interactive <- function() TRUE |
|
| 135 |
#' {{ next_example }}
|
|
| 136 |
#' @examples |
|
| 137 |
#' # CDISC data example |
|
| 138 |
#' data <- teal_data() |
|
| 139 |
#' data <- within(data, {
|
|
| 140 |
#' require(nestcolor) |
|
| 141 |
#' ADSL <- teal.data::rADSL |
|
| 142 |
#' }) |
|
| 143 |
#' join_keys(data) <- default_cdisc_join_keys[names(data)] |
|
| 144 |
#' |
|
| 145 |
#' app <- init( |
|
| 146 |
#' data = data, |
|
| 147 |
#' modules = tm_g_bivariate( |
|
| 148 |
#' x = data_extract_spec( |
|
| 149 |
#' dataname = "ADSL", |
|
| 150 |
#' select = select_spec( |
|
| 151 |
#' label = "Select variable:", |
|
| 152 |
#' choices = variable_choices(data[["ADSL"]]), |
|
| 153 |
#' selected = "AGE", |
|
| 154 |
#' fixed = FALSE |
|
| 155 |
#' ) |
|
| 156 |
#' ), |
|
| 157 |
#' y = data_extract_spec( |
|
| 158 |
#' dataname = "ADSL", |
|
| 159 |
#' select = select_spec( |
|
| 160 |
#' label = "Select variable:", |
|
| 161 |
#' choices = variable_choices(data[["ADSL"]]), |
|
| 162 |
#' selected = "SEX", |
|
| 163 |
#' multiple = FALSE, |
|
| 164 |
#' fixed = FALSE |
|
| 165 |
#' ) |
|
| 166 |
#' ), |
|
| 167 |
#' row_facet = data_extract_spec( |
|
| 168 |
#' dataname = "ADSL", |
|
| 169 |
#' select = select_spec( |
|
| 170 |
#' label = "Select variable:", |
|
| 171 |
#' choices = variable_choices(data[["ADSL"]]), |
|
| 172 |
#' selected = "ARM", |
|
| 173 |
#' fixed = FALSE |
|
| 174 |
#' ) |
|
| 175 |
#' ), |
|
| 176 |
#' col_facet = data_extract_spec( |
|
| 177 |
#' dataname = "ADSL", |
|
| 178 |
#' select = select_spec( |
|
| 179 |
#' label = "Select variable:", |
|
| 180 |
#' choices = variable_choices(data[["ADSL"]]), |
|
| 181 |
#' selected = "COUNTRY", |
|
| 182 |
#' fixed = FALSE |
|
| 183 |
#' ) |
|
| 184 |
#' ) |
|
| 185 |
#' ) |
|
| 186 |
#' ) |
|
| 187 |
#' if (interactive()) {
|
|
| 188 |
#' shinyApp(app$ui, app$server) |
|
| 189 |
#' } |
|
| 190 |
#' |
|
| 191 |
#' @export |
|
| 192 |
#' |
|
| 193 |
tm_g_bivariate <- function(label = "Bivariate Plots", |
|
| 194 |
x, |
|
| 195 |
y, |
|
| 196 |
row_facet = NULL, |
|
| 197 |
col_facet = NULL, |
|
| 198 |
facet = !is.null(row_facet) || !is.null(col_facet), |
|
| 199 |
color = NULL, |
|
| 200 |
fill = NULL, |
|
| 201 |
size = NULL, |
|
| 202 |
use_density = FALSE, |
|
| 203 |
color_settings = FALSE, |
|
| 204 |
free_x_scales = FALSE, |
|
| 205 |
free_y_scales = FALSE, |
|
| 206 |
plot_height = c(600, 200, 2000), |
|
| 207 |
plot_width = NULL, |
|
| 208 |
rotate_xaxis_labels = FALSE, |
|
| 209 |
swap_axes = FALSE, |
|
| 210 |
ggtheme = c("gray", "bw", "linedraw", "light", "dark", "minimal", "classic", "void"),
|
|
| 211 |
ggplot2_args = teal.widgets::ggplot2_args(), |
|
| 212 |
pre_output = NULL, |
|
| 213 |
post_output = NULL, |
|
| 214 |
transformators = list(), |
|
| 215 |
decorators = list()) {
|
|
| 216 | 18x |
message("Initializing tm_g_bivariate")
|
| 217 | ||
| 218 |
# Normalize the parameters |
|
| 219 | 14x |
if (inherits(x, "data_extract_spec")) x <- list(x) |
| 220 | 13x |
if (inherits(y, "data_extract_spec")) y <- list(y) |
| 221 | 1x |
if (inherits(row_facet, "data_extract_spec")) row_facet <- list(row_facet) |
| 222 | 1x |
if (inherits(col_facet, "data_extract_spec")) col_facet <- list(col_facet) |
| 223 | 1x |
if (inherits(color, "data_extract_spec")) color <- list(color) |
| 224 | 1x |
if (inherits(fill, "data_extract_spec")) fill <- list(fill) |
| 225 | 1x |
if (inherits(size, "data_extract_spec")) size <- list(size) |
| 226 | ||
| 227 |
# Start of assertions |
|
| 228 | 18x |
checkmate::assert_string(label) |
| 229 | ||
| 230 | 18x |
checkmate::assert_list(x, types = "data_extract_spec") |
| 231 | 18x |
assert_single_selection(x) |
| 232 | ||
| 233 | 16x |
checkmate::assert_list(y, types = "data_extract_spec") |
| 234 | 16x |
assert_single_selection(y) |
| 235 | ||
| 236 | 14x |
checkmate::assert_list(row_facet, types = "data_extract_spec", null.ok = TRUE) |
| 237 | 14x |
assert_single_selection(row_facet) |
| 238 | ||
| 239 | 14x |
checkmate::assert_list(col_facet, types = "data_extract_spec", null.ok = TRUE) |
| 240 | 14x |
assert_single_selection(col_facet) |
| 241 | ||
| 242 | 14x |
checkmate::assert_flag(facet) |
| 243 | ||
| 244 | 14x |
checkmate::assert_list(color, types = "data_extract_spec", null.ok = TRUE) |
| 245 | 14x |
assert_single_selection(color) |
| 246 | ||
| 247 | 14x |
checkmate::assert_list(fill, types = "data_extract_spec", null.ok = TRUE) |
| 248 | 14x |
assert_single_selection(fill) |
| 249 | ||
| 250 | 14x |
checkmate::assert_list(size, types = "data_extract_spec", null.ok = TRUE) |
| 251 | 14x |
assert_single_selection(size) |
| 252 | ||
| 253 | 14x |
checkmate::assert_flag(use_density) |
| 254 | ||
| 255 |
# Determines color, fill & size if they are not explicitly set |
|
| 256 | 14x |
checkmate::assert_flag(color_settings) |
| 257 | 14x |
if (color_settings) {
|
| 258 | 2x |
if (is.null(color)) {
|
| 259 | 2x |
color <- x |
| 260 | 2x |
color[[1]]$select <- teal.transform::select_spec(choices = color[[1]]$select$choices, selected = NULL) |
| 261 |
} |
|
| 262 | 2x |
if (is.null(fill)) {
|
| 263 | 2x |
fill <- x |
| 264 | 2x |
fill[[1]]$select <- teal.transform::select_spec(choices = fill[[1]]$select$choices, selected = NULL) |
| 265 |
} |
|
| 266 | 2x |
if (is.null(size)) {
|
| 267 | 2x |
size <- x |
| 268 | 2x |
size[[1]]$select <- teal.transform::select_spec(choices = size[[1]]$select$choices, selected = NULL) |
| 269 |
} |
|
| 270 |
} else {
|
|
| 271 | 12x |
if (!is.null(c(color, fill, size))) {
|
| 272 | 3x |
stop("'color_settings' argument needs to be set to TRUE if 'color', 'fill', and/or 'size' is/are supplied.")
|
| 273 |
} |
|
| 274 |
} |
|
| 275 | ||
| 276 | 11x |
checkmate::assert_flag(free_x_scales) |
| 277 | 11x |
checkmate::assert_flag(free_y_scales) |
| 278 | ||
| 279 | 11x |
checkmate::assert_numeric(plot_height, len = 3, any.missing = FALSE, finite = TRUE) |
| 280 | 10x |
checkmate::assert_numeric(plot_height[1], lower = plot_height[2], upper = plot_height[3], .var.name = "plot_height") |
| 281 | 8x |
checkmate::assert_numeric(plot_width, len = 3, any.missing = FALSE, null.ok = TRUE, finite = TRUE) |
| 282 | 7x |
checkmate::assert_numeric( |
| 283 | 7x |
plot_width[1], |
| 284 | 7x |
lower = plot_width[2], upper = plot_width[3], null.ok = TRUE, .var.name = "plot_width" |
| 285 |
) |
|
| 286 | ||
| 287 | 5x |
checkmate::assert_flag(rotate_xaxis_labels) |
| 288 | 5x |
checkmate::assert_flag(swap_axes) |
| 289 | ||
| 290 | 5x |
ggtheme <- match.arg(ggtheme) |
| 291 | 5x |
checkmate::assert_class(ggplot2_args, "ggplot2_args") |
| 292 | ||
| 293 | 5x |
checkmate::assert_multi_class(pre_output, c("shiny.tag", "shiny.tag.list", "html"), null.ok = TRUE)
|
| 294 | 5x |
checkmate::assert_multi_class(post_output, c("shiny.tag", "shiny.tag.list", "html"), null.ok = TRUE)
|
| 295 | ||
| 296 | 5x |
assert_decorators(decorators, "plot") |
| 297 |
# End of assertions |
|
| 298 | ||
| 299 |
# Make UI args |
|
| 300 | 5x |
args <- as.list(environment()) |
| 301 | ||
| 302 | 5x |
data_extract_list <- list( |
| 303 | 5x |
x = x, |
| 304 | 5x |
y = y, |
| 305 | 5x |
row_facet = row_facet, |
| 306 | 5x |
col_facet = col_facet, |
| 307 | 5x |
color_settings = color_settings, |
| 308 | 5x |
color = color, |
| 309 | 5x |
fill = fill, |
| 310 | 5x |
size = size |
| 311 |
) |
|
| 312 | ||
| 313 | 5x |
ans <- module( |
| 314 | 5x |
label = label, |
| 315 | 5x |
server = srv_g_bivariate, |
| 316 | 5x |
ui = ui_g_bivariate, |
| 317 | 5x |
ui_args = args, |
| 318 | 5x |
server_args = c( |
| 319 | 5x |
data_extract_list, |
| 320 | 5x |
list(plot_height = plot_height, plot_width = plot_width, ggplot2_args = ggplot2_args, decorators = decorators) |
| 321 |
), |
|
| 322 | 5x |
transformators = transformators, |
| 323 | 5x |
datanames = teal.transform::get_extract_datanames(data_extract_list) |
| 324 |
) |
|
| 325 | 5x |
attr(ans, "teal_bookmarkable") <- TRUE |
| 326 | 5x |
ans |
| 327 |
} |
|
| 328 | ||
| 329 |
# UI function for the bivariate module |
|
| 330 |
ui_g_bivariate <- function(id, ...) {
|
|
| 331 | ! |
args <- list(...) |
| 332 | ! |
is_single_dataset_value <- teal.transform::is_single_dataset( |
| 333 | ! |
args$x, args$y, args$row_facet, args$col_facet, args$color, args$fill, args$size |
| 334 |
) |
|
| 335 | ||
| 336 | ! |
ns <- NS(id) |
| 337 | ! |
teal.widgets::standard_layout( |
| 338 | ! |
output = teal.widgets::white_small_well( |
| 339 | ! |
tags$div(teal.widgets::plot_with_settings_ui(id = ns("myplot")))
|
| 340 |
), |
|
| 341 | ! |
encoding = tags$div( |
| 342 |
### Reporter |
|
| 343 | ! |
teal.reporter::add_card_button_ui(ns("add_reporter"), label = "Add Report Card"),
|
| 344 | ! |
tags$br(), tags$br(), |
| 345 |
### |
|
| 346 | ! |
tags$label("Encodings", class = "text-primary"),
|
| 347 | ! |
teal.transform::datanames_input(args[c("x", "y", "row_facet", "col_facet", "color", "fill", "size")]),
|
| 348 | ! |
teal.transform::data_extract_ui( |
| 349 | ! |
id = ns("x"),
|
| 350 | ! |
label = "X variable", |
| 351 | ! |
data_extract_spec = args$x, |
| 352 | ! |
is_single_dataset = is_single_dataset_value |
| 353 |
), |
|
| 354 | ! |
teal.transform::data_extract_ui( |
| 355 | ! |
id = ns("y"),
|
| 356 | ! |
label = "Y variable", |
| 357 | ! |
data_extract_spec = args$y, |
| 358 | ! |
is_single_dataset = is_single_dataset_value |
| 359 |
), |
|
| 360 | ! |
conditionalPanel( |
| 361 | ! |
condition = |
| 362 | ! |
"$(\"button[data-id*='-x-dataset'][data-id$='-select']\").text() == '- Nothing selected - ' || |
| 363 | ! |
$(\"button[data-id*='-y-dataset'][data-id$='-select']\").text() == '- Nothing selected - ' ", |
| 364 | ! |
shinyWidgets::radioGroupButtons( |
| 365 | ! |
inputId = ns("use_density"),
|
| 366 | ! |
label = NULL, |
| 367 | ! |
choices = c("frequency", "density"),
|
| 368 | ! |
selected = ifelse(args$use_density, "density", "frequency"), |
| 369 | ! |
justified = TRUE |
| 370 |
) |
|
| 371 |
), |
|
| 372 | ! |
ui_decorate_teal_data(ns("decorator"), decorators = select_decorators(args$decorators, "plot")),
|
| 373 | ! |
if (!is.null(args$row_facet) || !is.null(args$col_facet)) {
|
| 374 | ! |
tags$div( |
| 375 | ! |
class = "data-extract-box", |
| 376 | ! |
tags$br(), |
| 377 | ! |
bslib::input_switch( |
| 378 | ! |
id = ns("facetting"),
|
| 379 | ! |
label = "Facetting", |
| 380 | ! |
value = args$facet |
| 381 |
), |
|
| 382 | ! |
conditionalPanel( |
| 383 | ! |
condition = paste0("input['", ns("facetting"), "']"),
|
| 384 | ! |
tags$div( |
| 385 | ! |
if (!is.null(args$row_facet)) {
|
| 386 | ! |
teal.transform::data_extract_ui( |
| 387 | ! |
id = ns("row_facet"),
|
| 388 | ! |
label = "Row facetting variable", |
| 389 | ! |
data_extract_spec = args$row_facet, |
| 390 | ! |
is_single_dataset = is_single_dataset_value |
| 391 |
) |
|
| 392 |
}, |
|
| 393 | ! |
if (!is.null(args$col_facet)) {
|
| 394 | ! |
teal.transform::data_extract_ui( |
| 395 | ! |
id = ns("col_facet"),
|
| 396 | ! |
label = "Column facetting variable", |
| 397 | ! |
data_extract_spec = args$col_facet, |
| 398 | ! |
is_single_dataset = is_single_dataset_value |
| 399 |
) |
|
| 400 |
}, |
|
| 401 | ! |
checkboxInput(ns("free_x_scales"), "free x scales", value = args$free_x_scales),
|
| 402 | ! |
checkboxInput(ns("free_y_scales"), "free y scales", value = args$free_y_scales)
|
| 403 |
) |
|
| 404 |
) |
|
| 405 |
) |
|
| 406 |
}, |
|
| 407 | ! |
if (args$color_settings) {
|
| 408 |
# Put a grey border around the coloring settings |
|
| 409 | ! |
tags$div( |
| 410 | ! |
class = "data-extract-box", |
| 411 | ! |
tags$label("Color settings"),
|
| 412 | ! |
bslib::input_switch( |
| 413 | ! |
id = ns("coloring"),
|
| 414 | ! |
label = "Color settings", |
| 415 | ! |
value = TRUE |
| 416 |
), |
|
| 417 | ! |
conditionalPanel( |
| 418 | ! |
condition = paste0("input['", ns("coloring"), "']"),
|
| 419 | ! |
tags$div( |
| 420 | ! |
teal.transform::data_extract_ui( |
| 421 | ! |
id = ns("color"),
|
| 422 | ! |
label = "Outline color by variable", |
| 423 | ! |
data_extract_spec = args$color, |
| 424 | ! |
is_single_dataset = is_single_dataset_value |
| 425 |
), |
|
| 426 | ! |
teal.transform::data_extract_ui( |
| 427 | ! |
id = ns("fill"),
|
| 428 | ! |
label = "Fill color by variable", |
| 429 | ! |
data_extract_spec = args$fill, |
| 430 | ! |
is_single_dataset = is_single_dataset_value |
| 431 |
), |
|
| 432 | ! |
tags$div( |
| 433 | ! |
id = ns("size_settings"),
|
| 434 | ! |
teal.transform::data_extract_ui( |
| 435 | ! |
id = ns("size"),
|
| 436 | ! |
label = "Size of points by variable (only if x and y are numeric)", |
| 437 | ! |
data_extract_spec = args$size, |
| 438 | ! |
is_single_dataset = is_single_dataset_value |
| 439 |
) |
|
| 440 |
) |
|
| 441 |
) |
|
| 442 |
) |
|
| 443 |
) |
|
| 444 |
}, |
|
| 445 | ! |
bslib::accordion( |
| 446 | ! |
open = TRUE, |
| 447 | ! |
bslib::accordion_panel( |
| 448 | ! |
title = "Plot settings", |
| 449 | ! |
checkboxInput(ns("rotate_xaxis_labels"), "Rotate X axis labels", value = args$rotate_xaxis_labels),
|
| 450 | ! |
checkboxInput(ns("swap_axes"), "Swap axes", value = args$swap_axes),
|
| 451 | ! |
selectInput( |
| 452 | ! |
inputId = ns("ggtheme"),
|
| 453 | ! |
label = "Theme (by ggplot):", |
| 454 | ! |
choices = ggplot_themes, |
| 455 | ! |
selected = args$ggtheme, |
| 456 | ! |
multiple = FALSE |
| 457 |
), |
|
| 458 | ! |
sliderInput( |
| 459 | ! |
ns("alpha"), "Opacity Scatterplot:",
|
| 460 | ! |
min = 0, max = 1, |
| 461 | ! |
step = .05, value = .5, ticks = FALSE |
| 462 |
), |
|
| 463 | ! |
sliderInput( |
| 464 | ! |
ns("fixed_size"), "Scatterplot point size:",
|
| 465 | ! |
min = 1, max = 8, |
| 466 | ! |
step = 1, value = 2, ticks = FALSE |
| 467 |
), |
|
| 468 | ! |
checkboxInput(ns("add_lines"), "Add lines"),
|
| 469 |
) |
|
| 470 |
) |
|
| 471 |
), |
|
| 472 | ! |
forms = tagList( |
| 473 | ! |
teal.widgets::verbatim_popup_ui(ns("rcode"), "Show R code")
|
| 474 |
), |
|
| 475 | ! |
pre_output = args$pre_output, |
| 476 | ! |
post_output = args$post_output |
| 477 |
) |
|
| 478 |
} |
|
| 479 | ||
| 480 |
# Server function for the bivariate module |
|
| 481 |
srv_g_bivariate <- function(id, |
|
| 482 |
data, |
|
| 483 |
reporter, |
|
| 484 |
filter_panel_api, |
|
| 485 |
x, |
|
| 486 |
y, |
|
| 487 |
row_facet, |
|
| 488 |
col_facet, |
|
| 489 |
color_settings = FALSE, |
|
| 490 |
color, |
|
| 491 |
fill, |
|
| 492 |
size, |
|
| 493 |
plot_height, |
|
| 494 |
plot_width, |
|
| 495 |
ggplot2_args, |
|
| 496 |
decorators) {
|
|
| 497 | ! |
with_reporter <- !missing(reporter) && inherits(reporter, "Reporter") |
| 498 | ! |
with_filter <- !missing(filter_panel_api) && inherits(filter_panel_api, "FilterPanelAPI") |
| 499 | ! |
checkmate::assert_class(data, "reactive") |
| 500 | ! |
checkmate::assert_class(isolate(data()), "teal_data") |
| 501 | ! |
moduleServer(id, function(input, output, session) {
|
| 502 | ! |
teal.logger::log_shiny_input_changes(input, namespace = "teal.modules.general") |
| 503 | ||
| 504 | ! |
ns <- session$ns |
| 505 | ||
| 506 | ! |
data_extract <- list( |
| 507 | ! |
x = x, y = y, row_facet = row_facet, col_facet = col_facet, |
| 508 | ! |
color = color, fill = fill, size = size |
| 509 |
) |
|
| 510 | ||
| 511 | ! |
rule_var <- function(other) {
|
| 512 | ! |
function(value) {
|
| 513 | ! |
othervalue <- selector_list()[[other]]()$select |
| 514 | ! |
if (length(value) == 0L && length(othervalue) == 0L) {
|
| 515 | ! |
"Please select at least one of x-variable or y-variable" |
| 516 |
} |
|
| 517 |
} |
|
| 518 |
} |
|
| 519 | ! |
rule_diff <- function(other) {
|
| 520 | ! |
function(value) {
|
| 521 | ! |
othervalue <- selector_list()[[other]]()[["select"]] |
| 522 | ! |
if (!is.null(othervalue)) {
|
| 523 | ! |
if (identical(value, othervalue)) {
|
| 524 | ! |
"Row and column facetting variables must be different." |
| 525 |
} |
|
| 526 |
} |
|
| 527 |
} |
|
| 528 |
} |
|
| 529 | ||
| 530 | ! |
selector_list <- teal.transform::data_extract_multiple_srv( |
| 531 | ! |
data_extract = data_extract, |
| 532 | ! |
datasets = data, |
| 533 | ! |
select_validation_rule = list( |
| 534 | ! |
x = rule_var("y"),
|
| 535 | ! |
y = rule_var("x"),
|
| 536 | ! |
row_facet = shinyvalidate::compose_rules( |
| 537 | ! |
shinyvalidate::sv_optional(), |
| 538 | ! |
rule_diff("col_facet")
|
| 539 |
), |
|
| 540 | ! |
col_facet = shinyvalidate::compose_rules( |
| 541 | ! |
shinyvalidate::sv_optional(), |
| 542 | ! |
rule_diff("row_facet")
|
| 543 |
) |
|
| 544 |
) |
|
| 545 |
) |
|
| 546 | ||
| 547 | ! |
iv_r <- reactive({
|
| 548 | ! |
iv_facet <- shinyvalidate::InputValidator$new() |
| 549 | ! |
iv_child <- teal.transform::compose_and_enable_validators(iv_facet, selector_list, |
| 550 | ! |
validator_names = c("row_facet", "col_facet")
|
| 551 |
) |
|
| 552 | ! |
iv_child$condition(~ isTRUE(input$facetting)) |
| 553 | ||
| 554 | ! |
iv <- shinyvalidate::InputValidator$new() |
| 555 | ! |
iv$add_validator(iv_child) |
| 556 | ! |
teal.transform::compose_and_enable_validators(iv, selector_list, validator_names = c("x", "y"))
|
| 557 |
}) |
|
| 558 | ||
| 559 | ! |
anl_merged_input <- teal.transform::merge_expression_srv( |
| 560 | ! |
selector_list = selector_list, |
| 561 | ! |
datasets = data |
| 562 |
) |
|
| 563 | ! |
qenv <- reactive( |
| 564 | ! |
teal.code::eval_code(data(), 'library("ggplot2");library("dplyr")') # nolint: quotes.
|
| 565 |
) |
|
| 566 | ||
| 567 | ! |
anl_merged_q <- reactive({
|
| 568 | ! |
req(anl_merged_input()) |
| 569 | ! |
qenv() %>% |
| 570 | ! |
teal.code::eval_code(as.expression(anl_merged_input()$expr)) |
| 571 |
}) |
|
| 572 | ||
| 573 | ! |
merged <- list( |
| 574 | ! |
anl_input_r = anl_merged_input, |
| 575 | ! |
anl_q_r = anl_merged_q |
| 576 |
) |
|
| 577 | ||
| 578 | ! |
output_q <- reactive({
|
| 579 | ! |
teal::validate_inputs(iv_r()) |
| 580 | ||
| 581 | ! |
ANL <- merged$anl_q_r()[["ANL"]] |
| 582 | ! |
teal::validate_has_data(ANL, 3) |
| 583 | ||
| 584 | ! |
x_col_vec <- as.vector(merged$anl_input_r()$columns_source$x) |
| 585 | ! |
x_name <- `if`(is.null(x_col_vec), character(0), x_col_vec) |
| 586 | ! |
y_col_vec <- as.vector(merged$anl_input_r()$columns_source$y) |
| 587 | ! |
y_name <- `if`(is.null(y_col_vec), character(0), y_col_vec) |
| 588 | ||
| 589 | ! |
row_facet_name <- as.vector(merged$anl_input_r()$columns_source$row_facet) |
| 590 | ! |
col_facet_name <- as.vector(merged$anl_input_r()$columns_source$col_facet) |
| 591 | ! |
color_name <- if ("color" %in% names(merged$anl_input_r()$columns_source)) {
|
| 592 | ! |
as.vector(merged$anl_input_r()$columns_source$color) |
| 593 |
} else {
|
|
| 594 | ! |
character(0) |
| 595 |
} |
|
| 596 | ! |
fill_name <- if ("fill" %in% names(merged$anl_input_r()$columns_source)) {
|
| 597 | ! |
as.vector(merged$anl_input_r()$columns_source$fill) |
| 598 |
} else {
|
|
| 599 | ! |
character(0) |
| 600 |
} |
|
| 601 | ! |
size_name <- if ("size" %in% names(merged$anl_input_r()$columns_source)) {
|
| 602 | ! |
as.vector(merged$anl_input_r()$columns_source$size) |
| 603 |
} else {
|
|
| 604 | ! |
character(0) |
| 605 |
} |
|
| 606 | ||
| 607 | ! |
use_density <- input$use_density == "density" |
| 608 | ! |
free_x_scales <- input$free_x_scales |
| 609 | ! |
free_y_scales <- input$free_y_scales |
| 610 | ! |
ggtheme <- input$ggtheme |
| 611 | ! |
rotate_xaxis_labels <- input$rotate_xaxis_labels |
| 612 | ! |
swap_axes <- input$swap_axes |
| 613 | ||
| 614 | ! |
is_scatterplot <- all(vapply(ANL[c(x_name, y_name)], is.numeric, logical(1))) && |
| 615 | ! |
length(x_name) > 0 && length(y_name) > 0 |
| 616 | ||
| 617 | ! |
if (is_scatterplot) {
|
| 618 | ! |
shinyjs::show("alpha")
|
| 619 | ! |
alpha <- input$alpha |
| 620 | ! |
shinyjs::show("add_lines")
|
| 621 | ||
| 622 | ! |
if (color_settings && input$coloring) {
|
| 623 | ! |
shinyjs::hide("fixed_size")
|
| 624 | ! |
shinyjs::show("size_settings")
|
| 625 | ! |
size <- NULL |
| 626 |
} else {
|
|
| 627 | ! |
shinyjs::show("fixed_size")
|
| 628 | ! |
size <- input$fixed_size |
| 629 |
} |
|
| 630 |
} else {
|
|
| 631 | ! |
shinyjs::hide("add_lines")
|
| 632 | ! |
updateCheckboxInput(session, "add_lines", value = restoreInput(ns("add_lines"), FALSE))
|
| 633 | ! |
shinyjs::hide("alpha")
|
| 634 | ! |
shinyjs::hide("fixed_size")
|
| 635 | ! |
shinyjs::hide("size_settings")
|
| 636 | ! |
alpha <- 1 |
| 637 | ! |
size <- NULL |
| 638 |
} |
|
| 639 | ||
| 640 | ! |
teal::validate_has_data(ANL[, c(x_name, y_name), drop = FALSE], 3, complete = TRUE, allow_inf = FALSE) |
| 641 | ||
| 642 | ! |
cl <- bivariate_plot_call( |
| 643 | ! |
data_name = "ANL", |
| 644 | ! |
x = x_name, |
| 645 | ! |
y = y_name, |
| 646 | ! |
x_class = ifelse(!identical(x_name, character(0)), class(ANL[[x_name]]), "NULL"), |
| 647 | ! |
y_class = ifelse(!identical(y_name, character(0)), class(ANL[[y_name]]), "NULL"), |
| 648 | ! |
x_label = varname_w_label(x_name, ANL), |
| 649 | ! |
y_label = varname_w_label(y_name, ANL), |
| 650 | ! |
freq = !use_density, |
| 651 | ! |
theme = ggtheme, |
| 652 | ! |
rotate_xaxis_labels = rotate_xaxis_labels, |
| 653 | ! |
swap_axes = swap_axes, |
| 654 | ! |
alpha = alpha, |
| 655 | ! |
size = size, |
| 656 | ! |
ggplot2_args = ggplot2_args |
| 657 |
) |
|
| 658 | ||
| 659 | ! |
facetting <- (isTRUE(input$facetting) && (!is.null(row_facet_name) || !is.null(col_facet_name))) |
| 660 | ||
| 661 | ! |
if (facetting) {
|
| 662 | ! |
facet_cl <- facet_ggplot_call(row_facet_name, col_facet_name, free_x_scales, free_y_scales) |
| 663 | ||
| 664 | ! |
if (!is.null(facet_cl)) {
|
| 665 | ! |
cl <- call("+", cl, facet_cl)
|
| 666 |
} |
|
| 667 |
} |
|
| 668 | ||
| 669 | ! |
if (input$add_lines) {
|
| 670 | ! |
cl <- call("+", cl, quote(geom_line(size = 1)))
|
| 671 |
} |
|
| 672 | ||
| 673 | ! |
coloring_cl <- NULL |
| 674 | ! |
if (color_settings) {
|
| 675 | ! |
if (input$coloring) {
|
| 676 | ! |
coloring_cl <- coloring_ggplot_call( |
| 677 | ! |
colour = color_name, |
| 678 | ! |
fill = fill_name, |
| 679 | ! |
size = size_name, |
| 680 | ! |
is_point = any(grepl("geom_point", cl %>% deparse()))
|
| 681 |
) |
|
| 682 | ! |
legend_lbls <- substitute( |
| 683 | ! |
expr = labs(color = color_name, fill = fill_name, size = size_name), |
| 684 | ! |
env = list( |
| 685 | ! |
color_name = varname_w_label(color_name, ANL), |
| 686 | ! |
fill_name = varname_w_label(fill_name, ANL), |
| 687 | ! |
size_name = varname_w_label(size_name, ANL) |
| 688 |
) |
|
| 689 |
) |
|
| 690 |
} |
|
| 691 | ! |
if (!is.null(coloring_cl)) {
|
| 692 | ! |
cl <- call("+", call("+", cl, coloring_cl), legend_lbls)
|
| 693 |
} |
|
| 694 |
} |
|
| 695 | ||
| 696 | ! |
teal.code::eval_code(merged$anl_q_r(), substitute(expr = plot <- cl, env = list(cl = cl))) |
| 697 |
}) |
|
| 698 | ||
| 699 | ! |
decorated_output_q_facets <- srv_decorate_teal_data( |
| 700 | ! |
"decorator", |
| 701 | ! |
data = output_q, |
| 702 | ! |
decorators = select_decorators(decorators, "plot"), |
| 703 | ! |
expr = reactive({
|
| 704 | ! |
ANL <- merged$anl_q_r()[["ANL"]] |
| 705 | ! |
row_facet_name <- as.vector(merged$anl_input_r()$columns_source$row_facet) |
| 706 | ! |
col_facet_name <- as.vector(merged$anl_input_r()$columns_source$col_facet) |
| 707 | ||
| 708 |
# Add labels to facets |
|
| 709 | ! |
nulled_row_facet_name <- varname_w_label(row_facet_name, ANL) |
| 710 | ! |
nulled_col_facet_name <- varname_w_label(col_facet_name, ANL) |
| 711 | ! |
facetting <- (isTRUE(input$facetting) && (!is.null(row_facet_name) || !is.null(col_facet_name))) |
| 712 | ! |
without_facet <- (is.null(nulled_row_facet_name) && is.null(nulled_col_facet_name)) || !facetting |
| 713 | ||
| 714 | ! |
print_call <- if (without_facet) {
|
| 715 | ! |
quote(plot) |
| 716 |
} else {
|
|
| 717 | ! |
substitute( |
| 718 | ! |
expr = {
|
| 719 |
# Add facetting labels |
|
| 720 |
# optional: grid.newpage() # nolint: commented_code. |
|
| 721 |
# Prefixed with teal.modules.general as its usage will appear in "Show R code" |
|
| 722 | ! |
plot <- teal.modules.general::add_facet_labels( |
| 723 | ! |
plot, |
| 724 | ! |
xfacet_label = nulled_col_facet_name, |
| 725 | ! |
yfacet_label = nulled_row_facet_name |
| 726 |
) |
|
| 727 | ! |
grid::grid.newpage() |
| 728 | ! |
grid::grid.draw(plot) |
| 729 |
}, |
|
| 730 | ! |
env = list(nulled_col_facet_name = nulled_col_facet_name, nulled_row_facet_name = nulled_row_facet_name) |
| 731 |
) |
|
| 732 |
} |
|
| 733 | ! |
print_call |
| 734 |
}), |
|
| 735 | ! |
expr_is_reactive = TRUE |
| 736 |
) |
|
| 737 | ||
| 738 | ! |
plot_r <- reactive(req(decorated_output_q_facets())[["plot"]]) |
| 739 | ||
| 740 | ! |
pws <- teal.widgets::plot_with_settings_srv( |
| 741 | ! |
id = "myplot", |
| 742 | ! |
plot_r = plot_r, |
| 743 | ! |
height = plot_height, |
| 744 | ! |
width = plot_width |
| 745 |
) |
|
| 746 | ||
| 747 |
# Render R code. |
|
| 748 | ||
| 749 | ! |
source_code_r <- reactive(teal.code::get_code(req(decorated_output_q_facets()))) |
| 750 | ||
| 751 | ! |
teal.widgets::verbatim_popup_srv( |
| 752 | ! |
id = "rcode", |
| 753 | ! |
verbatim_content = source_code_r, |
| 754 | ! |
title = "Bivariate Plot" |
| 755 |
) |
|
| 756 | ||
| 757 |
### REPORTER |
|
| 758 | ! |
if (with_reporter) {
|
| 759 | ! |
card_fun <- function(comment, label) {
|
| 760 | ! |
card <- teal::report_card_template( |
| 761 | ! |
title = "Bivariate Plot", |
| 762 | ! |
label = label, |
| 763 | ! |
with_filter = with_filter, |
| 764 | ! |
filter_panel_api = filter_panel_api |
| 765 |
) |
|
| 766 | ! |
card$append_text("Plot", "header3")
|
| 767 | ! |
card$append_plot(plot_r(), dim = pws$dim()) |
| 768 | ! |
if (!comment == "") {
|
| 769 | ! |
card$append_text("Comment", "header3")
|
| 770 | ! |
card$append_text(comment) |
| 771 |
} |
|
| 772 | ! |
card$append_src(source_code_r()) |
| 773 | ! |
card |
| 774 |
} |
|
| 775 | ! |
teal.reporter::add_card_button_srv("add_reporter", reporter = reporter, card_fun = card_fun)
|
| 776 |
} |
|
| 777 |
### |
|
| 778 |
}) |
|
| 779 |
} |
|
| 780 | ||
| 781 |
# Get Substituted ggplot call |
|
| 782 |
bivariate_plot_call <- function(data_name, |
|
| 783 |
x = character(0), |
|
| 784 |
y = character(0), |
|
| 785 |
x_class = "NULL", |
|
| 786 |
y_class = "NULL", |
|
| 787 |
x_label = NULL, |
|
| 788 |
y_label = NULL, |
|
| 789 |
freq = TRUE, |
|
| 790 |
theme = "gray", |
|
| 791 |
rotate_xaxis_labels = FALSE, |
|
| 792 |
swap_axes = FALSE, |
|
| 793 |
alpha = double(0), |
|
| 794 |
size = 2, |
|
| 795 |
ggplot2_args = teal.widgets::ggplot2_args()) {
|
|
| 796 | ! |
supported_types <- c("NULL", "numeric", "integer", "factor", "character", "logical", "ordered")
|
| 797 | ! |
validate(need(x_class %in% supported_types, paste0("Data type '", x_class, "' is not supported.")))
|
| 798 | ! |
validate(need(y_class %in% supported_types, paste0("Data type '", y_class, "' is not supported.")))
|
| 799 | ||
| 800 | ||
| 801 | ! |
if (identical(x, character(0))) {
|
| 802 | ! |
x <- x_label <- "-" |
| 803 |
} else {
|
|
| 804 | ! |
x <- if (is.call(x)) x else as.name(x) |
| 805 |
} |
|
| 806 | ! |
if (identical(y, character(0))) {
|
| 807 | ! |
y <- y_label <- "-" |
| 808 |
} else {
|
|
| 809 | ! |
y <- if (is.call(y)) y else as.name(y) |
| 810 |
} |
|
| 811 | ||
| 812 | ! |
cl <- bivariate_ggplot_call( |
| 813 | ! |
x_class = x_class, |
| 814 | ! |
y_class = y_class, |
| 815 | ! |
freq = freq, |
| 816 | ! |
theme = theme, |
| 817 | ! |
rotate_xaxis_labels = rotate_xaxis_labels, |
| 818 | ! |
swap_axes = swap_axes, |
| 819 | ! |
alpha = alpha, |
| 820 | ! |
size = size, |
| 821 | ! |
ggplot2_args = ggplot2_args, |
| 822 | ! |
x = x, |
| 823 | ! |
y = y, |
| 824 | ! |
xlab = x_label, |
| 825 | ! |
ylab = y_label, |
| 826 | ! |
data_name = data_name |
| 827 |
) |
|
| 828 |
} |
|
| 829 | ||
| 830 |
# Create ggplot part of plot call |
|
| 831 |
# Due to the type of the x and y variable the plot type is chosen |
|
| 832 |
bivariate_ggplot_call <- function(x_class, |
|
| 833 |
y_class, |
|
| 834 |
freq = TRUE, |
|
| 835 |
theme = "gray", |
|
| 836 |
rotate_xaxis_labels = FALSE, |
|
| 837 |
swap_axes = FALSE, |
|
| 838 |
size = double(0), |
|
| 839 |
alpha = double(0), |
|
| 840 |
x = NULL, |
|
| 841 |
y = NULL, |
|
| 842 |
xlab = "-", |
|
| 843 |
ylab = "-", |
|
| 844 |
data_name = "ANL", |
|
| 845 |
ggplot2_args = teal.widgets::ggplot2_args()) {
|
|
| 846 | 35x |
x_class <- switch(x_class, |
| 847 | 35x |
"character" = , |
| 848 | 35x |
"ordered" = , |
| 849 | 35x |
"logical" = , |
| 850 | 35x |
"factor" = "factor", |
| 851 | 35x |
"integer" = , |
| 852 | 35x |
"numeric" = "numeric", |
| 853 | 35x |
"NULL" = "NULL", |
| 854 | 35x |
stop("unsupported x_class: ", x_class)
|
| 855 |
) |
|
| 856 | 35x |
y_class <- switch(y_class, |
| 857 | 35x |
"character" = , |
| 858 | 35x |
"ordered" = , |
| 859 | 35x |
"logical" = , |
| 860 | 35x |
"factor" = "factor", |
| 861 | 35x |
"integer" = , |
| 862 | 35x |
"numeric" = "numeric", |
| 863 | 35x |
"NULL" = "NULL", |
| 864 | 35x |
stop("unsupported y_class: ", y_class)
|
| 865 |
) |
|
| 866 | ||
| 867 | 35x |
if (all(c(x_class, y_class) == "NULL")) {
|
| 868 | ! |
stop("either x or y is required")
|
| 869 |
} |
|
| 870 | ||
| 871 | 35x |
reduce_plot_call <- function(...) {
|
| 872 | 76x |
args <- Filter(Negate(is.null), list(...)) |
| 873 | 76x |
Reduce(function(x, y) call("+", x, y), args)
|
| 874 |
} |
|
| 875 | ||
| 876 | 35x |
plot_call <- substitute(ggplot2::ggplot(data_name), env = list(data_name = as.name(data_name))) |
| 877 | ||
| 878 |
# Single data plots |
|
| 879 | 35x |
if (x_class == "numeric" && y_class == "NULL") {
|
| 880 | 6x |
plot_call <- reduce_plot_call(plot_call, substitute(ggplot2::aes(x = xval), env = list(xval = x))) |
| 881 | ||
| 882 | 6x |
if (freq) {
|
| 883 | 4x |
plot_call <- reduce_plot_call( |
| 884 | 4x |
plot_call, |
| 885 | 4x |
quote(ggplot2::geom_histogram(bins = 30)), |
| 886 | 4x |
quote(ggplot2::ylab("Frequency"))
|
| 887 |
) |
|
| 888 |
} else {
|
|
| 889 | 2x |
plot_call <- reduce_plot_call( |
| 890 | 2x |
plot_call, |
| 891 | 2x |
quote(ggplot2::geom_histogram(bins = 30, ggplot2::aes(y = ggplot2::after_stat(density)))), |
| 892 | 2x |
quote(ggplot2::geom_density(ggplot2::aes(y = ggplot2::after_stat(density)))), |
| 893 | 2x |
quote(ggplot2::ylab("Density"))
|
| 894 |
) |
|
| 895 |
} |
|
| 896 | 29x |
} else if (x_class == "NULL" && y_class == "numeric") {
|
| 897 | 6x |
plot_call <- reduce_plot_call(plot_call, substitute(ggplot2::aes(x = yval), env = list(yval = y))) |
| 898 | ||
| 899 | 6x |
if (freq) {
|
| 900 | 4x |
plot_call <- reduce_plot_call( |
| 901 | 4x |
plot_call, |
| 902 | 4x |
quote(ggplot2::geom_histogram(bins = 30)), |
| 903 | 4x |
quote(ggplot2::ylab("Frequency"))
|
| 904 |
) |
|
| 905 |
} else {
|
|
| 906 | 2x |
plot_call <- reduce_plot_call( |
| 907 | 2x |
plot_call, |
| 908 | 2x |
quote(ggplot2::geom_histogram(bins = 30, ggplot2::aes(y = ggplot2::after_stat(density)))), |
| 909 | 2x |
quote(ggplot2::geom_density(ggplot2::aes(y = ggplot2::after_stat(density)))), |
| 910 | 2x |
quote(ggplot2::ylab("Density"))
|
| 911 |
) |
|
| 912 |
} |
|
| 913 | 23x |
} else if (x_class == "factor" && y_class == "NULL") {
|
| 914 | 4x |
plot_call <- reduce_plot_call(plot_call, substitute(ggplot2::aes(x = xval), env = list(xval = x))) |
| 915 | ||
| 916 | 4x |
if (freq) {
|
| 917 | 2x |
plot_call <- reduce_plot_call( |
| 918 | 2x |
plot_call, |
| 919 | 2x |
quote(ggplot2::geom_bar()), |
| 920 | 2x |
quote(ggplot2::ylab("Frequency"))
|
| 921 |
) |
|
| 922 |
} else {
|
|
| 923 | 2x |
plot_call <- reduce_plot_call( |
| 924 | 2x |
plot_call, |
| 925 | 2x |
quote(ggplot2::geom_bar(ggplot2::aes(y = ggplot2::after_stat(prop), group = 1))), |
| 926 | 2x |
quote(ggplot2::ylab("Fraction"))
|
| 927 |
) |
|
| 928 |
} |
|
| 929 | 19x |
} else if (x_class == "NULL" && y_class == "factor") {
|
| 930 | 4x |
plot_call <- reduce_plot_call(plot_call, substitute(ggplot2::aes(x = yval), env = list(yval = y))) |
| 931 | ||
| 932 | 4x |
if (freq) {
|
| 933 | 2x |
plot_call <- reduce_plot_call( |
| 934 | 2x |
plot_call, |
| 935 | 2x |
quote(ggplot2::geom_bar()), |
| 936 | 2x |
quote(ggplot2::ylab("Frequency"))
|
| 937 |
) |
|
| 938 |
} else {
|
|
| 939 | 2x |
plot_call <- reduce_plot_call( |
| 940 | 2x |
plot_call, |
| 941 | 2x |
quote(ggplot2::geom_bar(ggplot2::aes(y = ggplot2::after_stat(prop), group = 1))), |
| 942 | 2x |
quote(ggplot2::ylab("Fraction"))
|
| 943 |
) |
|
| 944 |
} |
|
| 945 |
# Numeric Plots |
|
| 946 | 15x |
} else if (x_class == "numeric" && y_class == "numeric") {
|
| 947 | 2x |
plot_call <- reduce_plot_call( |
| 948 | 2x |
plot_call, |
| 949 | 2x |
substitute(ggplot2::aes(x = xval, y = yval), env = list(xval = x, yval = y)), |
| 950 |
# pch = 21 for consistent coloring behaviour b/w all geoms (outline and fill properties) |
|
| 951 | 2x |
`if`( |
| 952 | 2x |
!is.null(size), |
| 953 | 2x |
substitute( |
| 954 | 2x |
ggplot2::geom_point(alpha = alphaval, size = sizeval, pch = 21), |
| 955 | 2x |
env = list(alphaval = alpha, sizeval = size) |
| 956 |
), |
|
| 957 | 2x |
substitute( |
| 958 | 2x |
ggplot2::geom_point(alpha = alphaval, pch = 21), |
| 959 | 2x |
env = list(alphaval = alpha) |
| 960 |
) |
|
| 961 |
) |
|
| 962 |
) |
|
| 963 | 13x |
} else if ((x_class == "numeric" && y_class == "factor") || (x_class == "factor" && y_class == "numeric")) {
|
| 964 | 6x |
plot_call <- reduce_plot_call( |
| 965 | 6x |
plot_call, |
| 966 | 6x |
substitute(ggplot2::aes(x = xval, y = yval), env = list(xval = x, yval = y)), |
| 967 | 6x |
quote(ggplot2::geom_boxplot()) |
| 968 |
) |
|
| 969 |
# Factor and character plots |
|
| 970 | 7x |
} else if (x_class == "factor" && y_class == "factor") {
|
| 971 | 7x |
stop("Categorical variables 'x' and 'y' are currently not supported.")
|
| 972 |
} else {
|
|
| 973 | ! |
stop("x y type combination not allowed")
|
| 974 |
} |
|
| 975 | ||
| 976 | 28x |
labs_base <- if (x_class == "NULL") {
|
| 977 | 10x |
list(x = substitute(ylab, list(ylab = ylab))) |
| 978 | 28x |
} else if (y_class == "NULL") {
|
| 979 | 10x |
list(x = substitute(xlab, list(xlab = xlab))) |
| 980 |
} else {
|
|
| 981 | 8x |
list( |
| 982 | 8x |
x = substitute(xlab, list(xlab = xlab)), |
| 983 | 8x |
y = substitute(ylab, list(ylab = ylab)) |
| 984 |
) |
|
| 985 |
} |
|
| 986 | ||
| 987 | 28x |
dev_ggplot2_args <- teal.widgets::ggplot2_args(labs = labs_base) |
| 988 | ||
| 989 | 28x |
if (rotate_xaxis_labels) {
|
| 990 | ! |
dev_ggplot2_args$theme <- list(axis.text.x = quote(ggplot2::element_text(angle = 45, hjust = 1))) |
| 991 |
} |
|
| 992 | ||
| 993 | 28x |
all_ggplot2_args <- teal.widgets::resolve_ggplot2_args( |
| 994 | 28x |
user_plot = ggplot2_args, |
| 995 | 28x |
module_plot = dev_ggplot2_args |
| 996 |
) |
|
| 997 | ||
| 998 | 28x |
parsed_ggplot2_args <- teal.widgets::parse_ggplot2_args(all_ggplot2_args, ggtheme = theme) |
| 999 | ||
| 1000 | 28x |
plot_call <- reduce_plot_call( |
| 1001 | 28x |
plot_call, |
| 1002 | 28x |
parsed_ggplot2_args$labs, |
| 1003 | 28x |
parsed_ggplot2_args$ggtheme, |
| 1004 | 28x |
parsed_ggplot2_args$theme |
| 1005 |
) |
|
| 1006 | ||
| 1007 | 28x |
if (swap_axes) {
|
| 1008 | ! |
plot_call <- reduce_plot_call(plot_call, quote(coord_flip())) |
| 1009 |
} |
|
| 1010 | ||
| 1011 | 28x |
plot_call |
| 1012 |
} |
|
| 1013 | ||
| 1014 |
# Create facet call |
|
| 1015 |
facet_ggplot_call <- function(row_facet = character(0), |
|
| 1016 |
col_facet = character(0), |
|
| 1017 |
free_x_scales = FALSE, |
|
| 1018 |
free_y_scales = FALSE) {
|
|
| 1019 | ! |
scales <- if (free_x_scales && free_y_scales) {
|
| 1020 | ! |
"free" |
| 1021 | ! |
} else if (free_x_scales) {
|
| 1022 | ! |
"free_x" |
| 1023 | ! |
} else if (free_y_scales) {
|
| 1024 | ! |
"free_y" |
| 1025 |
} else {
|
|
| 1026 | ! |
"fixed" |
| 1027 |
} |
|
| 1028 | ||
| 1029 | ! |
if (identical(row_facet, character(0)) && identical(col_facet, character(0))) {
|
| 1030 | ! |
NULL |
| 1031 | ! |
} else if (!identical(row_facet, character(0)) && !identical(col_facet, character(0))) {
|
| 1032 | ! |
call( |
| 1033 | ! |
"facet_grid", |
| 1034 | ! |
rows = call_fun_dots("vars", row_facet),
|
| 1035 | ! |
cols = call_fun_dots("vars", col_facet),
|
| 1036 | ! |
scales = scales |
| 1037 |
) |
|
| 1038 | ! |
} else if (identical(row_facet, character(0)) && !identical(col_facet, character(0))) {
|
| 1039 | ! |
call("facet_grid", cols = call_fun_dots("vars", col_facet), scales = scales)
|
| 1040 | ! |
} else if (!identical(row_facet, character(0)) && identical(col_facet, character(0))) {
|
| 1041 | ! |
call("facet_grid", rows = call_fun_dots("vars", row_facet), scales = scales)
|
| 1042 |
} |
|
| 1043 |
} |
|
| 1044 | ||
| 1045 |
coloring_ggplot_call <- function(colour, |
|
| 1046 |
fill, |
|
| 1047 |
size, |
|
| 1048 |
is_point = FALSE) {
|
|
| 1049 |
if ( |
|
| 1050 | 15x |
!identical(colour, character(0)) && |
| 1051 | 15x |
!identical(fill, character(0)) && |
| 1052 | 15x |
is_point && |
| 1053 | 15x |
!identical(size, character(0)) |
| 1054 |
) {
|
|
| 1055 | 1x |
substitute( |
| 1056 | 1x |
expr = ggplot2::aes(colour = colour_name, fill = fill_name, size = size_name), |
| 1057 | 1x |
env = list(colour_name = as.name(colour), fill_name = as.name(fill), size_name = as.name(size)) |
| 1058 |
) |
|
| 1059 |
} else if ( |
|
| 1060 | 14x |
identical(colour, character(0)) && |
| 1061 | 14x |
!identical(fill, character(0)) && |
| 1062 | 14x |
is_point && |
| 1063 | 14x |
identical(size, character(0)) |
| 1064 |
) {
|
|
| 1065 | 1x |
substitute(expr = ggplot2::aes(fill = fill_name), env = list(fill_name = as.name(fill))) |
| 1066 |
} else if ( |
|
| 1067 | 13x |
!identical(colour, character(0)) && |
| 1068 | 13x |
!identical(fill, character(0)) && |
| 1069 | 13x |
(!is_point || identical(size, character(0))) |
| 1070 |
) {
|
|
| 1071 | 3x |
substitute( |
| 1072 | 3x |
expr = ggplot2::aes(colour = colour_name, fill = fill_name), |
| 1073 | 3x |
env = list(colour_name = as.name(colour), fill_name = as.name(fill)) |
| 1074 |
) |
|
| 1075 |
} else if ( |
|
| 1076 | 10x |
!identical(colour, character(0)) && |
| 1077 | 10x |
identical(fill, character(0)) && |
| 1078 | 10x |
(!is_point || identical(size, character(0))) |
| 1079 |
) {
|
|
| 1080 | 1x |
substitute(expr = ggplot2::aes(colour = colour_name), env = list(colour_name = as.name(colour))) |
| 1081 |
} else if ( |
|
| 1082 | 9x |
identical(colour, character(0)) && |
| 1083 | 9x |
!identical(fill, character(0)) && |
| 1084 | 9x |
(!is_point || identical(size, character(0))) |
| 1085 |
) {
|
|
| 1086 | 2x |
substitute(expr = ggplot2::aes(fill = fill_name), env = list(fill_name = as.name(fill))) |
| 1087 |
} else if ( |
|
| 1088 | 7x |
identical(colour, character(0)) && |
| 1089 | 7x |
identical(fill, character(0)) && |
| 1090 | 7x |
is_point && |
| 1091 | 7x |
!identical(size, character(0)) |
| 1092 |
) {
|
|
| 1093 | 1x |
substitute(expr = ggplot2::aes(size = size_name), env = list(size_name = as.name(size))) |
| 1094 |
} else if ( |
|
| 1095 | 6x |
!identical(colour, character(0)) && |
| 1096 | 6x |
identical(fill, character(0)) && |
| 1097 | 6x |
is_point && |
| 1098 | 6x |
!identical(size, character(0)) |
| 1099 |
) {
|
|
| 1100 | 1x |
substitute( |
| 1101 | 1x |
expr = ggplot2::aes(colour = colour_name, size = size_name), |
| 1102 | 1x |
env = list(colour_name = as.name(colour), size_name = as.name(size)) |
| 1103 |
) |
|
| 1104 |
} else if ( |
|
| 1105 | 5x |
identical(colour, character(0)) && |
| 1106 | 5x |
!identical(fill, character(0)) && |
| 1107 | 5x |
is_point && |
| 1108 | 5x |
!identical(size, character(0)) |
| 1109 |
) {
|
|
| 1110 | 1x |
substitute( |
| 1111 | 1x |
expr = ggplot2::aes(colour = colour_name, fill = fill_name, size = size_name), |
| 1112 | 1x |
env = list(colour_name = as.name(fill), fill_name = as.name(fill), size_name = as.name(size)) |
| 1113 |
) |
|
| 1114 |
} else {
|
|
| 1115 | 4x |
NULL |
| 1116 |
} |
|
| 1117 |
} |
| 1 |
#' Shared parameters documentation |
|
| 2 |
#' |
|
| 3 |
#' Defines common arguments shared across multiple functions in the package |
|
| 4 |
#' to avoid repetition by using `inheritParams`. |
|
| 5 |
#' |
|
| 6 |
#' @param plot_height (`numeric`) optional, specifies the plot height as a three-element vector of |
|
| 7 |
#' `value`, `min`, and `max` intended for use with a slider UI element. |
|
| 8 |
#' @param plot_width (`numeric`) optional, specifies the plot width as a three-element vector of |
|
| 9 |
#' `value`, `min`, and `max` for a slider encoding the plot width. |
|
| 10 |
#' @param rotate_xaxis_labels (`logical`) optional, whether to rotate plot X axis labels. Does not |
|
| 11 |
#' rotate by default (`FALSE`). |
|
| 12 |
#' @param ggtheme (`character`) optional, `ggplot2` theme to be used by default. Defaults to `"gray"`. |
|
| 13 |
#' @param ggplot2_args (`ggplot2_args`) object created by [teal.widgets::ggplot2_args()] |
|
| 14 |
#' with settings for the module plot. |
|
| 15 |
#' The argument is merged with options variable `teal.ggplot2_args` and default module setup. |
|
| 16 |
#' |
|
| 17 |
#' For more details see the vignette: `vignette("custom-ggplot2-arguments", package = "teal.widgets")`
|
|
| 18 |
#' @param basic_table_args (`basic_table_args`) object created by [teal.widgets::basic_table_args()] |
|
| 19 |
#' with settings for the module table. |
|
| 20 |
#' The argument is merged with options variable `teal.basic_table_args` and default module setup. |
|
| 21 |
#' |
|
| 22 |
#' For more details see the vignette: `vignette("custom-basic-table-arguments", package = "teal.widgets")`
|
|
| 23 |
#' @param pre_output (`shiny.tag`) optional, text or UI element to be displayed before the module's output, |
|
| 24 |
#' providing context or a title. |
|
| 25 |
#' with text placed before the output to put the output into context. For example a title. |
|
| 26 |
#' @param post_output (`shiny.tag`) optional, text or UI element to be displayed after the module's output, |
|
| 27 |
#' adding context or further instructions. Elements like `shiny::helpText()` are useful. |
|
| 28 |
#' @param alpha (`integer(1)` or `integer(3)`) optional, specifies point opacity. |
|
| 29 |
#' - When the length of `alpha` is one: the plot points will have a fixed opacity. |
|
| 30 |
#' - When the length of `alpha` is three: the plot points opacity are dynamically adjusted based on |
|
| 31 |
#' vector of `value`, `min`, and `max`. |
|
| 32 |
#' @param size (`integer(1)` or `integer(3)`) optional, specifies point size. |
|
| 33 |
#' - When the length of `size` is one: the plot point sizes will have a fixed size. |
|
| 34 |
#' - When the length of `size` is three: the plot points size are dynamically adjusted based on |
|
| 35 |
#' vector of `value`, `min`, and `max`. |
|
| 36 |
#' @param decorators `r lifecycle::badge("experimental")`
|
|
| 37 |
#' (named `list` of lists of `teal_transform_module`) optional, |
|
| 38 |
#' decorator for tables or plots included in the module output reported. |
|
| 39 |
#' The decorators are applied to the respective output objects. |
|
| 40 |
#' |
|
| 41 |
#' See section "Decorating Module" below for more details. |
|
| 42 |
#' |
|
| 43 |
#' @return Object of class `teal_module` to be used in `teal` applications. |
|
| 44 |
#' |
|
| 45 |
#' @name shared_params |
|
| 46 |
#' @keywords internal |
|
| 47 |
NULL |
|
| 48 | ||
| 49 |
#' Add labels for facets to a `ggplot2` object |
|
| 50 |
#' |
|
| 51 |
#' Enhances a `ggplot2` plot by adding labels that describe |
|
| 52 |
#' the faceting variables along the x and y axes. |
|
| 53 |
#' |
|
| 54 |
#' @param p (`ggplot2`) object to which facet labels will be added. |
|
| 55 |
#' @param xfacet_label (`character`) Label for the facet along the x-axis. |
|
| 56 |
#' If `NULL`, no label is added. If a vector, labels are joined with " & ". |
|
| 57 |
#' @param yfacet_label (`character`) Label for the facet along the y-axis. |
|
| 58 |
#' Similar behavior to `xfacet_label`. |
|
| 59 |
#' |
|
| 60 |
#' @return Returns `grid` or `grob` object (to be drawn with `grid.draw`) |
|
| 61 |
#' |
|
| 62 |
#' @examples |
|
| 63 |
#' library(ggplot2) |
|
| 64 |
#' library(grid) |
|
| 65 |
#' |
|
| 66 |
#' p <- ggplot(mtcars) + |
|
| 67 |
#' aes(x = mpg, y = disp) + |
|
| 68 |
#' geom_point() + |
|
| 69 |
#' facet_grid(gear ~ cyl) |
|
| 70 |
#' |
|
| 71 |
#' xfacet_label <- "cylinders" |
|
| 72 |
#' yfacet_label <- "gear" |
|
| 73 |
#' res <- add_facet_labels(p, xfacet_label, yfacet_label) |
|
| 74 |
#' grid.newpage() |
|
| 75 |
#' grid.draw(res) |
|
| 76 |
#' |
|
| 77 |
#' grid.newpage() |
|
| 78 |
#' grid.draw(add_facet_labels(p, xfacet_label = NULL, yfacet_label)) |
|
| 79 |
#' grid.newpage() |
|
| 80 |
#' grid.draw(add_facet_labels(p, xfacet_label, yfacet_label = NULL)) |
|
| 81 |
#' grid.newpage() |
|
| 82 |
#' grid.draw(add_facet_labels(p, xfacet_label = NULL, yfacet_label = NULL)) |
|
| 83 |
#' |
|
| 84 |
#' @export |
|
| 85 |
#' |
|
| 86 |
add_facet_labels <- function(p, xfacet_label = NULL, yfacet_label = NULL) {
|
|
| 87 | ! |
checkmate::assert_class(p, classes = "ggplot") |
| 88 | ! |
checkmate::assert_character(xfacet_label, null.ok = TRUE, min.len = 1) |
| 89 | ! |
checkmate::assert_character(yfacet_label, null.ok = TRUE, min.len = 1) |
| 90 | ! |
if (is.null(xfacet_label) && is.null(yfacet_label)) {
|
| 91 | ! |
return(ggplot2::ggplotGrob(p)) |
| 92 |
} |
|
| 93 | ! |
grid::grid.grabExpr({
|
| 94 | ! |
g <- ggplot2::ggplotGrob(p) |
| 95 | ||
| 96 |
# we are going to replace these, so we make sure they have nothing in them |
|
| 97 | ! |
checkmate::assert_class(g$grobs[[grep("xlab-t", g$layout$name, fixed = TRUE)]], "zeroGrob")
|
| 98 | ! |
checkmate::assert_class(g$grobs[[grep("ylab-r", g$layout$name, fixed = TRUE)]], "zeroGrob")
|
| 99 | ||
| 100 | ! |
xaxis_label_grob <- g$grobs[[grep("xlab-b", g$layout$name, fixed = TRUE)]]
|
| 101 | ! |
xaxis_label_grob$children[[1]]$label <- paste(xfacet_label, collapse = " & ") |
| 102 | ! |
yaxis_label_grob <- g$grobs[[grep("ylab-l", g$layout$name, fixed = TRUE)]]
|
| 103 | ! |
yaxis_label_grob$children[[1]]$label <- paste(yfacet_label, collapse = " & ") |
| 104 | ! |
yaxis_label_grob$children[[1]]$rot <- 270 |
| 105 | ||
| 106 | ! |
top_height <- if (is.null(xfacet_label)) 0 else grid::unit(2, "line") |
| 107 | ! |
right_width <- if (is.null(yfacet_label)) 0 else grid::unit(2, "line") |
| 108 | ||
| 109 | ! |
grid::grid.newpage() |
| 110 | ! |
grid::pushViewport(grid::plotViewport(margins = c(0, 0, top_height, right_width), name = "ggplot")) |
| 111 | ! |
grid::grid.draw(g) |
| 112 | ! |
grid::upViewport(1) |
| 113 | ||
| 114 |
# draw x facet |
|
| 115 | ! |
if (!is.null(xfacet_label)) {
|
| 116 | ! |
grid::pushViewport(grid::viewport( |
| 117 | ! |
x = 0, y = grid::unit(1, "npc") - top_height, width = grid::unit(1, "npc"), |
| 118 | ! |
height = top_height, just = c("left", "bottom"), name = "topxaxis"
|
| 119 |
)) |
|
| 120 | ! |
grid::grid.draw(xaxis_label_grob) |
| 121 | ! |
grid::upViewport(1) |
| 122 |
} |
|
| 123 | ||
| 124 |
# draw y facet |
|
| 125 | ! |
if (!is.null(yfacet_label)) {
|
| 126 | ! |
grid::pushViewport(grid::viewport( |
| 127 | ! |
x = grid::unit(1, "npc") - grid::unit(as.numeric(right_width) / 2, "line"), y = 0, width = right_width, |
| 128 | ! |
height = grid::unit(1, "npc"), just = c("left", "bottom"), name = "rightyaxis"
|
| 129 |
)) |
|
| 130 | ! |
grid::grid.draw(yaxis_label_grob) |
| 131 | ! |
grid::upViewport(1) |
| 132 |
} |
|
| 133 |
}) |
|
| 134 |
} |
|
| 135 | ||
| 136 |
#' Call a function with a character vector for the `...` argument |
|
| 137 |
#' |
|
| 138 |
#' @param fun (`character`) Name of a function where the `...` argument shall be replaced by values from `str_args`. |
|
| 139 |
#' @param str_args (`character`) A character vector that the function shall be executed with |
|
| 140 |
#' |
|
| 141 |
#' @return |
|
| 142 |
#' Value of call to `fun` with arguments specified in `str_args`. |
|
| 143 |
#' |
|
| 144 |
#' @keywords internal |
|
| 145 |
call_fun_dots <- function(fun, str_args) {
|
|
| 146 | ! |
do.call("call", c(list(fun), lapply(str_args, as.name)), quote = TRUE)
|
| 147 |
} |
|
| 148 | ||
| 149 |
#' Generate a string for a variable including its label |
|
| 150 |
#' |
|
| 151 |
#' @param var_names (`character`) Name of variable to extract labels from. |
|
| 152 |
#' @param dataset (`dataset`) Name of analysis dataset. |
|
| 153 |
#' @param prefix,suffix (`character`) String to paste to the beginning/end of the variable name with label. |
|
| 154 |
#' @param wrap_width (`numeric`) Number of characters to wrap original label to. Defaults to 80. |
|
| 155 |
#' |
|
| 156 |
#' @return (`character`) String with variable name and label. |
|
| 157 |
#' |
|
| 158 |
#' @keywords internal |
|
| 159 |
#' |
|
| 160 |
varname_w_label <- function(var_names, |
|
| 161 |
dataset, |
|
| 162 |
wrap_width = 80, |
|
| 163 |
prefix = NULL, |
|
| 164 |
suffix = NULL) {
|
|
| 165 | ! |
add_label <- function(var_names) {
|
| 166 | ! |
label <- vapply( |
| 167 | ! |
dataset[var_names], function(x) {
|
| 168 | ! |
attr_label <- attr(x, "label") |
| 169 | ! |
`if`(is.null(attr_label), "", attr_label) |
| 170 |
}, |
|
| 171 | ! |
character(1) |
| 172 |
) |
|
| 173 | ||
| 174 | ! |
if (length(label) == 1 && !is.na(label) && !identical(label, "")) {
|
| 175 | ! |
paste0(prefix, label, " [", var_names, "]", suffix) |
| 176 |
} else {
|
|
| 177 | ! |
var_names |
| 178 |
} |
|
| 179 |
} |
|
| 180 | ||
| 181 | ! |
if (length(var_names) < 1) {
|
| 182 | ! |
NULL |
| 183 | ! |
} else if (length(var_names) == 1) {
|
| 184 | ! |
stringr::str_wrap(add_label(var_names), width = wrap_width) |
| 185 | ! |
} else if (length(var_names) > 1) {
|
| 186 | ! |
stringr::str_wrap(vapply(var_names, add_label, character(1)), width = wrap_width) |
| 187 |
} |
|
| 188 |
} |
|
| 189 | ||
| 190 |
# see vignette("ggplot2-specs", package="ggplot2")
|
|
| 191 |
shape_names <- c( |
|
| 192 |
"circle", paste("circle", c("open", "filled", "cross", "plus", "small")), "bullet",
|
|
| 193 |
"square", paste("square", c("open", "filled", "cross", "plus", "triangle")),
|
|
| 194 |
"diamond", paste("diamond", c("open", "filled", "plus")),
|
|
| 195 |
"triangle", paste("triangle", c("open", "filled", "square")),
|
|
| 196 |
paste("triangle down", c("open", "filled")),
|
|
| 197 |
"plus", "cross", "asterisk" |
|
| 198 |
) |
|
| 199 | ||
| 200 |
#' Get icons to represent variable types in dataset |
|
| 201 |
#' |
|
| 202 |
#' @param var_type (`character`) of R internal types (classes). |
|
| 203 |
#' @return (`character`) vector of HTML icons corresponding to data type in each column. |
|
| 204 |
#' @keywords internal |
|
| 205 |
variable_type_icons <- function(var_type) {
|
|
| 206 | ! |
checkmate::assert_character(var_type, any.missing = FALSE) |
| 207 | ||
| 208 | ! |
class_to_icon <- list( |
| 209 | ! |
numeric = "arrow-up-1-9", |
| 210 | ! |
integer = "arrow-up-1-9", |
| 211 | ! |
logical = "pause", |
| 212 | ! |
Date = "calendar", |
| 213 | ! |
POSIXct = "calendar", |
| 214 | ! |
POSIXlt = "calendar", |
| 215 | ! |
factor = "chart-bar", |
| 216 | ! |
character = "keyboard", |
| 217 | ! |
primary_key = "key", |
| 218 | ! |
unknown = "circle-question" |
| 219 |
) |
|
| 220 | ! |
class_to_icon <- lapply(class_to_icon, function(icon_name) toString(icon(icon_name, lib = "font-awesome"))) |
| 221 | ||
| 222 | ! |
unname(vapply( |
| 223 | ! |
var_type, |
| 224 | ! |
FUN.VALUE = character(1), |
| 225 | ! |
FUN = function(class) {
|
| 226 | ! |
if (class == "") {
|
| 227 | ! |
class |
| 228 | ! |
} else if (is.null(class_to_icon[[class]])) {
|
| 229 | ! |
class_to_icon[["unknown"]] |
| 230 |
} else {
|
|
| 231 | ! |
class_to_icon[[class]] |
| 232 |
} |
|
| 233 |
} |
|
| 234 |
)) |
|
| 235 |
} |
|
| 236 | ||
| 237 |
#' |
|
| 238 |
#' @param id (`character(1)`) the id of the tab panel with tabs. |
|
| 239 |
#' @param name (`character(1)`) the name of the tab. |
|
| 240 |
#' @return JavaScript expression to be used in `shiny::conditionalPanel()` to determine |
|
| 241 |
#' if the specified tab is active. |
|
| 242 |
#' @keywords internal |
|
| 243 |
#' |
|
| 244 |
is_tab_active_js <- function(id, name) {
|
|
| 245 |
# supporting the bs3 and higher version at the same time |
|
| 246 | ! |
sprintf( |
| 247 | ! |
"$(\"#%1$s > li.active\").text().trim() == '%2$s' || $(\"#%1$s > li a.active\").text().trim() == '%2$s'", |
| 248 | ! |
id, name |
| 249 |
) |
|
| 250 |
} |
|
| 251 | ||
| 252 |
#' Assert single selection on `data_extract_spec` object |
|
| 253 |
#' Helper to reduce code in assertions |
|
| 254 |
#' @noRd |
|
| 255 |
#' |
|
| 256 |
assert_single_selection <- function(x, |
|
| 257 |
.var.name = checkmate::vname(x)) { # nolint: object_name.
|
|
| 258 | 104x |
if (any(vapply(x, function(.x) .x$select$multiple, logical(1)))) {
|
| 259 | 4x |
stop("'", .var.name, "' should not allow multiple selection")
|
| 260 |
} |
|
| 261 | 100x |
invisible(TRUE) |
| 262 |
} |
|
| 263 | ||
| 264 |
#' Wrappers around `srv_transform_teal_data` that allows to decorate the data |
|
| 265 |
#' @inheritParams teal::srv_transform_teal_data |
|
| 266 |
#' @param expr (`expression` or `reactive`) to evaluate on the output of the decoration. |
|
| 267 |
#' When an expression it must be inline code. See [within()] |
|
| 268 |
#' Default is `NULL` which won't evaluate any appending code. |
|
| 269 |
#' @param expr_is_reactive (`logical(1)`) whether `expr` is a reactive expression |
|
| 270 |
#' that skips defusing the argument. |
|
| 271 |
#' @details |
|
| 272 |
#' `srv_decorate_teal_data` is a wrapper around `srv_transform_teal_data` that |
|
| 273 |
#' allows to decorate the data with additional expressions. |
|
| 274 |
#' When original `teal_data` object is in error state, it will show that error |
|
| 275 |
#' first. |
|
| 276 |
#' |
|
| 277 |
#' @keywords internal |
|
| 278 |
srv_decorate_teal_data <- function(id, data, decorators, expr, expr_is_reactive = FALSE) {
|
|
| 279 | ! |
checkmate::assert_class(data, classes = "reactive") |
| 280 | ! |
checkmate::assert_list(decorators, "teal_transform_module") |
| 281 | ! |
checkmate::assert_flag(expr_is_reactive) |
| 282 | ||
| 283 | ! |
missing_expr <- missing(expr) |
| 284 | ! |
if (!missing_expr && !expr_is_reactive) {
|
| 285 | ! |
expr <- dplyr::enexpr(expr) # Using dplyr re-export to avoid adding rlang to Imports |
| 286 |
} |
|
| 287 | ||
| 288 | ! |
moduleServer(id, function(input, output, session) {
|
| 289 | ! |
decorated_output <- srv_transform_teal_data("inner", data = data, transformators = decorators)
|
| 290 | ||
| 291 | ! |
reactive({
|
| 292 | ! |
data_out <- try(data(), silent = TRUE) |
| 293 | ! |
if (inherits(data_out, "qenv.error")) {
|
| 294 | ! |
data() |
| 295 |
} else {
|
|
| 296 |
# ensure original errors are displayed and `eval_code` is never executed with NULL |
|
| 297 | ! |
req(data(), decorated_output()) |
| 298 | ! |
if (missing_expr) {
|
| 299 | ! |
decorated_output() |
| 300 | ! |
} else if (expr_is_reactive) {
|
| 301 | ! |
teal.code::eval_code(decorated_output(), expr()) |
| 302 |
} else {
|
|
| 303 | ! |
teal.code::eval_code(decorated_output(), expr) |
| 304 |
} |
|
| 305 |
} |
|
| 306 |
}) |
|
| 307 |
}) |
|
| 308 |
} |
|
| 309 | ||
| 310 |
#' @rdname srv_decorate_teal_data |
|
| 311 |
#' @details |
|
| 312 |
#' `ui_decorate_teal_data` is a wrapper around `ui_transform_teal_data`. |
|
| 313 |
#' @keywords internal |
|
| 314 |
ui_decorate_teal_data <- function(id, decorators, ...) {
|
|
| 315 | ! |
teal::ui_transform_teal_data(NS(id, "inner"), transformators = decorators, ...) |
| 316 |
} |
|
| 317 | ||
| 318 |
#' Internal function to check if decorators is a valid object |
|
| 319 |
#' @noRd |
|
| 320 |
check_decorators <- function(x, names = NULL) { # nolint: object_name.
|
|
| 321 | ||
| 322 | 5x |
check_message <- checkmate::check_list(x, names = "named") |
| 323 | ||
| 324 | 5x |
if (!is.null(names)) {
|
| 325 | 5x |
if (isTRUE(check_message)) {
|
| 326 | 5x |
if (length(names(x)) != length(unique(names(x)))) {
|
| 327 | ! |
check_message <- sprintf( |
| 328 | ! |
"The `decorators` must contain unique names from these names: %s.", |
| 329 | ! |
paste(names, collapse = ", ") |
| 330 |
) |
|
| 331 |
} |
|
| 332 |
} else {
|
|
| 333 | ! |
check_message <- sprintf( |
| 334 | ! |
"The `decorators` must be a named list from these names: %s.", |
| 335 | ! |
paste(names, collapse = ", ") |
| 336 |
) |
|
| 337 |
} |
|
| 338 |
} |
|
| 339 | ||
| 340 | 5x |
if (!isTRUE(check_message)) {
|
| 341 | ! |
return(check_message) |
| 342 |
} |
|
| 343 | ||
| 344 | 5x |
valid_elements <- vapply( |
| 345 | 5x |
x, |
| 346 | 5x |
checkmate::test_class, |
| 347 | 5x |
classes = "teal_transform_module", |
| 348 | 5x |
FUN.VALUE = logical(1L) |
| 349 |
) |
|
| 350 | ||
| 351 | 5x |
if (all(valid_elements)) {
|
| 352 | 5x |
return(TRUE) |
| 353 |
} |
|
| 354 | ||
| 355 | ! |
"Make sure that the named list contains 'teal_transform_module' objects created using `teal_transform_module()`." |
| 356 |
} |
|
| 357 |
#' Internal assertion on decorators |
|
| 358 |
#' @noRd |
|
| 359 |
assert_decorators <- checkmate::makeAssertionFunction(check_decorators) |
|
| 360 | ||
| 361 |
#' Subset decorators based on the scope |
|
| 362 |
#' |
|
| 363 |
#' @param scope (`character`) a character vector of decorator names to include. |
|
| 364 |
#' @param decorators (named `list`) of list decorators to subset. |
|
| 365 |
#' |
|
| 366 |
#' @return Subsetted list with all decorators to include. |
|
| 367 |
#' It can be an empty list if none of the scope exists in `decorators` argument. |
|
| 368 |
#' @keywords internal |
|
| 369 |
select_decorators <- function(decorators, scope) {
|
|
| 370 | ! |
checkmate::assert_character(scope, null.ok = TRUE) |
| 371 | ! |
if (scope %in% names(decorators)) {
|
| 372 | ! |
decorators[scope] |
| 373 |
} else {
|
|
| 374 | ! |
list() |
| 375 |
} |
|
| 376 |
} |
| 1 |
#' `teal` module: Principal component analysis |
|
| 2 |
#' |
|
| 3 |
#' Module conducts principal component analysis (PCA) on a given dataset and offers different |
|
| 4 |
#' ways of visualizing the outcomes, including elbow plot, circle plot, biplot, and eigenvector plot. |
|
| 5 |
#' Additionally, it enables dynamic customization of plot aesthetics, such as opacity, size, and |
|
| 6 |
#' font size, through UI inputs. |
|
| 7 |
#' |
|
| 8 |
#' @inheritParams teal::module |
|
| 9 |
#' @inheritParams shared_params |
|
| 10 |
#' @param dat (`data_extract_spec` or `list` of multiple `data_extract_spec`) |
|
| 11 |
#' specifying columns used to compute PCA. |
|
| 12 |
#' @param font_size (`numeric`) optional, specifies font size. |
|
| 13 |
#' It controls the font size for plot titles, axis labels, and legends. |
|
| 14 |
#' - If vector of `length == 1` then the font sizes will have a fixed size. |
|
| 15 |
#' - while vector of `value`, `min`, and `max` allows dynamic adjustment. |
|
| 16 |
#' @param ggplot2_args `r roxygen_ggplot2_args_param("Elbow plot", "Circle plot", "Biplot", "Eigenvector plot")`
|
|
| 17 |
#' |
|
| 18 |
#' @inherit shared_params return |
|
| 19 |
#' |
|
| 20 |
#' @section Decorating Module: |
|
| 21 |
#' |
|
| 22 |
#' This module generates the following objects, which can be modified in place using decorators: |
|
| 23 |
#' - `elbow_plot` (`ggplot`) |
|
| 24 |
#' - `circle_plot` (`ggplot`) |
|
| 25 |
#' - `biplot` (`ggplot`) |
|
| 26 |
#' - `eigenvector_plot` (`ggplot`) |
|
| 27 |
#' |
|
| 28 |
#' A Decorator is applied to the specific output using a named list of `teal_transform_module` objects. |
|
| 29 |
#' The name of this list corresponds to the name of the output to which the decorator is applied. |
|
| 30 |
#' See code snippet below: |
|
| 31 |
#' |
|
| 32 |
#' ``` |
|
| 33 |
#' tm_a_pca( |
|
| 34 |
#' ..., # arguments for module |
|
| 35 |
#' decorators = list( |
|
| 36 |
#' elbow_plot = teal_transform_module(...), # applied to the `elbow_plot` output |
|
| 37 |
#' circle_plot = teal_transform_module(...), # applied to the `circle_plot` output |
|
| 38 |
#' biplot = teal_transform_module(...), # applied to the `biplot` output |
|
| 39 |
#' eigenvector_plot = teal_transform_module(...) # applied to the `eigenvector_plot` output |
|
| 40 |
#' ) |
|
| 41 |
#' ) |
|
| 42 |
#' ``` |
|
| 43 |
#' |
|
| 44 |
#' For additional details and examples of decorators, refer to the vignette |
|
| 45 |
#' `vignette("decorate-module-output", package = "teal.modules.general")`.
|
|
| 46 |
#' |
|
| 47 |
#' To learn more please refer to the vignette |
|
| 48 |
#' `vignette("transform-module-output", package = "teal")` or the [`teal::teal_transform_module()`] documentation.
|
|
| 49 |
#' |
|
| 50 |
#' @examplesShinylive |
|
| 51 |
#' library(teal.modules.general) |
|
| 52 |
#' interactive <- function() TRUE |
|
| 53 |
#' {{ next_example }}
|
|
| 54 |
#' @examples |
|
| 55 |
#' |
|
| 56 |
#' # general data example |
|
| 57 |
#' data <- teal_data() |
|
| 58 |
#' data <- within(data, {
|
|
| 59 |
#' require(nestcolor) |
|
| 60 |
#' USArrests <- USArrests |
|
| 61 |
#' }) |
|
| 62 |
#' |
|
| 63 |
#' app <- init( |
|
| 64 |
#' data = data, |
|
| 65 |
#' modules = modules( |
|
| 66 |
#' tm_a_pca( |
|
| 67 |
#' "PCA", |
|
| 68 |
#' dat = data_extract_spec( |
|
| 69 |
#' dataname = "USArrests", |
|
| 70 |
#' select = select_spec( |
|
| 71 |
#' choices = variable_choices( |
|
| 72 |
#' data = data[["USArrests"]], c("Murder", "Assault", "UrbanPop", "Rape")
|
|
| 73 |
#' ), |
|
| 74 |
#' selected = c("Murder", "Assault"),
|
|
| 75 |
#' multiple = TRUE |
|
| 76 |
#' ), |
|
| 77 |
#' filter = NULL |
|
| 78 |
#' ) |
|
| 79 |
#' ) |
|
| 80 |
#' ) |
|
| 81 |
#' ) |
|
| 82 |
#' if (interactive()) {
|
|
| 83 |
#' shinyApp(app$ui, app$server) |
|
| 84 |
#' } |
|
| 85 |
#' |
|
| 86 |
#' @examplesShinylive |
|
| 87 |
#' library(teal.modules.general) |
|
| 88 |
#' interactive <- function() TRUE |
|
| 89 |
#' {{ next_example }}
|
|
| 90 |
#' @examples |
|
| 91 |
#' |
|
| 92 |
#' # CDISC data example |
|
| 93 |
#' data <- teal_data() |
|
| 94 |
#' data <- within(data, {
|
|
| 95 |
#' require(nestcolor) |
|
| 96 |
#' ADSL <- teal.data::rADSL |
|
| 97 |
#' }) |
|
| 98 |
#' join_keys(data) <- default_cdisc_join_keys[names(data)] |
|
| 99 |
#' |
|
| 100 |
#' app <- init( |
|
| 101 |
#' data = data, |
|
| 102 |
#' modules = modules( |
|
| 103 |
#' tm_a_pca( |
|
| 104 |
#' "PCA", |
|
| 105 |
#' dat = data_extract_spec( |
|
| 106 |
#' dataname = "ADSL", |
|
| 107 |
#' select = select_spec( |
|
| 108 |
#' choices = variable_choices( |
|
| 109 |
#' data = data[["ADSL"]], c("BMRKR1", "AGE", "EOSDY")
|
|
| 110 |
#' ), |
|
| 111 |
#' selected = c("BMRKR1", "AGE"),
|
|
| 112 |
#' multiple = TRUE |
|
| 113 |
#' ), |
|
| 114 |
#' filter = NULL |
|
| 115 |
#' ) |
|
| 116 |
#' ) |
|
| 117 |
#' ) |
|
| 118 |
#' ) |
|
| 119 |
#' if (interactive()) {
|
|
| 120 |
#' shinyApp(app$ui, app$server) |
|
| 121 |
#' } |
|
| 122 |
#' |
|
| 123 |
#' @export |
|
| 124 |
#' |
|
| 125 |
tm_a_pca <- function(label = "Principal Component Analysis", |
|
| 126 |
dat, |
|
| 127 |
plot_height = c(600, 200, 2000), |
|
| 128 |
plot_width = NULL, |
|
| 129 |
ggtheme = c("gray", "bw", "linedraw", "light", "dark", "minimal", "classic", "void"),
|
|
| 130 |
ggplot2_args = teal.widgets::ggplot2_args(), |
|
| 131 |
rotate_xaxis_labels = FALSE, |
|
| 132 |
font_size = c(12, 8, 20), |
|
| 133 |
alpha = c(1, 0, 1), |
|
| 134 |
size = c(2, 1, 8), |
|
| 135 |
pre_output = NULL, |
|
| 136 |
post_output = NULL, |
|
| 137 |
transformators = list(), |
|
| 138 |
decorators = list()) {
|
|
| 139 | ! |
message("Initializing tm_a_pca")
|
| 140 | ||
| 141 |
# Normalize the parameters |
|
| 142 | ! |
if (inherits(dat, "data_extract_spec")) dat <- list(dat) |
| 143 | ! |
if (inherits(ggplot2_args, "ggplot2_args")) ggplot2_args <- list(default = ggplot2_args) |
| 144 | ||
| 145 |
# Start of assertions |
|
| 146 | ! |
checkmate::assert_string(label) |
| 147 | ! |
checkmate::assert_list(dat, types = "data_extract_spec") |
| 148 | ||
| 149 | ! |
checkmate::assert_numeric(plot_height, len = 3, any.missing = FALSE, finite = TRUE) |
| 150 | ! |
checkmate::assert_numeric(plot_height[1], lower = plot_height[2], upper = plot_height[3], .var.name = "plot_height") |
| 151 | ! |
checkmate::assert_numeric(plot_width, len = 3, any.missing = FALSE, null.ok = TRUE, finite = TRUE) |
| 152 | ! |
checkmate::assert_numeric( |
| 153 | ! |
plot_width[1], |
| 154 | ! |
lower = plot_width[2], upper = plot_width[3], null.ok = TRUE, .var.name = "plot_width" |
| 155 |
) |
|
| 156 | ||
| 157 | ! |
ggtheme <- match.arg(ggtheme) |
| 158 | ||
| 159 | ! |
plot_choices <- c("Elbow plot", "Circle plot", "Biplot", "Eigenvector plot")
|
| 160 | ! |
checkmate::assert_list(ggplot2_args, types = "ggplot2_args") |
| 161 | ! |
checkmate::assert_subset(names(ggplot2_args), c("default", plot_choices))
|
| 162 | ||
| 163 | ! |
checkmate::assert_flag(rotate_xaxis_labels) |
| 164 | ||
| 165 | ! |
if (length(font_size) == 1) {
|
| 166 | ! |
checkmate::assert_numeric(font_size, any.missing = FALSE, finite = TRUE, lower = 8, upper = 20) |
| 167 |
} else {
|
|
| 168 | ! |
checkmate::assert_numeric(font_size, len = 3, any.missing = FALSE, finite = TRUE, lower = 8, upper = 20) |
| 169 | ! |
checkmate::assert_numeric(font_size[1], lower = font_size[2], upper = font_size[3], .var.name = "font_size") |
| 170 |
} |
|
| 171 | ||
| 172 | ! |
if (length(alpha) == 1) {
|
| 173 | ! |
checkmate::assert_numeric(alpha, any.missing = FALSE, finite = TRUE, lower = 0, upper = 1) |
| 174 |
} else {
|
|
| 175 | ! |
checkmate::assert_numeric(alpha, len = 3, any.missing = FALSE, finite = TRUE, lower = 0, upper = 1) |
| 176 | ! |
checkmate::assert_numeric(alpha[1], lower = alpha[2], upper = alpha[3], .var.name = "alpha") |
| 177 |
} |
|
| 178 | ||
| 179 | ! |
if (length(size) == 1) {
|
| 180 | ! |
checkmate::assert_numeric(size, any.missing = FALSE, finite = TRUE, lower = 1, upper = 8) |
| 181 |
} else {
|
|
| 182 | ! |
checkmate::assert_numeric(size, len = 3, any.missing = FALSE, finite = TRUE, lower = 1, upper = 8) |
| 183 | ! |
checkmate::assert_numeric(size[1], lower = size[2], upper = size[3], .var.name = "size") |
| 184 |
} |
|
| 185 | ||
| 186 | ! |
checkmate::assert_multi_class(pre_output, c("shiny.tag", "shiny.tag.list", "html"), null.ok = TRUE)
|
| 187 | ! |
checkmate::assert_multi_class(post_output, c("shiny.tag", "shiny.tag.list", "html"), null.ok = TRUE)
|
| 188 | ||
| 189 | ! |
available_decorators <- c("elbow_plot", "circle_plot", "biplot", "eigenvector_plot")
|
| 190 | ! |
assert_decorators(decorators, available_decorators) |
| 191 | ||
| 192 |
# Make UI args |
|
| 193 | ! |
args <- as.list(environment()) |
| 194 | ||
| 195 | ! |
data_extract_list <- list(dat = dat) |
| 196 | ||
| 197 | ! |
ans <- module( |
| 198 | ! |
label = label, |
| 199 | ! |
server = srv_a_pca, |
| 200 | ! |
ui = ui_a_pca, |
| 201 | ! |
ui_args = args, |
| 202 | ! |
server_args = c( |
| 203 | ! |
data_extract_list, |
| 204 | ! |
list( |
| 205 | ! |
plot_height = plot_height, |
| 206 | ! |
plot_width = plot_width, |
| 207 | ! |
ggplot2_args = ggplot2_args, |
| 208 | ! |
decorators = decorators |
| 209 |
) |
|
| 210 |
), |
|
| 211 | ! |
transformators = transformators, |
| 212 | ! |
datanames = teal.transform::get_extract_datanames(data_extract_list) |
| 213 |
) |
|
| 214 | ! |
attr(ans, "teal_bookmarkable") <- FALSE |
| 215 | ! |
ans |
| 216 |
} |
|
| 217 | ||
| 218 |
# UI function for the PCA module |
|
| 219 |
ui_a_pca <- function(id, ...) {
|
|
| 220 | ! |
ns <- NS(id) |
| 221 | ! |
args <- list(...) |
| 222 | ! |
is_single_dataset_value <- teal.transform::is_single_dataset(args$dat) |
| 223 | ||
| 224 | ! |
color_selector <- args$dat |
| 225 | ! |
for (i in seq_along(color_selector)) {
|
| 226 | ! |
color_selector[[i]]$select$multiple <- FALSE |
| 227 | ! |
color_selector[[i]]$select$always_selected <- NULL |
| 228 | ! |
color_selector[[i]]$select$selected <- NULL |
| 229 |
} |
|
| 230 | ||
| 231 | ! |
tagList( |
| 232 | ! |
teal.widgets::standard_layout( |
| 233 | ! |
output = teal.widgets::white_small_well( |
| 234 | ! |
uiOutput(ns("all_plots"))
|
| 235 |
), |
|
| 236 | ! |
encoding = tags$div( |
| 237 |
### Reporter |
|
| 238 | ! |
teal.reporter::add_card_button_ui(ns("add_reporter"), label = "Add Report Card"),
|
| 239 | ! |
tags$br(), tags$br(), |
| 240 |
### |
|
| 241 | ! |
tags$label("Encodings", class = "text-primary"),
|
| 242 | ! |
teal.transform::datanames_input(args["dat"]), |
| 243 | ! |
teal.transform::data_extract_ui( |
| 244 | ! |
id = ns("dat"),
|
| 245 | ! |
label = "Data selection", |
| 246 | ! |
data_extract_spec = args$dat, |
| 247 | ! |
is_single_dataset = is_single_dataset_value |
| 248 |
), |
|
| 249 | ! |
bslib::accordion( |
| 250 | ! |
open = TRUE, |
| 251 | ! |
bslib::accordion_panel( |
| 252 | ! |
title = "Display", |
| 253 | ! |
checkboxGroupInput( |
| 254 | ! |
ns("tables_display"),
|
| 255 | ! |
"Tables display", |
| 256 | ! |
choices = c("PC importance" = "importance", "Eigenvectors" = "eigenvector"),
|
| 257 | ! |
selected = c("importance", "eigenvector")
|
| 258 |
), |
|
| 259 | ! |
radioButtons( |
| 260 | ! |
ns("plot_type"),
|
| 261 | ! |
label = "Plot type", |
| 262 | ! |
choices = args$plot_choices, |
| 263 | ! |
selected = args$plot_choices[1] |
| 264 |
), |
|
| 265 | ! |
conditionalPanel( |
| 266 | ! |
condition = sprintf("input['%s'] == 'Elbow plot'", ns("plot_type")),
|
| 267 | ! |
ui_decorate_teal_data( |
| 268 | ! |
ns("d_elbow_plot"),
|
| 269 | ! |
decorators = select_decorators(args$decorators, "elbow_plot") |
| 270 |
) |
|
| 271 |
), |
|
| 272 | ! |
conditionalPanel( |
| 273 | ! |
condition = sprintf("input['%s'] == 'Circle plot'", ns("plot_type")),
|
| 274 | ! |
ui_decorate_teal_data( |
| 275 | ! |
ns("d_circle_plot"),
|
| 276 | ! |
decorators = select_decorators(args$decorators, "circle_plot") |
| 277 |
) |
|
| 278 |
), |
|
| 279 | ! |
conditionalPanel( |
| 280 | ! |
condition = sprintf("input['%s'] == 'Biplot'", ns("plot_type")),
|
| 281 | ! |
ui_decorate_teal_data( |
| 282 | ! |
ns("d_biplot"),
|
| 283 | ! |
decorators = select_decorators(args$decorators, "biplot") |
| 284 |
) |
|
| 285 |
), |
|
| 286 | ! |
conditionalPanel( |
| 287 | ! |
condition = sprintf("input['%s'] == 'Eigenvector plot'", ns("plot_type")),
|
| 288 | ! |
ui_decorate_teal_data( |
| 289 | ! |
ns("d_eigenvector_plot"),
|
| 290 | ! |
decorators = select_decorators(args$decorators, "eigenvector_plot") |
| 291 |
) |
|
| 292 |
) |
|
| 293 |
), |
|
| 294 | ! |
bslib::accordion_panel( |
| 295 | ! |
title = "Pre-processing", |
| 296 | ! |
radioButtons( |
| 297 | ! |
ns("standardization"), "Standardization",
|
| 298 | ! |
choices = c("None" = "none", "Center" = "center", "Center & Scale" = "center_scale"),
|
| 299 | ! |
selected = "center_scale" |
| 300 |
), |
|
| 301 | ! |
radioButtons( |
| 302 | ! |
ns("na_action"), "NA action",
|
| 303 | ! |
choices = c("None" = "none", "Drop" = "drop"),
|
| 304 | ! |
selected = "none" |
| 305 |
) |
|
| 306 |
), |
|
| 307 | ! |
bslib::accordion_panel( |
| 308 | ! |
title = "Selected plot specific settings", |
| 309 | ! |
uiOutput(ns("plot_settings")),
|
| 310 | ! |
conditionalPanel( |
| 311 | ! |
condition = sprintf("input['%s'] == 'Biplot'", ns("plot_type")),
|
| 312 | ! |
list( |
| 313 | ! |
teal.transform::data_extract_ui( |
| 314 | ! |
id = ns("response"),
|
| 315 | ! |
label = "Color by", |
| 316 | ! |
data_extract_spec = color_selector, |
| 317 | ! |
is_single_dataset = is_single_dataset_value |
| 318 |
), |
|
| 319 | ! |
teal.widgets::optionalSliderInputValMinMax(ns("alpha"), "Opacity:", args$alpha, ticks = FALSE),
|
| 320 | ! |
teal.widgets::optionalSliderInputValMinMax(ns("size"), "Points size:", args$size, ticks = FALSE)
|
| 321 |
) |
|
| 322 |
) |
|
| 323 |
), |
|
| 324 | ! |
bslib::accordion_panel( |
| 325 | ! |
title = "Plot settings", |
| 326 | ! |
collapsed = TRUE, |
| 327 | ! |
conditionalPanel( |
| 328 | ! |
condition = sprintf( |
| 329 | ! |
"input['%s'] == 'Elbow plot' || input['%s'] == 'Eigenvector plot'", |
| 330 | ! |
ns("plot_type"),
|
| 331 | ! |
ns("plot_type")
|
| 332 |
), |
|
| 333 | ! |
list(checkboxInput(ns("rotate_xaxis_labels"), "Rotate X axis labels", value = args$rotate_xaxis_labels))
|
| 334 |
), |
|
| 335 | ! |
selectInput( |
| 336 | ! |
inputId = ns("ggtheme"),
|
| 337 | ! |
label = "Theme (by ggplot):", |
| 338 | ! |
choices = ggplot_themes, |
| 339 | ! |
selected = args$ggtheme, |
| 340 | ! |
multiple = FALSE |
| 341 |
), |
|
| 342 | ! |
teal.widgets::optionalSliderInputValMinMax(ns("font_size"), "Font Size", args$font_size, ticks = FALSE)
|
| 343 |
) |
|
| 344 |
) |
|
| 345 |
), |
|
| 346 | ! |
forms = tagList( |
| 347 | ! |
teal.widgets::verbatim_popup_ui(ns("rcode"), "Show R code")
|
| 348 |
), |
|
| 349 | ! |
pre_output = args$pre_output, |
| 350 | ! |
post_output = args$post_output |
| 351 |
) |
|
| 352 |
) |
|
| 353 |
} |
|
| 354 | ||
| 355 |
# Server function for the PCA module |
|
| 356 |
srv_a_pca <- function(id, data, reporter, filter_panel_api, dat, plot_height, plot_width, ggplot2_args, decorators) {
|
|
| 357 | ! |
with_reporter <- !missing(reporter) && inherits(reporter, "Reporter") |
| 358 | ! |
with_filter <- !missing(filter_panel_api) && inherits(filter_panel_api, "FilterPanelAPI") |
| 359 | ! |
checkmate::assert_class(data, "reactive") |
| 360 | ! |
checkmate::assert_class(isolate(data()), "teal_data") |
| 361 | ! |
moduleServer(id, function(input, output, session) {
|
| 362 | ! |
teal.logger::log_shiny_input_changes(input, namespace = "teal.modules.general") |
| 363 | ||
| 364 | ! |
response <- dat |
| 365 | ||
| 366 | ! |
for (i in seq_along(response)) {
|
| 367 | ! |
response[[i]]$select$multiple <- FALSE |
| 368 | ! |
response[[i]]$select$always_selected <- NULL |
| 369 | ! |
response[[i]]$select$selected <- NULL |
| 370 | ! |
all_cols <- teal.data::col_labels(isolate(data())[[response[[i]]$dataname]]) |
| 371 | ! |
ignore_cols <- unlist(teal.data::join_keys(isolate(data()))[[response[[i]]$dataname]]) |
| 372 | ! |
color_cols <- all_cols[!names(all_cols) %in% ignore_cols] |
| 373 | ! |
response[[i]]$select$choices <- teal.transform::choices_labeled(names(color_cols), color_cols) |
| 374 |
} |
|
| 375 | ||
| 376 | ! |
selector_list <- teal.transform::data_extract_multiple_srv( |
| 377 | ! |
data_extract = list(dat = dat, response = response), |
| 378 | ! |
datasets = data, |
| 379 | ! |
select_validation_rule = list( |
| 380 | ! |
dat = ~ if (length(.) < 2L) "Please select more than 1 variable to perform PCA.", |
| 381 | ! |
response = shinyvalidate::compose_rules( |
| 382 | ! |
shinyvalidate::sv_optional(), |
| 383 | ! |
~ if (isTRUE(is.element(., selector_list()$dat()$select))) {
|
| 384 | ! |
"Response must not have been used for PCA." |
| 385 |
} |
|
| 386 |
) |
|
| 387 |
) |
|
| 388 |
) |
|
| 389 | ||
| 390 | ! |
iv_r <- reactive({
|
| 391 | ! |
iv <- shinyvalidate::InputValidator$new() |
| 392 | ! |
teal.transform::compose_and_enable_validators(iv, selector_list) |
| 393 |
}) |
|
| 394 | ||
| 395 | ! |
iv_extra <- shinyvalidate::InputValidator$new() |
| 396 | ! |
iv_extra$add_rule("x_axis", function(value) {
|
| 397 | ! |
if (isTRUE(input$plot_type %in% c("Circle plot", "Biplot"))) {
|
| 398 | ! |
if (!shinyvalidate::input_provided(value)) {
|
| 399 | ! |
"Need X axis" |
| 400 |
} |
|
| 401 |
} |
|
| 402 |
}) |
|
| 403 | ! |
iv_extra$add_rule("y_axis", function(value) {
|
| 404 | ! |
if (isTRUE(input$plot_type %in% c("Circle plot", "Biplot"))) {
|
| 405 | ! |
if (!shinyvalidate::input_provided(value)) {
|
| 406 | ! |
"Need Y axis" |
| 407 |
} |
|
| 408 |
} |
|
| 409 |
}) |
|
| 410 | ! |
rule_dupl <- function(...) {
|
| 411 | ! |
if (isTRUE(input$plot_type %in% c("Circle plot", "Biplot"))) {
|
| 412 | ! |
if (isTRUE(input$x_axis == input$y_axis)) {
|
| 413 | ! |
"Please choose different X and Y axes." |
| 414 |
} |
|
| 415 |
} |
|
| 416 |
} |
|
| 417 | ! |
iv_extra$add_rule("x_axis", rule_dupl)
|
| 418 | ! |
iv_extra$add_rule("y_axis", rule_dupl)
|
| 419 | ! |
iv_extra$add_rule("variables", function(value) {
|
| 420 | ! |
if (identical(input$plot_type, "Circle plot")) {
|
| 421 | ! |
if (!shinyvalidate::input_provided(value)) {
|
| 422 | ! |
"Need Original Coordinates" |
| 423 |
} |
|
| 424 |
} |
|
| 425 |
}) |
|
| 426 | ! |
iv_extra$add_rule("pc", function(value) {
|
| 427 | ! |
if (identical(input$plot_type, "Eigenvector plot")) {
|
| 428 | ! |
if (!shinyvalidate::input_provided(value)) {
|
| 429 | ! |
"Need PC" |
| 430 |
} |
|
| 431 |
} |
|
| 432 |
}) |
|
| 433 | ! |
iv_extra$enable() |
| 434 | ||
| 435 | ! |
anl_merged_input <- teal.transform::merge_expression_srv( |
| 436 | ! |
selector_list = selector_list, |
| 437 | ! |
datasets = data |
| 438 |
) |
|
| 439 | ! |
qenv <- reactive( |
| 440 | ! |
teal.code::eval_code(data(), 'library("ggplot2");library("dplyr");library("tidyr")') # nolint quotes
|
| 441 |
) |
|
| 442 | ! |
anl_merged_q <- reactive({
|
| 443 | ! |
req(anl_merged_input()) |
| 444 | ! |
qenv() %>% |
| 445 | ! |
teal.code::eval_code(as.expression(anl_merged_input()$expr)) |
| 446 |
}) |
|
| 447 | ||
| 448 | ! |
merged <- list( |
| 449 | ! |
anl_input_r = anl_merged_input, |
| 450 | ! |
anl_q_r = anl_merged_q |
| 451 |
) |
|
| 452 | ||
| 453 | ! |
validation <- reactive({
|
| 454 | ! |
req(merged$anl_q_r()) |
| 455 |
# inputs |
|
| 456 | ! |
keep_cols <- as.character(merged$anl_input_r()$columns_source$dat) |
| 457 | ! |
na_action <- input$na_action |
| 458 | ! |
standardization <- input$standardization |
| 459 | ! |
center <- standardization %in% c("center", "center_scale")
|
| 460 | ! |
scale <- standardization == "center_scale" |
| 461 | ! |
ANL <- merged$anl_q_r()[["ANL"]] |
| 462 | ||
| 463 | ! |
teal::validate_has_data(ANL, 10) |
| 464 | ! |
validate(need( |
| 465 | ! |
na_action != "none" | !anyNA(ANL[keep_cols]), |
| 466 | ! |
paste( |
| 467 | ! |
"There are NAs in the dataset. Please deal with them in preprocessing", |
| 468 | ! |
"or select \"Drop\" in the NA actions inside the encodings panel (left)." |
| 469 |
) |
|
| 470 |
)) |
|
| 471 | ! |
if (scale) {
|
| 472 | ! |
not_single <- vapply(ANL[keep_cols], function(column) length(unique(column)) != 1, FUN.VALUE = logical(1)) |
| 473 | ||
| 474 | ! |
msg <- paste0( |
| 475 | ! |
"You have selected `Center & Scale` under `Standardization` in the `Pre-processing` panel, ", |
| 476 | ! |
"but one or more of your columns has/have a variance value of zero, indicating all values are identical" |
| 477 |
) |
|
| 478 | ! |
validate(need(all(not_single), msg)) |
| 479 |
} |
|
| 480 |
}) |
|
| 481 | ||
| 482 |
# computation ---- |
|
| 483 | ! |
computation <- reactive({
|
| 484 | ! |
validation() |
| 485 | ||
| 486 |
# inputs |
|
| 487 | ! |
keep_cols <- as.character(merged$anl_input_r()$columns_source$dat) |
| 488 | ! |
na_action <- input$na_action |
| 489 | ! |
standardization <- input$standardization |
| 490 | ! |
center <- standardization %in% c("center", "center_scale")
|
| 491 | ! |
scale <- standardization == "center_scale" |
| 492 | ! |
ANL <- merged$anl_q_r()[["ANL"]] |
| 493 | ||
| 494 | ! |
qenv <- teal.code::eval_code( |
| 495 | ! |
merged$anl_q_r(), |
| 496 | ! |
substitute( |
| 497 | ! |
expr = keep_columns <- keep_cols, |
| 498 | ! |
env = list(keep_cols = keep_cols) |
| 499 |
) |
|
| 500 |
) |
|
| 501 | ||
| 502 | ! |
if (na_action == "drop") {
|
| 503 | ! |
qenv <- teal.code::eval_code( |
| 504 | ! |
qenv, |
| 505 | ! |
quote(ANL <- tidyr::drop_na(ANL, keep_columns)) |
| 506 |
) |
|
| 507 |
} |
|
| 508 | ||
| 509 | ! |
qenv <- teal.code::eval_code( |
| 510 | ! |
qenv, |
| 511 | ! |
substitute( |
| 512 | ! |
expr = pca <- summary(stats::prcomp(ANL[keep_columns], center = center, scale. = scale, retx = TRUE)), |
| 513 | ! |
env = list(center = center, scale = scale) |
| 514 |
) |
|
| 515 |
) |
|
| 516 | ||
| 517 | ! |
qenv <- teal.code::eval_code( |
| 518 | ! |
qenv, |
| 519 | ! |
quote({
|
| 520 | ! |
tbl_importance <- dplyr::as_tibble(pca$importance, rownames = "Metric") |
| 521 | ! |
tbl_importance |
| 522 |
}) |
|
| 523 |
) |
|
| 524 | ||
| 525 | ! |
teal.code::eval_code( |
| 526 | ! |
qenv, |
| 527 | ! |
quote({
|
| 528 | ! |
tbl_eigenvector <- dplyr::as_tibble(pca$rotation, rownames = "Variable") |
| 529 | ! |
tbl_eigenvector |
| 530 |
}) |
|
| 531 |
) |
|
| 532 |
}) |
|
| 533 | ||
| 534 |
# plot args ---- |
|
| 535 | ! |
output$plot_settings <- renderUI({
|
| 536 |
# reactivity triggers |
|
| 537 | ! |
req(iv_r()$is_valid()) |
| 538 | ! |
req(computation()) |
| 539 | ! |
qenv <- computation() |
| 540 | ||
| 541 | ! |
ns <- session$ns |
| 542 | ||
| 543 | ! |
pca <- qenv[["pca"]] |
| 544 | ! |
chcs_pcs <- colnames(pca$rotation) |
| 545 | ! |
chcs_vars <- qenv[["keep_columns"]] |
| 546 | ||
| 547 | ! |
tagList( |
| 548 | ! |
conditionalPanel( |
| 549 | ! |
condition = sprintf( |
| 550 | ! |
"input['%s'] == 'Biplot' || input['%s'] == 'Circle plot'", |
| 551 | ! |
ns("plot_type"), ns("plot_type")
|
| 552 |
), |
|
| 553 | ! |
list( |
| 554 | ! |
teal.widgets::optionalSelectInput(ns("x_axis"), "X axis", choices = chcs_pcs, selected = chcs_pcs[1]),
|
| 555 | ! |
teal.widgets::optionalSelectInput(ns("y_axis"), "Y axis", choices = chcs_pcs, selected = chcs_pcs[2]),
|
| 556 | ! |
teal.widgets::optionalSelectInput( |
| 557 | ! |
ns("variables"), "Original coordinates",
|
| 558 | ! |
choices = chcs_vars, selected = chcs_vars, |
| 559 | ! |
multiple = TRUE |
| 560 |
) |
|
| 561 |
) |
|
| 562 |
), |
|
| 563 | ! |
conditionalPanel( |
| 564 | ! |
condition = sprintf("input['%s'] == 'Elbow plot'", ns("plot_type")),
|
| 565 | ! |
helpText("No plot specific settings available.")
|
| 566 |
), |
|
| 567 | ! |
conditionalPanel( |
| 568 | ! |
condition = paste0("input['", ns("plot_type"), "'] == 'Eigenvector plot'"),
|
| 569 | ! |
teal.widgets::optionalSelectInput(ns("pc"), "PC", choices = chcs_pcs, selected = chcs_pcs[1])
|
| 570 |
) |
|
| 571 |
) |
|
| 572 |
}) |
|
| 573 | ||
| 574 |
# plot elbow ---- |
|
| 575 | ! |
plot_elbow <- function(base_q) {
|
| 576 | ! |
ggtheme <- input$ggtheme |
| 577 | ! |
rotate_xaxis_labels <- input$rotate_xaxis_labels |
| 578 | ! |
font_size <- input$font_size |
| 579 | ||
| 580 | ! |
angle_value <- ifelse(isTRUE(rotate_xaxis_labels), 45, 0) |
| 581 | ! |
hjust_value <- ifelse(isTRUE(rotate_xaxis_labels), 1, 0.5) |
| 582 | ||
| 583 | ! |
dev_ggplot2_args <- teal.widgets::ggplot2_args( |
| 584 | ! |
labs = list(x = "Principal component", y = "Proportion of variance explained", color = "", fill = "Legend"), |
| 585 | ! |
theme = list( |
| 586 | ! |
legend.position = "right", |
| 587 | ! |
legend.spacing.y = quote(grid::unit(-5, "pt")), |
| 588 | ! |
legend.title = quote(ggplot2::element_text(vjust = 25)), |
| 589 | ! |
axis.text.x = substitute( |
| 590 | ! |
ggplot2::element_text(angle = angle_value, hjust = hjust_value), |
| 591 | ! |
list(angle_value = angle_value, hjust_value = hjust_value) |
| 592 |
), |
|
| 593 | ! |
text = substitute(ggplot2::element_text(size = font_size), list(font_size = font_size)) |
| 594 |
) |
|
| 595 |
) |
|
| 596 | ||
| 597 | ! |
parsed_ggplot2_args <- teal.widgets::parse_ggplot2_args( |
| 598 | ! |
teal.widgets::resolve_ggplot2_args( |
| 599 | ! |
user_plot = ggplot2_args[["Elbow plot"]], |
| 600 | ! |
user_default = ggplot2_args$default, |
| 601 | ! |
module_plot = dev_ggplot2_args |
| 602 |
), |
|
| 603 | ! |
ggtheme = ggtheme |
| 604 |
) |
|
| 605 | ||
| 606 | ! |
teal.code::eval_code( |
| 607 | ! |
base_q, |
| 608 | ! |
substitute( |
| 609 | ! |
expr = {
|
| 610 | ! |
elb_dat <- pca$importance[c("Proportion of Variance", "Cumulative Proportion"), ] %>%
|
| 611 | ! |
dplyr::as_tibble(rownames = "metric") %>% |
| 612 | ! |
tidyr::gather("component", "value", -metric) %>%
|
| 613 | ! |
dplyr::mutate( |
| 614 | ! |
component = factor(component, levels = unique(stringr::str_sort(component, numeric = TRUE))) |
| 615 |
) |
|
| 616 | ||
| 617 | ! |
cols <- c(getOption("ggplot2.discrete.colour"), c("lightblue", "darkred", "black"))[1:3]
|
| 618 | ! |
elbow_plot <- ggplot2::ggplot(mapping = ggplot2::aes_string(x = "component", y = "value")) + |
| 619 | ! |
ggplot2::geom_bar( |
| 620 | ! |
ggplot2::aes(fill = "Single variance"), |
| 621 | ! |
data = dplyr::filter(elb_dat, metric == "Proportion of Variance"), |
| 622 | ! |
color = "black", |
| 623 | ! |
stat = "identity" |
| 624 |
) + |
|
| 625 | ! |
ggplot2::geom_point( |
| 626 | ! |
ggplot2::aes(color = "Cumulative variance"), |
| 627 | ! |
data = dplyr::filter(elb_dat, metric == "Cumulative Proportion") |
| 628 |
) + |
|
| 629 | ! |
ggplot2::geom_line( |
| 630 | ! |
ggplot2::aes(group = 1, color = "Cumulative variance"), |
| 631 | ! |
data = dplyr::filter(elb_dat, metric == "Cumulative Proportion") |
| 632 |
) + |
|
| 633 | ! |
labs + |
| 634 | ! |
ggplot2::scale_color_manual(values = c("Cumulative variance" = cols[2], "Single variance" = cols[3])) +
|
| 635 | ! |
ggplot2::scale_fill_manual(values = c("Cumulative variance" = cols[2], "Single variance" = cols[1])) +
|
| 636 | ! |
ggthemes + |
| 637 | ! |
themes |
| 638 |
}, |
|
| 639 | ! |
env = list( |
| 640 | ! |
ggthemes = parsed_ggplot2_args$ggtheme, |
| 641 | ! |
labs = parsed_ggplot2_args$labs, |
| 642 | ! |
themes = parsed_ggplot2_args$theme |
| 643 |
) |
|
| 644 |
) |
|
| 645 |
) |
|
| 646 |
} |
|
| 647 | ||
| 648 |
# plot circle ---- |
|
| 649 | ! |
plot_circle <- function(base_q) {
|
| 650 | ! |
x_axis <- input$x_axis |
| 651 | ! |
y_axis <- input$y_axis |
| 652 | ! |
variables <- input$variables |
| 653 | ! |
ggtheme <- input$ggtheme |
| 654 | ||
| 655 | ! |
rotate_xaxis_labels <- input$rotate_xaxis_labels |
| 656 | ! |
font_size <- input$font_size |
| 657 | ||
| 658 | ! |
angle <- ifelse(isTRUE(rotate_xaxis_labels), 45, 0) |
| 659 | ! |
hjust <- ifelse(isTRUE(rotate_xaxis_labels), 1, 0.5) |
| 660 | ||
| 661 | ! |
dev_ggplot2_args <- teal.widgets::ggplot2_args( |
| 662 | ! |
theme = list( |
| 663 | ! |
text = substitute(ggplot2::element_text(size = font_size), list(font_size = font_size)), |
| 664 | ! |
axis.text.x = substitute( |
| 665 | ! |
ggplot2::element_text(angle = angle_val, hjust = hjust_val), |
| 666 | ! |
list(angle_val = angle, hjust_val = hjust) |
| 667 |
) |
|
| 668 |
) |
|
| 669 |
) |
|
| 670 | ||
| 671 | ! |
all_ggplot2_args <- teal.widgets::resolve_ggplot2_args( |
| 672 | ! |
user_plot = ggplot2_args[["Circle plot"]], |
| 673 | ! |
user_default = ggplot2_args$default, |
| 674 | ! |
module_plot = dev_ggplot2_args |
| 675 |
) |
|
| 676 | ||
| 677 | ! |
parsed_ggplot2_args <- teal.widgets::parse_ggplot2_args( |
| 678 | ! |
all_ggplot2_args, |
| 679 | ! |
ggtheme = ggtheme |
| 680 |
) |
|
| 681 | ||
| 682 | ! |
teal.code::eval_code( |
| 683 | ! |
base_q, |
| 684 | ! |
substitute( |
| 685 | ! |
expr = {
|
| 686 | ! |
pca_rot <- pca$rotation[, c(x_axis, y_axis)] %>% |
| 687 | ! |
dplyr::as_tibble(rownames = "label") %>% |
| 688 | ! |
dplyr::filter(label %in% variables) |
| 689 | ||
| 690 | ! |
circle_data <- data.frame( |
| 691 | ! |
x = cos(seq(0, 2 * pi, length.out = 100)), |
| 692 | ! |
y = sin(seq(0, 2 * pi, length.out = 100)) |
| 693 |
) |
|
| 694 | ||
| 695 | ! |
circle_plot <- ggplot2::ggplot(pca_rot) + |
| 696 | ! |
ggplot2::geom_point(ggplot2::aes_string(x = x_axis, y = y_axis)) + |
| 697 | ! |
ggplot2::geom_label( |
| 698 | ! |
ggplot2::aes_string(x = x_axis, y = y_axis, label = "label"), |
| 699 | ! |
nudge_x = 0.1, nudge_y = 0.05, |
| 700 | ! |
fontface = "bold" |
| 701 |
) + |
|
| 702 | ! |
ggplot2::geom_path(ggplot2::aes(x, y, group = 1), data = circle_data) + |
| 703 | ! |
ggplot2::geom_point(ggplot2::aes(x = x, y = y), data = data.frame(x = 0, y = 0), shape = "x", size = 5) + |
| 704 | ! |
labs + |
| 705 | ! |
ggthemes + |
| 706 | ! |
themes |
| 707 |
}, |
|
| 708 | ! |
env = list( |
| 709 | ! |
x_axis = x_axis, |
| 710 | ! |
y_axis = y_axis, |
| 711 | ! |
variables = variables, |
| 712 | ! |
ggthemes = parsed_ggplot2_args$ggtheme, |
| 713 | ! |
labs = `if`(is.null(parsed_ggplot2_args$labs), quote(labs()), parsed_ggplot2_args$labs), |
| 714 | ! |
themes = parsed_ggplot2_args$theme |
| 715 |
) |
|
| 716 |
) |
|
| 717 |
) |
|
| 718 |
} |
|
| 719 | ||
| 720 |
# plot biplot ---- |
|
| 721 | ! |
plot_biplot <- function(base_q) {
|
| 722 | ! |
qenv <- base_q |
| 723 | ||
| 724 | ! |
ANL <- qenv[["ANL"]] |
| 725 | ||
| 726 | ! |
resp_col <- as.character(merged$anl_input_r()$columns_source$response) |
| 727 | ! |
dat_cols <- as.character(merged$anl_input_r()$columns_source$dat) |
| 728 | ! |
x_axis <- input$x_axis |
| 729 | ! |
y_axis <- input$y_axis |
| 730 | ! |
variables <- input$variables |
| 731 | ! |
pca <- qenv[["pca"]] |
| 732 | ||
| 733 | ! |
ggtheme <- input$ggtheme |
| 734 | ||
| 735 | ! |
rotate_xaxis_labels <- input$rotate_xaxis_labels |
| 736 | ! |
alpha <- input$alpha |
| 737 | ! |
size <- input$size |
| 738 | ! |
font_size <- input$font_size |
| 739 | ||
| 740 | ! |
qenv <- teal.code::eval_code( |
| 741 | ! |
qenv, |
| 742 | ! |
substitute( |
| 743 | ! |
expr = pca_rot <- dplyr::as_tibble(pca$x[, c(x_axis, y_axis)]), |
| 744 | ! |
env = list(x_axis = x_axis, y_axis = y_axis) |
| 745 |
) |
|
| 746 |
) |
|
| 747 | ||
| 748 |
# rot_vars = data frame that displays arrows in the plot, need to be scaled to data |
|
| 749 | ! |
if (!is.null(input$variables)) {
|
| 750 | ! |
qenv <- teal.code::eval_code( |
| 751 | ! |
qenv, |
| 752 | ! |
substitute( |
| 753 | ! |
expr = {
|
| 754 | ! |
r <- sqrt(qchisq(0.69, df = 2)) * prod(colMeans(pca_rot ^ 2)) ^ (1 / 4) # styler: off |
| 755 | ! |
v_scale <- rowSums(pca$rotation ^ 2) # styler: off |
| 756 | ||
| 757 | ! |
rot_vars <- pca$rotation[, c(x_axis, y_axis)] %>% |
| 758 | ! |
dplyr::as_tibble(rownames = "label") %>% |
| 759 | ! |
dplyr::mutate_at(vars(c(x_axis, y_axis)), function(x) r * x / sqrt(max(v_scale))) |
| 760 |
}, |
|
| 761 | ! |
env = list(x_axis = x_axis, y_axis = y_axis) |
| 762 |
) |
|
| 763 |
) %>% |
|
| 764 | ! |
teal.code::eval_code( |
| 765 | ! |
if (is.logical(pca$center) && !pca$center) {
|
| 766 | ! |
substitute( |
| 767 | ! |
expr = {
|
| 768 | ! |
rot_vars <- rot_vars %>% |
| 769 | ! |
tibble::column_to_rownames("label") %>%
|
| 770 | ! |
sweep(1, apply(ANL[keep_columns], 2, mean, na.rm = TRUE)) %>% |
| 771 | ! |
tibble::rownames_to_column("label") %>%
|
| 772 | ! |
dplyr::mutate( |
| 773 | ! |
xstart = mean(pca$x[, x_axis], na.rm = TRUE), |
| 774 | ! |
ystart = mean(pca$x[, y_axis], na.rm = TRUE) |
| 775 |
) |
|
| 776 |
}, |
|
| 777 | ! |
env = list(x_axis = x_axis, y_axis = y_axis) |
| 778 |
) |
|
| 779 |
} else {
|
|
| 780 | ! |
quote(rot_vars <- rot_vars %>% dplyr::mutate(xstart = 0, ystart = 0)) |
| 781 |
} |
|
| 782 |
) %>% |
|
| 783 | ! |
teal.code::eval_code( |
| 784 | ! |
substitute( |
| 785 | ! |
expr = rot_vars <- rot_vars %>% dplyr::filter(label %in% variables), |
| 786 | ! |
env = list(variables = variables) |
| 787 |
) |
|
| 788 |
) |
|
| 789 |
} |
|
| 790 | ||
| 791 | ! |
pca_plot_biplot_expr <- list(quote(ggplot())) |
| 792 | ||
| 793 | ! |
if (length(resp_col) == 0) {
|
| 794 | ! |
pca_plot_biplot_expr <- c( |
| 795 | ! |
pca_plot_biplot_expr, |
| 796 | ! |
substitute( |
| 797 | ! |
ggplot2::geom_point(ggplot2::aes_string(x = x_axis, y = y_axis), |
| 798 | ! |
data = pca_rot, alpha = alpha, size = size |
| 799 |
), |
|
| 800 | ! |
list(x_axis = input$x_axis, y_axis = input$y_axis, alpha = input$alpha, size = input$size) |
| 801 |
) |
|
| 802 |
) |
|
| 803 | ! |
dev_labs <- list() |
| 804 |
} else {
|
|
| 805 | ! |
rp_keys <- setdiff(colnames(ANL), as.character(unlist(merged$anl_input_r()$columns_source))) |
| 806 | ||
| 807 | ! |
response <- ANL[[resp_col]] |
| 808 | ||
| 809 | ! |
aes_biplot <- substitute( |
| 810 | ! |
ggplot2::aes_string(x = x_axis, y = y_axis, color = "response"), |
| 811 | ! |
env = list(x_axis = x_axis, y_axis = y_axis) |
| 812 |
) |
|
| 813 | ||
| 814 | ! |
qenv <- teal.code::eval_code( |
| 815 | ! |
qenv, |
| 816 | ! |
substitute(response <- ANL[[resp_col]], env = list(resp_col = resp_col)) |
| 817 |
) |
|
| 818 | ||
| 819 | ! |
dev_labs <- list(color = varname_w_label(resp_col, ANL)) |
| 820 | ||
| 821 | ! |
scales_biplot <- |
| 822 | ! |
if ( |
| 823 | ! |
is.character(response) || |
| 824 | ! |
is.factor(response) || |
| 825 | ! |
(is.numeric(response) && length(unique(response)) <= 6) |
| 826 |
) {
|
|
| 827 | ! |
qenv <- teal.code::eval_code( |
| 828 | ! |
qenv, |
| 829 | ! |
quote(pca_rot$response <- as.factor(response)) |
| 830 |
) |
|
| 831 | ! |
quote(ggplot2::scale_color_brewer(palette = "Dark2")) |
| 832 | ! |
} else if (inherits(response, "Date")) {
|
| 833 | ! |
qenv <- teal.code::eval_code( |
| 834 | ! |
qenv, |
| 835 | ! |
quote(pca_rot$response <- numeric(response)) |
| 836 |
) |
|
| 837 | ||
| 838 | ! |
quote( |
| 839 | ! |
ggplot2::scale_color_gradient( |
| 840 | ! |
low = c(getOption("ggplot2.discrete.colour")[2], "darkred")[1],
|
| 841 | ! |
high = c(getOption("ggplot2.discrete.colour"), "lightblue")[1],
|
| 842 | ! |
labels = function(x) as.Date(x, origin = "1970-01-01") |
| 843 |
) |
|
| 844 |
) |
|
| 845 |
} else {
|
|
| 846 | ! |
qenv <- teal.code::eval_code( |
| 847 | ! |
qenv, |
| 848 | ! |
quote(pca_rot$response <- response) |
| 849 |
) |
|
| 850 | ! |
quote(ggplot2::scale_color_gradient( |
| 851 | ! |
low = c(getOption("ggplot2.discrete.colour")[2], "darkred")[1],
|
| 852 | ! |
high = c(getOption("ggplot2.discrete.colour"), "lightblue")[1]
|
| 853 |
)) |
|
| 854 |
} |
|
| 855 | ||
| 856 | ! |
pca_plot_biplot_expr <- c( |
| 857 | ! |
pca_plot_biplot_expr, |
| 858 | ! |
substitute( |
| 859 | ! |
ggplot2::geom_point(aes_biplot, data = pca_rot, alpha = alpha, size = size), |
| 860 | ! |
env = list(aes_biplot = aes_biplot, alpha = alpha, size = size) |
| 861 |
), |
|
| 862 | ! |
scales_biplot |
| 863 |
) |
|
| 864 |
} |
|
| 865 | ||
| 866 | ! |
if (!is.null(input$variables)) {
|
| 867 | ! |
pca_plot_biplot_expr <- c( |
| 868 | ! |
pca_plot_biplot_expr, |
| 869 | ! |
substitute( |
| 870 | ! |
ggplot2::geom_segment( |
| 871 | ! |
ggplot2::aes_string(x = "xstart", y = "ystart", xend = x_axis, yend = y_axis), |
| 872 | ! |
data = rot_vars, |
| 873 | ! |
lineend = "round", linejoin = "round", |
| 874 | ! |
arrow = grid::arrow(length = grid::unit(0.5, "cm")) |
| 875 |
), |
|
| 876 | ! |
env = list(x_axis = x_axis, y_axis = y_axis) |
| 877 |
), |
|
| 878 | ! |
substitute( |
| 879 | ! |
ggplot2::geom_label( |
| 880 | ! |
ggplot2::aes_string( |
| 881 | ! |
x = x_axis, |
| 882 | ! |
y = y_axis, |
| 883 | ! |
label = "label" |
| 884 |
), |
|
| 885 | ! |
data = rot_vars, |
| 886 | ! |
nudge_y = 0.1, |
| 887 | ! |
fontface = "bold" |
| 888 |
), |
|
| 889 | ! |
env = list(x_axis = x_axis, y_axis = y_axis) |
| 890 |
), |
|
| 891 | ! |
quote(ggplot2::geom_point(ggplot2::aes(x = xstart, y = ystart), data = rot_vars, shape = "x", size = 5)) |
| 892 |
) |
|
| 893 |
} |
|
| 894 | ||
| 895 | ! |
angle <- ifelse(isTRUE(rotate_xaxis_labels), 45, 0) |
| 896 | ! |
hjust <- ifelse(isTRUE(rotate_xaxis_labels), 1, 0.5) |
| 897 | ||
| 898 | ! |
dev_ggplot2_args <- teal.widgets::ggplot2_args( |
| 899 | ! |
labs = dev_labs, |
| 900 | ! |
theme = list( |
| 901 | ! |
text = substitute(ggplot2::element_text(size = font_size), list(font_size = font_size)), |
| 902 | ! |
axis.text.x = substitute( |
| 903 | ! |
ggplot2::element_text(angle = angle_val, hjust = hjust_val), |
| 904 | ! |
list(angle_val = angle, hjust_val = hjust) |
| 905 |
) |
|
| 906 |
) |
|
| 907 |
) |
|
| 908 | ||
| 909 | ! |
all_ggplot2_args <- teal.widgets::resolve_ggplot2_args( |
| 910 | ! |
user_plot = ggplot2_args[["Biplot"]], |
| 911 | ! |
user_default = ggplot2_args$default, |
| 912 | ! |
module_plot = dev_ggplot2_args |
| 913 |
) |
|
| 914 | ||
| 915 | ! |
parsed_ggplot2_args <- teal.widgets::parse_ggplot2_args( |
| 916 | ! |
all_ggplot2_args, |
| 917 | ! |
ggtheme = ggtheme |
| 918 |
) |
|
| 919 | ||
| 920 | ! |
pca_plot_biplot_expr <- c( |
| 921 | ! |
pca_plot_biplot_expr, |
| 922 | ! |
parsed_ggplot2_args |
| 923 |
) |
|
| 924 | ||
| 925 | ! |
teal.code::eval_code( |
| 926 | ! |
qenv, |
| 927 | ! |
substitute( |
| 928 | ! |
expr = {
|
| 929 | ! |
biplot <- plot_call |
| 930 |
}, |
|
| 931 | ! |
env = list( |
| 932 | ! |
plot_call = Reduce(function(x, y) call("+", x, y), pca_plot_biplot_expr)
|
| 933 |
) |
|
| 934 |
) |
|
| 935 |
) |
|
| 936 |
} |
|
| 937 | ||
| 938 |
# plot eigenvector_plot ---- |
|
| 939 | ! |
plot_eigenvector <- function(base_q) {
|
| 940 | ! |
req(input$pc) |
| 941 | ! |
pc <- input$pc |
| 942 | ! |
ggtheme <- input$ggtheme |
| 943 | ||
| 944 | ! |
rotate_xaxis_labels <- input$rotate_xaxis_labels |
| 945 | ! |
font_size <- input$font_size |
| 946 | ||
| 947 | ! |
angle <- ifelse(rotate_xaxis_labels, 45, 0) |
| 948 | ! |
hjust <- ifelse(rotate_xaxis_labels, 1, 0.5) |
| 949 | ||
| 950 | ! |
dev_ggplot2_args <- teal.widgets::ggplot2_args( |
| 951 | ! |
theme = list( |
| 952 | ! |
text = substitute(ggplot2::element_text(size = font_size), list(font_size = font_size)), |
| 953 | ! |
axis.text.x = substitute( |
| 954 | ! |
ggplot2::element_text(angle = angle_val, hjust = hjust_val), |
| 955 | ! |
list(angle_val = angle, hjust_val = hjust) |
| 956 |
) |
|
| 957 |
) |
|
| 958 |
) |
|
| 959 | ||
| 960 | ! |
all_ggplot2_args <- teal.widgets::resolve_ggplot2_args( |
| 961 | ! |
user_plot = ggplot2_args[["Eigenvector plot"]], |
| 962 | ! |
user_default = ggplot2_args$default, |
| 963 | ! |
module_plot = dev_ggplot2_args |
| 964 |
) |
|
| 965 | ||
| 966 | ! |
parsed_ggplot2_args <- teal.widgets::parse_ggplot2_args( |
| 967 | ! |
all_ggplot2_args, |
| 968 | ! |
ggtheme = ggtheme |
| 969 |
) |
|
| 970 | ||
| 971 | ! |
ggplot_exprs <- c( |
| 972 | ! |
list( |
| 973 | ! |
quote(ggplot(pca_rot)), |
| 974 | ! |
substitute( |
| 975 | ! |
ggplot2::geom_bar( |
| 976 | ! |
ggplot2::aes_string(x = "Variable", y = pc), |
| 977 | ! |
stat = "identity", |
| 978 | ! |
color = "black", |
| 979 | ! |
fill = c(getOption("ggplot2.discrete.colour"), "lightblue")[1]
|
| 980 |
), |
|
| 981 | ! |
env = list(pc = pc) |
| 982 |
), |
|
| 983 | ! |
substitute( |
| 984 | ! |
ggplot2::geom_text( |
| 985 | ! |
ggplot2::aes( |
| 986 | ! |
x = Variable, |
| 987 | ! |
y = pc_name, |
| 988 | ! |
label = round(pc_name, 3), |
| 989 | ! |
vjust = ifelse(pc_name > 0, -0.5, 1.3) |
| 990 |
) |
|
| 991 |
), |
|
| 992 | ! |
env = list(pc_name = as.name(pc)) |
| 993 |
) |
|
| 994 |
), |
|
| 995 | ! |
parsed_ggplot2_args$labs, |
| 996 | ! |
parsed_ggplot2_args$ggtheme, |
| 997 | ! |
parsed_ggplot2_args$theme |
| 998 |
) |
|
| 999 | ||
| 1000 | ! |
teal.code::eval_code( |
| 1001 | ! |
base_q, |
| 1002 | ! |
substitute( |
| 1003 | ! |
expr = {
|
| 1004 | ! |
pca_rot <- pca$rotation[, pc, drop = FALSE] %>% |
| 1005 | ! |
dplyr::as_tibble(rownames = "Variable") |
| 1006 | ! |
eigenvector_plot <- plot_call |
| 1007 |
}, |
|
| 1008 | ! |
env = list( |
| 1009 | ! |
pc = pc, |
| 1010 | ! |
plot_call = Reduce(function(x, y) call("+", x, y), ggplot_exprs)
|
| 1011 |
) |
|
| 1012 |
) |
|
| 1013 |
) |
|
| 1014 |
} |
|
| 1015 | ||
| 1016 |
# qenvs --- |
|
| 1017 | ! |
output_q <- lapply( |
| 1018 | ! |
list( |
| 1019 | ! |
elbow_plot = plot_elbow, |
| 1020 | ! |
circle_plot = plot_circle, |
| 1021 | ! |
biplot = plot_biplot, |
| 1022 | ! |
eigenvector_plot = plot_eigenvector |
| 1023 |
), |
|
| 1024 | ! |
function(fun) {
|
| 1025 | ! |
reactive({
|
| 1026 | ! |
req(computation()) |
| 1027 | ! |
teal::validate_inputs(iv_r()) |
| 1028 | ! |
teal::validate_inputs(iv_extra, header = "Plot settings are required") |
| 1029 | ! |
fun(computation()) |
| 1030 |
}) |
|
| 1031 |
} |
|
| 1032 |
) |
|
| 1033 | ||
| 1034 | ! |
decorated_q <- mapply( |
| 1035 | ! |
function(obj_name, q) {
|
| 1036 | ! |
srv_decorate_teal_data( |
| 1037 | ! |
id = sprintf("d_%s", obj_name),
|
| 1038 | ! |
data = q, |
| 1039 | ! |
decorators = select_decorators(decorators, obj_name), |
| 1040 | ! |
expr = reactive({
|
| 1041 | ! |
substitute(print(.plot), env = list(.plot = as.name(obj_name))) |
| 1042 |
}), |
|
| 1043 | ! |
expr_is_reactive = TRUE |
| 1044 |
) |
|
| 1045 |
}, |
|
| 1046 | ! |
names(output_q), |
| 1047 | ! |
output_q |
| 1048 |
) |
|
| 1049 | ||
| 1050 |
# plot final ---- |
|
| 1051 | ! |
decorated_output_q <- reactive({
|
| 1052 | ! |
switch(req(input$plot_type), |
| 1053 | ! |
"Elbow plot" = decorated_q$elbow_plot(), |
| 1054 | ! |
"Circle plot" = decorated_q$circle_plot(), |
| 1055 | ! |
"Biplot" = decorated_q$biplot(), |
| 1056 | ! |
"Eigenvector plot" = decorated_q$eigenvector_plot(), |
| 1057 | ! |
stop("Unknown plot")
|
| 1058 |
) |
|
| 1059 |
}) |
|
| 1060 | ||
| 1061 | ! |
plot_r <- reactive({
|
| 1062 | ! |
plot_name <- gsub(" ", "_", tolower(req(input$plot_type)))
|
| 1063 | ! |
req(decorated_output_q())[[plot_name]] |
| 1064 |
}) |
|
| 1065 | ||
| 1066 | ! |
pws <- teal.widgets::plot_with_settings_srv( |
| 1067 | ! |
id = "pca_plot", |
| 1068 | ! |
plot_r = plot_r, |
| 1069 | ! |
height = plot_height, |
| 1070 | ! |
width = plot_width, |
| 1071 | ! |
graph_align = "center" |
| 1072 |
) |
|
| 1073 | ||
| 1074 |
# tables ---- |
|
| 1075 | ! |
output$tbl_importance <- renderTable( |
| 1076 | ! |
expr = {
|
| 1077 | ! |
req("importance" %in% input$tables_display, computation())
|
| 1078 | ! |
computation()[["tbl_importance"]] |
| 1079 |
}, |
|
| 1080 | ! |
bordered = TRUE, |
| 1081 | ! |
align = "c", |
| 1082 | ! |
digits = 3 |
| 1083 |
) |
|
| 1084 | ||
| 1085 | ! |
output$tbl_importance_ui <- renderUI({
|
| 1086 | ! |
req("importance" %in% input$tables_display)
|
| 1087 | ! |
tags$div( |
| 1088 | ! |
align = "center", |
| 1089 | ! |
tags$h4("Principal components importance"),
|
| 1090 | ! |
tableOutput(session$ns("tbl_importance")),
|
| 1091 | ! |
tags$hr() |
| 1092 |
) |
|
| 1093 |
}) |
|
| 1094 | ||
| 1095 | ! |
output$tbl_eigenvector <- renderTable( |
| 1096 | ! |
expr = {
|
| 1097 | ! |
req("eigenvector" %in% input$tables_display, req(computation()))
|
| 1098 | ! |
computation()[["tbl_eigenvector"]] |
| 1099 |
}, |
|
| 1100 | ! |
bordered = TRUE, |
| 1101 | ! |
align = "c", |
| 1102 | ! |
digits = 3 |
| 1103 |
) |
|
| 1104 | ||
| 1105 | ! |
output$tbl_eigenvector_ui <- renderUI({
|
| 1106 | ! |
req("eigenvector" %in% input$tables_display)
|
| 1107 | ! |
tags$div( |
| 1108 | ! |
align = "center", |
| 1109 | ! |
tags$h4("Eigenvectors"),
|
| 1110 | ! |
tableOutput(session$ns("tbl_eigenvector")),
|
| 1111 | ! |
tags$hr() |
| 1112 |
) |
|
| 1113 |
}) |
|
| 1114 | ||
| 1115 | ! |
output$all_plots <- renderUI({
|
| 1116 | ! |
teal::validate_inputs(iv_r()) |
| 1117 | ! |
teal::validate_inputs(iv_extra, header = "Plot settings are required") |
| 1118 | ||
| 1119 | ! |
validation() |
| 1120 | ! |
tags$div( |
| 1121 | ! |
uiOutput(session$ns("tbl_importance_ui")),
|
| 1122 | ! |
uiOutput(session$ns("tbl_eigenvector_ui")),
|
| 1123 | ! |
teal.widgets::plot_with_settings_ui(id = session$ns("pca_plot"))
|
| 1124 |
) |
|
| 1125 |
}) |
|
| 1126 | ||
| 1127 |
# Render R code. |
|
| 1128 | ! |
source_code_r <- reactive(teal.code::get_code(req(decorated_output_q()))) |
| 1129 | ||
| 1130 | ! |
teal.widgets::verbatim_popup_srv( |
| 1131 | ! |
id = "rcode", |
| 1132 | ! |
verbatim_content = source_code_r, |
| 1133 | ! |
title = "R Code for PCA" |
| 1134 |
) |
|
| 1135 | ||
| 1136 |
### REPORTER |
|
| 1137 | ! |
if (with_reporter) {
|
| 1138 | ! |
card_fun <- function(comment, label) {
|
| 1139 | ! |
card <- teal::report_card_template( |
| 1140 | ! |
title = "Principal Component Analysis Plot", |
| 1141 | ! |
label = label, |
| 1142 | ! |
with_filter = with_filter, |
| 1143 | ! |
filter_panel_api = filter_panel_api |
| 1144 |
) |
|
| 1145 | ! |
card$append_text("Principal Components Table", "header3")
|
| 1146 | ! |
card$append_table(computation()[["tbl_importance"]]) |
| 1147 | ! |
card$append_text("Eigenvectors Table", "header3")
|
| 1148 | ! |
card$append_table(computation()[["tbl_eigenvector"]]) |
| 1149 | ! |
card$append_text("Plot", "header3")
|
| 1150 | ! |
card$append_plot(plot_r(), dim = pws$dim()) |
| 1151 | ! |
if (!comment == "") {
|
| 1152 | ! |
card$append_text("Comment", "header3")
|
| 1153 | ! |
card$append_text(comment) |
| 1154 |
} |
|
| 1155 | ! |
card$append_src(source_code_r()) |
| 1156 | ! |
card |
| 1157 |
} |
|
| 1158 | ! |
teal.reporter::add_card_button_srv("add_reporter", reporter = reporter, card_fun = card_fun)
|
| 1159 |
} |
|
| 1160 |
### |
|
| 1161 |
}) |
|
| 1162 |
} |
| 1 |
#' `teal` module: Missing data analysis |
|
| 2 |
#' |
|
| 3 |
#' This module analyzes missing data in `data.frame`s to help users explore missing observations and |
|
| 4 |
#' gain insights into the completeness of their data. |
|
| 5 |
#' It is useful for clinical data analysis within the context of `CDISC` standards and |
|
| 6 |
#' adaptable for general data analysis purposes. |
|
| 7 |
#' |
|
| 8 |
#' @inheritParams teal::module |
|
| 9 |
#' @inheritParams shared_params |
|
| 10 |
#' @param parent_dataname (`character(1)`) Specifies the parent dataset name. Default is `ADSL` for `CDISC` data. |
|
| 11 |
#' If provided and exists, enables additional analysis "by subject". For non-`CDISC` data, this parameter can be |
|
| 12 |
#' ignored. |
|
| 13 |
# nolint start: line_length. |
|
| 14 |
#' @param ggtheme (`character`) optional, specifies the default `ggplot2` theme for plots. Defaults to `classic`. |
|
| 15 |
#' @param ggplot2_args `r roxygen_ggplot2_args_param("Summary Obs", "Summary Patients", "Combinations Main", "Combinations Hist", "By Subject")`
|
|
| 16 |
# nolint end: line_length. |
|
| 17 |
#' |
|
| 18 |
#' @inherit shared_params return |
|
| 19 |
#' |
|
| 20 |
#' @section Decorating Module: |
|
| 21 |
#' |
|
| 22 |
#' This module generates the following objects, which can be modified in place using decorators: |
|
| 23 |
#' - `summary_plot` (`grob` created with [ggplot2::ggplotGrob()]) |
|
| 24 |
#' - `combination_plot` (`grob` created with [ggplot2::ggplotGrob()]) |
|
| 25 |
#' - `by_subject_plot` (`ggplot`) |
|
| 26 |
#' |
|
| 27 |
#' A Decorator is applied to the specific output using a named list of `teal_transform_module` objects. |
|
| 28 |
#' The name of this list corresponds to the name of the output to which the decorator is applied. |
|
| 29 |
#' See code snippet below: |
|
| 30 |
#' |
|
| 31 |
#' ``` |
|
| 32 |
#' tm_missing_data( |
|
| 33 |
#' ..., # arguments for module |
|
| 34 |
#' decorators = list( |
|
| 35 |
#' summary_plot = teal_transform_module(...), # applied only to `summary_plot` output |
|
| 36 |
#' combination_plot = teal_transform_module(...), # applied only to `combination_plot` output |
|
| 37 |
#' by_subject_plot = teal_transform_module(...) # applied only to `by_subject_plot` output |
|
| 38 |
#' ) |
|
| 39 |
#' ) |
|
| 40 |
#' ``` |
|
| 41 |
#' |
|
| 42 |
#' For additional details and examples of decorators, refer to the vignette |
|
| 43 |
#' `vignette("decorate-module-output", package = "teal.modules.general")`.
|
|
| 44 |
#' |
|
| 45 |
#' To learn more please refer to the vignette |
|
| 46 |
#' `vignette("transform-module-output", package = "teal")` or the [`teal::teal_transform_module()`] documentation.
|
|
| 47 |
#' |
|
| 48 |
#' @examplesShinylive |
|
| 49 |
#' library(teal.modules.general) |
|
| 50 |
#' interactive <- function() TRUE |
|
| 51 |
#' {{ next_example }}
|
|
| 52 |
#' @examples |
|
| 53 |
#' # general example data |
|
| 54 |
#' data <- teal_data() |
|
| 55 |
#' data <- within(data, {
|
|
| 56 |
#' require(nestcolor) |
|
| 57 |
#' |
|
| 58 |
#' add_nas <- function(x) {
|
|
| 59 |
#' x[sample(seq_along(x), floor(length(x) * runif(1, .05, .17)))] <- NA |
|
| 60 |
#' x |
|
| 61 |
#' } |
|
| 62 |
#' |
|
| 63 |
#' iris <- iris |
|
| 64 |
#' mtcars <- mtcars |
|
| 65 |
#' |
|
| 66 |
#' iris[] <- lapply(iris, add_nas) |
|
| 67 |
#' mtcars[] <- lapply(mtcars, add_nas) |
|
| 68 |
#' mtcars[["cyl"]] <- as.factor(mtcars[["cyl"]]) |
|
| 69 |
#' mtcars[["gear"]] <- as.factor(mtcars[["gear"]]) |
|
| 70 |
#' }) |
|
| 71 |
#' |
|
| 72 |
#' app <- init( |
|
| 73 |
#' data = data, |
|
| 74 |
#' modules = modules( |
|
| 75 |
#' tm_missing_data(parent_dataname = "mtcars") |
|
| 76 |
#' ) |
|
| 77 |
#' ) |
|
| 78 |
#' if (interactive()) {
|
|
| 79 |
#' shinyApp(app$ui, app$server) |
|
| 80 |
#' } |
|
| 81 |
#' |
|
| 82 |
#' @examplesShinylive |
|
| 83 |
#' library(teal.modules.general) |
|
| 84 |
#' interactive <- function() TRUE |
|
| 85 |
#' {{ next_example }}
|
|
| 86 |
#' @examples |
|
| 87 |
#' # CDISC example data |
|
| 88 |
#' data <- teal_data() |
|
| 89 |
#' data <- within(data, {
|
|
| 90 |
#' require(nestcolor) |
|
| 91 |
#' ADSL <- teal.data::rADSL |
|
| 92 |
#' ADRS <- rADRS |
|
| 93 |
#' }) |
|
| 94 |
#' join_keys(data) <- default_cdisc_join_keys[names(data)] |
|
| 95 |
#' |
|
| 96 |
#' app <- init( |
|
| 97 |
#' data = data, |
|
| 98 |
#' modules = modules( |
|
| 99 |
#' tm_missing_data() |
|
| 100 |
#' ) |
|
| 101 |
#' ) |
|
| 102 |
#' if (interactive()) {
|
|
| 103 |
#' shinyApp(app$ui, app$server) |
|
| 104 |
#' } |
|
| 105 |
#' |
|
| 106 |
#' @export |
|
| 107 |
#' |
|
| 108 |
tm_missing_data <- function(label = "Missing data", |
|
| 109 |
plot_height = c(600, 400, 5000), |
|
| 110 |
plot_width = NULL, |
|
| 111 |
datanames = "all", |
|
| 112 |
parent_dataname = "ADSL", |
|
| 113 |
ggtheme = c("classic", "gray", "bw", "linedraw", "light", "dark", "minimal", "void"),
|
|
| 114 |
ggplot2_args = list( |
|
| 115 |
"Combinations Hist" = teal.widgets::ggplot2_args(labs = list(caption = NULL)), |
|
| 116 |
"Combinations Main" = teal.widgets::ggplot2_args(labs = list(title = NULL)) |
|
| 117 |
), |
|
| 118 |
pre_output = NULL, |
|
| 119 |
post_output = NULL, |
|
| 120 |
transformators = list(), |
|
| 121 |
decorators = list()) {
|
|
| 122 | ! |
message("Initializing tm_missing_data")
|
| 123 | ||
| 124 |
# Normalize the parameters |
|
| 125 | ! |
if (inherits(ggplot2_args, "ggplot2_args")) ggplot2_args <- list(default = ggplot2_args) |
| 126 | ||
| 127 |
# Start of assertions |
|
| 128 | ! |
checkmate::assert_string(label) |
| 129 | ||
| 130 | ! |
checkmate::assert_numeric(plot_height, len = 3, any.missing = FALSE, finite = TRUE) |
| 131 | ! |
checkmate::assert_numeric(plot_height[1], lower = plot_height[2], upper = plot_height[3], .var.name = "plot_height") |
| 132 | ! |
checkmate::assert_numeric(plot_width, len = 3, any.missing = FALSE, null.ok = TRUE, finite = TRUE) |
| 133 | ! |
checkmate::assert_numeric( |
| 134 | ! |
plot_width[1], |
| 135 | ! |
lower = plot_width[2], upper = plot_width[3], null.ok = TRUE, .var.name = "plot_width" |
| 136 |
) |
|
| 137 | ||
| 138 | ! |
checkmate::assert_character(datanames, min.len = 0, min.chars = 1, null.ok = TRUE) |
| 139 | ! |
checkmate::assert_character(parent_dataname, min.len = 0, max.len = 1) |
| 140 | ! |
ggtheme <- match.arg(ggtheme) |
| 141 | ||
| 142 | ! |
plot_choices <- c("Summary Obs", "Summary Patients", "Combinations Main", "Combinations Hist", "By Subject")
|
| 143 | ! |
checkmate::assert_list(ggplot2_args, types = "ggplot2_args") |
| 144 | ! |
checkmate::assert_subset(names(ggplot2_args), c("default", plot_choices))
|
| 145 | ||
| 146 | ! |
checkmate::assert_multi_class(pre_output, c("shiny.tag", "shiny.tag.list", "html"), null.ok = TRUE)
|
| 147 | ! |
checkmate::assert_multi_class(post_output, c("shiny.tag", "shiny.tag.list", "html"), null.ok = TRUE)
|
| 148 | ||
| 149 | ! |
assert_decorators(decorators, names = c("summary_plot", "combination_plot", "by_subject_plot"))
|
| 150 |
# End of assertions |
|
| 151 | ||
| 152 | ! |
datanames_module <- if (identical(datanames, "all") || is.null(datanames)) {
|
| 153 | ! |
datanames |
| 154 |
} else {
|
|
| 155 | ! |
union(datanames, parent_dataname) |
| 156 |
} |
|
| 157 | ||
| 158 | ! |
ans <- module( |
| 159 | ! |
label, |
| 160 | ! |
server = srv_page_missing_data, |
| 161 | ! |
datanames = datanames_module, |
| 162 | ! |
server_args = list( |
| 163 | ! |
datanames = if (is.null(datanames)) "all" else datanames, |
| 164 | ! |
parent_dataname = parent_dataname, |
| 165 | ! |
plot_height = plot_height, |
| 166 | ! |
plot_width = plot_width, |
| 167 | ! |
ggplot2_args = ggplot2_args, |
| 168 | ! |
ggtheme = ggtheme, |
| 169 | ! |
decorators = decorators |
| 170 |
), |
|
| 171 | ! |
ui = ui_page_missing_data, |
| 172 | ! |
transformators = transformators, |
| 173 | ! |
ui_args = list(pre_output = pre_output, post_output = post_output) |
| 174 |
) |
|
| 175 | ! |
attr(ans, "teal_bookmarkable") <- TRUE |
| 176 | ! |
ans |
| 177 |
} |
|
| 178 | ||
| 179 |
# UI function for the missing data module (all datasets) |
|
| 180 |
ui_page_missing_data <- function(id, pre_output = NULL, post_output = NULL) {
|
|
| 181 | ! |
ns <- NS(id) |
| 182 | ! |
tagList( |
| 183 | ! |
teal.widgets::standard_layout( |
| 184 | ! |
output = teal.widgets::white_small_well( |
| 185 | ! |
uiOutput(ns("dataset_tabs"))
|
| 186 |
), |
|
| 187 | ! |
encoding = tags$div( |
| 188 | ! |
uiOutput(ns("dataset_encodings"))
|
| 189 |
), |
|
| 190 | ! |
uiOutput(ns("dataset_reporter")),
|
| 191 | ! |
pre_output = pre_output, |
| 192 | ! |
post_output = post_output |
| 193 |
) |
|
| 194 |
) |
|
| 195 |
} |
|
| 196 | ||
| 197 |
# Server function for the missing data module (all datasets) |
|
| 198 |
srv_page_missing_data <- function(id, data, reporter, filter_panel_api, datanames, parent_dataname, |
|
| 199 |
plot_height, plot_width, ggplot2_args, ggtheme, decorators) {
|
|
| 200 | ! |
with_reporter <- !missing(reporter) && inherits(reporter, "Reporter") |
| 201 | ! |
with_filter <- !missing(filter_panel_api) && inherits(filter_panel_api, "FilterPanelAPI") |
| 202 | ! |
moduleServer(id, function(input, output, session) {
|
| 203 | ! |
teal.logger::log_shiny_input_changes(input, namespace = "teal.modules.general") |
| 204 | ||
| 205 | ! |
datanames <- Filter(function(name) {
|
| 206 | ! |
is.data.frame(isolate(data())[[name]]) |
| 207 | ! |
}, if (identical(datanames, "all")) names(isolate(data())) else datanames) |
| 208 | ||
| 209 | ! |
if_subject_plot <- length(parent_dataname) > 0 && parent_dataname %in% datanames |
| 210 | ||
| 211 | ! |
ns <- session$ns |
| 212 | ||
| 213 | ! |
output$dataset_tabs <- renderUI({
|
| 214 | ! |
do.call( |
| 215 | ! |
tabsetPanel, |
| 216 | ! |
c( |
| 217 | ! |
id = ns("dataname_tab"),
|
| 218 | ! |
lapply( |
| 219 | ! |
datanames, |
| 220 | ! |
function(x) {
|
| 221 | ! |
tabPanel( |
| 222 | ! |
title = x, |
| 223 | ! |
ui_missing_data(id = ns(x), by_subject_plot = if_subject_plot) |
| 224 |
) |
|
| 225 |
} |
|
| 226 |
) |
|
| 227 |
) |
|
| 228 |
) |
|
| 229 |
}) |
|
| 230 | ||
| 231 | ! |
output$dataset_encodings <- renderUI({
|
| 232 | ! |
tagList( |
| 233 | ! |
lapply( |
| 234 | ! |
datanames, |
| 235 | ! |
function(x) {
|
| 236 | ! |
conditionalPanel( |
| 237 | ! |
is_tab_active_js(ns("dataname_tab"), x),
|
| 238 | ! |
encoding_missing_data( |
| 239 | ! |
id = ns(x), |
| 240 | ! |
summary_per_patient = if_subject_plot, |
| 241 | ! |
ggtheme = ggtheme, |
| 242 | ! |
datanames = datanames, |
| 243 | ! |
decorators = decorators |
| 244 |
) |
|
| 245 |
) |
|
| 246 |
} |
|
| 247 |
) |
|
| 248 |
) |
|
| 249 |
}) |
|
| 250 | ||
| 251 | ! |
output$dataset_reporter <- renderUI({
|
| 252 | ! |
lapply(datanames, function(x) {
|
| 253 | ! |
dataname_ns <- NS(ns(x)) |
| 254 | ||
| 255 | ! |
conditionalPanel( |
| 256 | ! |
is_tab_active_js(ns("dataname_tab"), x),
|
| 257 | ! |
tagList( |
| 258 | ! |
teal.widgets::verbatim_popup_ui(dataname_ns("rcode"), "Show R code")
|
| 259 |
) |
|
| 260 |
) |
|
| 261 |
}) |
|
| 262 |
}) |
|
| 263 | ||
| 264 | ! |
lapply( |
| 265 | ! |
datanames, |
| 266 | ! |
function(x) {
|
| 267 | ! |
srv_missing_data( |
| 268 | ! |
id = x, |
| 269 | ! |
data = data, |
| 270 | ! |
reporter = if (with_reporter) reporter, |
| 271 | ! |
filter_panel_api = if (with_filter) filter_panel_api, |
| 272 | ! |
dataname = x, |
| 273 | ! |
parent_dataname = parent_dataname, |
| 274 | ! |
plot_height = plot_height, |
| 275 | ! |
plot_width = plot_width, |
| 276 | ! |
ggplot2_args = ggplot2_args, |
| 277 | ! |
decorators = decorators |
| 278 |
) |
|
| 279 |
} |
|
| 280 |
) |
|
| 281 |
}) |
|
| 282 |
} |
|
| 283 | ||
| 284 |
# UI function for the missing data module (single dataset) |
|
| 285 |
ui_missing_data <- function(id, by_subject_plot = FALSE) {
|
|
| 286 | ! |
ns <- NS(id) |
| 287 | ||
| 288 | ! |
tab_list <- list( |
| 289 | ! |
tabPanel( |
| 290 | ! |
"Summary", |
| 291 | ! |
teal.widgets::plot_with_settings_ui(id = ns("summary_plot")),
|
| 292 | ! |
helpText( |
| 293 | ! |
tags$p(paste( |
| 294 | ! |
'The "Summary" graph shows the number of missing values per variable (both absolute and percentage),', |
| 295 | ! |
"sorted by magnitude." |
| 296 |
)), |
|
| 297 | ! |
tags$p( |
| 298 | ! |
'The "summary per patients" graph is showing how many subjects have at least one missing observation', |
| 299 | ! |
"for each variable. It will be most useful for panel datasets." |
| 300 |
) |
|
| 301 |
) |
|
| 302 |
), |
|
| 303 | ! |
tabPanel( |
| 304 | ! |
"Combinations", |
| 305 | ! |
teal.widgets::plot_with_settings_ui(id = ns("combination_plot")),
|
| 306 | ! |
helpText( |
| 307 | ! |
tags$p(paste( |
| 308 | ! |
'The "Combinations" graph is used to explore the relationship between the missing data within', |
| 309 | ! |
"different columns of the dataset.", |
| 310 | ! |
"It shows the different patterns of missingness in the rows of the data.", |
| 311 | ! |
'For example, suppose that 70 rows of the data have exactly columns "A" and "B" missing.', |
| 312 | ! |
"In this case there would be a bar of height 70 in the top graph and", |
| 313 | ! |
'the column below this in the second graph would have rows "A" and "B" cells shaded red.' |
| 314 |
)), |
|
| 315 | ! |
tags$p(paste( |
| 316 | ! |
"Due to the large number of missing data patterns possible, only those with a large set of observations", |
| 317 | ! |
'are shown in the graph and the "Combination cut-off" slider can be used to adjust the number shown.' |
| 318 |
)) |
|
| 319 |
) |
|
| 320 |
), |
|
| 321 | ! |
tabPanel( |
| 322 | ! |
"By Variable Levels", |
| 323 | ! |
teal.widgets::get_dt_rows(ns("levels_table"), ns("levels_table_rows")),
|
| 324 | ! |
DT::dataTableOutput(ns("levels_table"))
|
| 325 |
) |
|
| 326 |
) |
|
| 327 | ! |
if (isTRUE(by_subject_plot)) {
|
| 328 | ! |
tab_list <- append( |
| 329 | ! |
tab_list, |
| 330 | ! |
list(tabPanel( |
| 331 | ! |
"Grouped by Subject", |
| 332 | ! |
teal.widgets::plot_with_settings_ui(id = ns("by_subject_plot")),
|
| 333 | ! |
helpText( |
| 334 | ! |
tags$p(paste( |
| 335 | ! |
"This graph shows the missingness with respect to subjects rather than individual rows of the", |
| 336 | ! |
"dataset. Each row represents one dataset variable and each column a single subject. Only subjects", |
| 337 | ! |
"with at least one record in this dataset are shown. For a given subject, if they have any missing", |
| 338 | ! |
"values of a specific variable then the appropriate cell in the graph is marked as missing." |
| 339 |
)) |
|
| 340 |
) |
|
| 341 |
)) |
|
| 342 |
) |
|
| 343 |
} |
|
| 344 | ||
| 345 | ! |
do.call( |
| 346 | ! |
tabsetPanel, |
| 347 | ! |
c( |
| 348 | ! |
id = ns("summary_type"),
|
| 349 | ! |
tab_list |
| 350 |
) |
|
| 351 |
) |
|
| 352 |
} |
|
| 353 | ||
| 354 |
# UI encoding for the missing data module (all datasets) |
|
| 355 |
encoding_missing_data <- function(id, summary_per_patient = FALSE, ggtheme, datanames, decorators) {
|
|
| 356 | ! |
ns <- NS(id) |
| 357 | ||
| 358 | ! |
tagList( |
| 359 |
### Reporter |
|
| 360 | ! |
teal.reporter::add_card_button_ui(ns("add_reporter"), label = "Add Report Card"),
|
| 361 | ! |
tags$br(), tags$br(), |
| 362 |
### |
|
| 363 | ! |
tags$label("Encodings", class = "text-primary"),
|
| 364 | ! |
helpText( |
| 365 | ! |
paste0("Dataset", `if`(length(datanames) > 1, "s", ""), ":"),
|
| 366 | ! |
tags$code(paste(datanames, collapse = ", ")) |
| 367 |
), |
|
| 368 | ! |
uiOutput(ns("variables")),
|
| 369 | ! |
actionButton( |
| 370 | ! |
ns("filter_na"),
|
| 371 | ! |
tags$span("Select only vars with missings", style = "white-space: normal;"),
|
| 372 | ! |
width = "100%", |
| 373 | ! |
style = "margin-bottom: 1rem;" |
| 374 |
), |
|
| 375 | ! |
conditionalPanel( |
| 376 | ! |
is_tab_active_js(ns("summary_type"), "Summary"),
|
| 377 | ! |
bslib::input_switch( |
| 378 | ! |
id = ns("any_na"),
|
| 379 | ! |
label = tags$div( |
| 380 | ! |
HTML("Add <b>anyna</b> variable"),
|
| 381 | ! |
bslib::tooltip( |
| 382 | ! |
icon("circle-info"),
|
| 383 | ! |
tags$span( |
| 384 | ! |
"Describes the number of observations with at least one missing value in any variable." |
| 385 |
) |
|
| 386 |
) |
|
| 387 |
), |
|
| 388 | ! |
value = FALSE |
| 389 |
), |
|
| 390 | ! |
if (summary_per_patient) {
|
| 391 | ! |
bslib::input_switch( |
| 392 | ! |
id = ns("if_patients_plot"),
|
| 393 | ! |
label = tags$div( |
| 394 | ! |
"Add summary per patients", |
| 395 | ! |
bslib::tooltip( |
| 396 | ! |
icon("circle-info"),
|
| 397 | ! |
tags$span( |
| 398 | ! |
paste( |
| 399 | ! |
"Displays the number of missing values per observation,", |
| 400 | ! |
"where the x-axis is sorted by observation appearance in the table." |
| 401 |
) |
|
| 402 |
) |
|
| 403 |
) |
|
| 404 |
), |
|
| 405 | ! |
value = FALSE |
| 406 |
) |
|
| 407 |
}, |
|
| 408 | ! |
ui_decorate_teal_data(ns("dec_summary_plot"), decorators = select_decorators(decorators, "summary_plot"))
|
| 409 |
), |
|
| 410 | ! |
conditionalPanel( |
| 411 | ! |
is_tab_active_js(ns("summary_type"), "Combinations"),
|
| 412 | ! |
uiOutput(ns("cutoff")),
|
| 413 | ! |
ui_decorate_teal_data(ns("dec_combination_plot"), decorators = select_decorators(decorators, "combination_plot"))
|
| 414 |
), |
|
| 415 | ! |
conditionalPanel( |
| 416 | ! |
is_tab_active_js(ns("summary_type"), "Grouped by Subject"),
|
| 417 | ! |
ui_decorate_teal_data(ns("dec_by_subject_plot"), decorators = select_decorators(decorators, "by_subject_plot"))
|
| 418 |
), |
|
| 419 | ! |
conditionalPanel( |
| 420 | ! |
is_tab_active_js(ns("summary_type"), "By Variable Levels"),
|
| 421 | ! |
uiOutput(ns("group_by_var_ui")),
|
| 422 | ! |
uiOutput(ns("group_by_vals_ui")),
|
| 423 | ! |
radioButtons( |
| 424 | ! |
ns("count_type"),
|
| 425 | ! |
label = "Display missing as", |
| 426 | ! |
choices = c("counts", "proportions"),
|
| 427 | ! |
selected = "counts", |
| 428 | ! |
inline = TRUE |
| 429 |
) |
|
| 430 |
), |
|
| 431 | ! |
bslib::accordion( |
| 432 | ! |
bslib::accordion_panel( |
| 433 | ! |
title = "Plot settings", |
| 434 | ! |
selectInput( |
| 435 | ! |
inputId = ns("ggtheme"),
|
| 436 | ! |
label = "Theme (by ggplot):", |
| 437 | ! |
choices = ggplot_themes, |
| 438 | ! |
selected = ggtheme, |
| 439 | ! |
multiple = FALSE |
| 440 |
) |
|
| 441 |
) |
|
| 442 |
) |
|
| 443 |
) |
|
| 444 |
} |
|
| 445 | ||
| 446 |
# Server function for the missing data (single dataset) |
|
| 447 |
srv_missing_data <- function(id, |
|
| 448 |
data, |
|
| 449 |
reporter, |
|
| 450 |
filter_panel_api, |
|
| 451 |
dataname, |
|
| 452 |
parent_dataname, |
|
| 453 |
plot_height, |
|
| 454 |
plot_width, |
|
| 455 |
ggplot2_args, |
|
| 456 |
decorators) {
|
|
| 457 | ! |
with_reporter <- !missing(reporter) && inherits(reporter, "Reporter") |
| 458 | ! |
with_filter <- !missing(filter_panel_api) && inherits(filter_panel_api, "FilterPanelAPI") |
| 459 | ! |
checkmate::assert_class(data, "reactive") |
| 460 | ! |
checkmate::assert_class(isolate(data()), "teal_data") |
| 461 | ! |
moduleServer(id, function(input, output, session) {
|
| 462 | ! |
ns <- session$ns |
| 463 | ||
| 464 | ! |
prev_group_by_var <- reactiveVal("")
|
| 465 | ! |
data_r <- reactive(data()[[dataname]]) |
| 466 | ! |
data_keys <- reactive(unlist(teal.data::join_keys(data())[[dataname]])) |
| 467 | ||
| 468 | ! |
iv_r <- reactive({
|
| 469 | ! |
iv <- shinyvalidate::InputValidator$new() |
| 470 | ! |
iv$add_rule( |
| 471 | ! |
"variables_select", |
| 472 | ! |
shinyvalidate::sv_required("At least one reference variable needs to be selected.")
|
| 473 |
) |
|
| 474 | ! |
iv$add_rule( |
| 475 | ! |
"variables_select", |
| 476 | ! |
~ if (length(setdiff((.), data_keys())) < 1) "Please also select non-key columns." |
| 477 |
) |
|
| 478 | ! |
iv_summary_table <- shinyvalidate::InputValidator$new() |
| 479 | ! |
iv_summary_table$condition(~ isTRUE(input$summary_type == "By Variable Levels")) |
| 480 | ! |
iv_summary_table$add_rule("count_type", shinyvalidate::sv_required("Please select type of counts"))
|
| 481 | ! |
iv_summary_table$add_rule( |
| 482 | ! |
"group_by_vals", |
| 483 | ! |
shinyvalidate::sv_required("Please select both group-by variable and values")
|
| 484 |
) |
|
| 485 | ! |
iv_summary_table$add_rule( |
| 486 | ! |
"group_by_var", |
| 487 | ! |
~ if (length(.) > 0 && length(input$variables_select) == 1 && (.) == input$variables_select) {
|
| 488 | ! |
"If only one reference variable is selected it must not be the grouping variable." |
| 489 |
} |
|
| 490 |
) |
|
| 491 | ! |
iv_summary_table$add_rule( |
| 492 | ! |
"variables_select", |
| 493 | ! |
~ if (length(input$group_by_var) > 0 && length(.) == 1 && (.) == input$group_by_var) {
|
| 494 | ! |
"If only one reference variable is selected it must not be the grouping variable." |
| 495 |
} |
|
| 496 |
) |
|
| 497 | ! |
iv$add_validator(iv_summary_table) |
| 498 | ! |
iv$enable() |
| 499 | ! |
iv |
| 500 |
}) |
|
| 501 | ||
| 502 | ! |
data_parent_keys <- reactive({
|
| 503 | ! |
if (length(parent_dataname) > 0 && parent_dataname %in% names(data())) {
|
| 504 | ! |
keys <- teal.data::join_keys(data())[[dataname]] |
| 505 | ! |
if (parent_dataname %in% names(keys)) {
|
| 506 | ! |
keys[[parent_dataname]] |
| 507 |
} else {
|
|
| 508 | ! |
keys[[dataname]] |
| 509 |
} |
|
| 510 |
} else {
|
|
| 511 | ! |
NULL |
| 512 |
} |
|
| 513 |
}) |
|
| 514 | ||
| 515 | ! |
common_code_q <- reactive({
|
| 516 | ! |
teal::validate_inputs(iv_r()) |
| 517 | ||
| 518 | ! |
group_var <- input$group_by_var |
| 519 | ! |
anl <- data_r() |
| 520 | ! |
qenv <- teal.code::eval_code(data(), {
|
| 521 | ! |
'library("dplyr");library("ggplot2");library("tidyr");library("gridExtra")' # nolint quotes
|
| 522 |
}) |
|
| 523 | ||
| 524 | ! |
qenv <- if (!is.null(selected_vars()) && length(selected_vars()) != ncol(anl)) {
|
| 525 | ! |
teal.code::eval_code( |
| 526 | ! |
qenv, |
| 527 | ! |
substitute( |
| 528 | ! |
expr = ANL <- anl_name[, selected_vars, drop = FALSE], |
| 529 | ! |
env = list(anl_name = as.name(dataname), selected_vars = selected_vars()) |
| 530 |
) |
|
| 531 |
) |
|
| 532 |
} else {
|
|
| 533 | ! |
teal.code::eval_code( |
| 534 | ! |
qenv, |
| 535 | ! |
substitute(expr = ANL <- anl_name, env = list(anl_name = as.name(dataname))) |
| 536 |
) |
|
| 537 |
} |
|
| 538 | ||
| 539 | ! |
if (input$summary_type == "By Variable Levels" && !is.null(group_var) && !(group_var %in% selected_vars())) {
|
| 540 | ! |
qenv <- teal.code::eval_code( |
| 541 | ! |
qenv, |
| 542 | ! |
substitute( |
| 543 | ! |
expr = ANL[[group_var]] <- anl_name[[group_var]], |
| 544 | ! |
env = list(group_var = group_var, anl_name = as.name(dataname)) |
| 545 |
) |
|
| 546 |
) |
|
| 547 |
} |
|
| 548 | ||
| 549 | ! |
new_col_name <- "**anyna**" |
| 550 | ||
| 551 | ! |
qenv <- teal.code::eval_code( |
| 552 | ! |
qenv, |
| 553 | ! |
substitute( |
| 554 | ! |
expr = |
| 555 | ! |
create_cols_labels <- function(cols, just_label = FALSE) {
|
| 556 | ! |
column_labels <- column_labels_value |
| 557 | ! |
column_labels[is.na(column_labels) | length(column_labels) == 0] <- "" |
| 558 | ! |
if (just_label) {
|
| 559 | ! |
labels <- column_labels[cols] |
| 560 |
} else {
|
|
| 561 | ! |
labels <- ifelse(cols == new_col_name | cols == "", cols, paste0(column_labels[cols], " [", cols, "]")) |
| 562 |
} |
|
| 563 | ! |
labels |
| 564 |
}, |
|
| 565 | ! |
env = list( |
| 566 | ! |
new_col_name = new_col_name, |
| 567 | ! |
column_labels_value = c(teal.data::col_labels(data_r())[selected_vars()], |
| 568 | ! |
new_col_name = new_col_name |
| 569 |
) |
|
| 570 |
) |
|
| 571 |
) |
|
| 572 |
) |
|
| 573 | ! |
qenv |
| 574 |
}) |
|
| 575 | ||
| 576 | ! |
selected_vars <- reactive({
|
| 577 | ! |
req(input$variables_select) |
| 578 | ! |
keys <- data_keys() |
| 579 | ! |
vars <- unique(c(keys, input$variables_select)) |
| 580 | ! |
vars |
| 581 |
}) |
|
| 582 | ||
| 583 | ! |
vars_summary <- reactive({
|
| 584 | ! |
na_count <- data_r() %>% |
| 585 | ! |
sapply(function(x) mean(is.na(x)), USE.NAMES = TRUE) %>% |
| 586 | ! |
sort(decreasing = TRUE) |
| 587 | ||
| 588 | ! |
tibble::tibble( |
| 589 | ! |
key = names(na_count), |
| 590 | ! |
value = unname(na_count), |
| 591 | ! |
label = cut(na_count, breaks = seq(from = 0, to = 1, by = 0.1), include.lowest = TRUE) |
| 592 |
) |
|
| 593 |
}) |
|
| 594 | ||
| 595 |
# Keep encoding panel up-to-date |
|
| 596 | ! |
output$variables <- renderUI({
|
| 597 | ! |
choices <- split(x = vars_summary()$key, f = vars_summary()$label, drop = TRUE) %>% rev() |
| 598 | ! |
selected <- choices <- unname(unlist(choices)) |
| 599 | ||
| 600 | ! |
teal.widgets::optionalSelectInput( |
| 601 | ! |
ns("variables_select"),
|
| 602 | ! |
label = "Select variables", |
| 603 | ! |
label_help = HTML(paste0("Dataset: ", tags$code(dataname))),
|
| 604 | ! |
choices = teal.transform::variable_choices(data_r(), choices), |
| 605 | ! |
selected = selected, |
| 606 | ! |
multiple = TRUE |
| 607 |
) |
|
| 608 |
}) |
|
| 609 | ||
| 610 | ! |
observeEvent(input$filter_na, {
|
| 611 | ! |
choices <- vars_summary() %>% |
| 612 | ! |
dplyr::select(!!as.name("key")) %>%
|
| 613 | ! |
getElement(name = 1) |
| 614 | ||
| 615 | ! |
selected <- vars_summary() %>% |
| 616 | ! |
dplyr::filter(!!as.name("value") > 0) %>%
|
| 617 | ! |
dplyr::select(!!as.name("key")) %>%
|
| 618 | ! |
getElement(name = 1) |
| 619 | ||
| 620 | ! |
teal.widgets::updateOptionalSelectInput( |
| 621 | ! |
session = session, |
| 622 | ! |
inputId = "variables_select", |
| 623 | ! |
choices = teal.transform::variable_choices(data_r()), |
| 624 | ! |
selected = restoreInput(ns("variables_select"), selected)
|
| 625 |
) |
|
| 626 |
}) |
|
| 627 | ||
| 628 | ! |
output$group_by_var_ui <- renderUI({
|
| 629 | ! |
all_choices <- teal.transform::variable_choices(data_r()) |
| 630 | ! |
cat_choices <- all_choices[!sapply(data_r(), function(x) is.numeric(x) || inherits(x, "POSIXct"))] |
| 631 | ! |
validate( |
| 632 | ! |
need(cat_choices, "Dataset does not have any non-numeric or non-datetime variables to use to group data with") |
| 633 |
) |
|
| 634 | ! |
teal.widgets::optionalSelectInput( |
| 635 | ! |
ns("group_by_var"),
|
| 636 | ! |
label = "Group by variable", |
| 637 | ! |
choices = cat_choices, |
| 638 | ! |
selected = `if`( |
| 639 | ! |
is.null(isolate(input$group_by_var)), |
| 640 | ! |
cat_choices[1], |
| 641 | ! |
isolate(input$group_by_var) |
| 642 |
), |
|
| 643 | ! |
multiple = FALSE, |
| 644 | ! |
label_help = paste0("Dataset: ", dataname)
|
| 645 |
) |
|
| 646 |
}) |
|
| 647 | ||
| 648 | ! |
output$group_by_vals_ui <- renderUI({
|
| 649 | ! |
req(input$group_by_var) |
| 650 | ||
| 651 | ! |
choices <- teal.transform::value_choices(data_r(), input$group_by_var, input$group_by_var) |
| 652 | ! |
prev_choices <- isolate(input$group_by_vals) |
| 653 | ||
| 654 |
# determine selected value based on filtered data |
|
| 655 |
# display those previously selected values that are still available |
|
| 656 | ! |
selected <- if (!is.null(prev_choices) && any(prev_choices %in% choices)) {
|
| 657 | ! |
prev_choices[match(choices[choices %in% prev_choices], prev_choices)] |
| 658 | ! |
} else if ( |
| 659 | ! |
!is.null(prev_choices) && |
| 660 | ! |
!any(prev_choices %in% choices) && |
| 661 | ! |
isolate(prev_group_by_var()) == input$group_by_var |
| 662 |
) {
|
|
| 663 |
# if not any previously selected value is available and the grouping variable is the same, |
|
| 664 |
# then display NULL |
|
| 665 | ! |
NULL |
| 666 |
} else {
|
|
| 667 |
# if new grouping variable (i.e. not any previously selected value is available), |
|
| 668 |
# then display all choices |
|
| 669 | ! |
choices |
| 670 |
} |
|
| 671 | ||
| 672 | ! |
prev_group_by_var(input$group_by_var) # set current group_by_var |
| 673 | ! |
validate(need(length(choices) < 100, "Please select group-by variable with fewer than 100 unique values")) |
| 674 | ! |
teal.widgets::optionalSelectInput( |
| 675 | ! |
ns("group_by_vals"),
|
| 676 | ! |
label = "Filter levels", |
| 677 | ! |
choices = choices, |
| 678 | ! |
selected = selected, |
| 679 | ! |
multiple = TRUE, |
| 680 | ! |
label_help = paste0("Dataset: ", dataname)
|
| 681 |
) |
|
| 682 |
}) |
|
| 683 | ||
| 684 | ! |
combination_cutoff_q <- reactive({
|
| 685 | ! |
req(common_code_q()) |
| 686 | ! |
teal.code::eval_code( |
| 687 | ! |
common_code_q(), |
| 688 | ! |
quote( |
| 689 | ! |
combination_cutoff <- ANL %>% |
| 690 | ! |
dplyr::mutate_all(is.na) %>% |
| 691 | ! |
dplyr::group_by_all() %>% |
| 692 | ! |
dplyr::tally() %>% |
| 693 | ! |
dplyr::ungroup() |
| 694 |
) |
|
| 695 |
) |
|
| 696 |
}) |
|
| 697 | ||
| 698 | ! |
output$cutoff <- renderUI({
|
| 699 | ! |
x <- combination_cutoff_q()[["combination_cutoff"]]$n |
| 700 | ||
| 701 |
# select 10-th from the top |
|
| 702 | ! |
n <- length(x) |
| 703 | ! |
idx <- max(1, n - 10) |
| 704 | ! |
prev_value <- isolate(input$combination_cutoff) |
| 705 | ! |
value <- if (is.null(prev_value) || prev_value > max(x) || prev_value < min(x)) {
|
| 706 | ! |
sort(x, partial = idx)[idx] |
| 707 |
} else {
|
|
| 708 | ! |
prev_value |
| 709 |
} |
|
| 710 | ||
| 711 | ! |
teal.widgets::optionalSliderInputValMinMax( |
| 712 | ! |
ns("combination_cutoff"),
|
| 713 | ! |
"Combination cut-off", |
| 714 | ! |
c(value, range(x)) |
| 715 |
) |
|
| 716 |
}) |
|
| 717 | ||
| 718 |
# Prepare qenvs for output objects |
|
| 719 | ||
| 720 | ! |
summary_plot_q <- reactive({
|
| 721 | ! |
req(input$summary_type == "Summary") # needed to trigger show r code update on tab change |
| 722 | ! |
teal::validate_has_data(data_r(), 1) |
| 723 | ||
| 724 | ! |
qenv <- common_code_q() |
| 725 | ! |
if (input$any_na) {
|
| 726 | ! |
new_col_name <- "**anyna**" |
| 727 | ! |
qenv <- teal.code::eval_code( |
| 728 | ! |
qenv, |
| 729 | ! |
substitute( |
| 730 | ! |
expr = ANL[[new_col_name]] <- ifelse(rowSums(is.na(ANL)) > 0, NA, FALSE), |
| 731 | ! |
env = list(new_col_name = new_col_name) |
| 732 |
) |
|
| 733 |
) |
|
| 734 |
} |
|
| 735 | ||
| 736 | ! |
qenv <- teal.code::eval_code( |
| 737 | ! |
qenv, |
| 738 | ! |
substitute( |
| 739 | ! |
expr = analysis_vars <- setdiff(colnames(ANL), data_keys), |
| 740 | ! |
env = list(data_keys = data_keys()) |
| 741 |
) |
|
| 742 |
) %>% |
|
| 743 | ! |
teal.code::eval_code( |
| 744 | ! |
substitute( |
| 745 | ! |
expr = summary_plot_obs <- data_frame_call[, analysis_vars] %>% |
| 746 | ! |
dplyr::summarise_all(list(function(x) sum(is.na(x)))) %>% |
| 747 | ! |
tidyr::pivot_longer(dplyr::everything(), names_to = "col", values_to = "n_na") %>% |
| 748 | ! |
dplyr::mutate(n_not_na = nrow(ANL) - n_na) %>% |
| 749 | ! |
tidyr::pivot_longer(-col, names_to = "isna", values_to = "n") %>% |
| 750 | ! |
dplyr::mutate(isna = isna == "n_na", n_pct = n / nrow(ANL) * 100), |
| 751 | ! |
env = list(data_frame_call = if (!inherits(data_r(), "tbl_df")) {
|
| 752 | ! |
quote(tibble::as_tibble(ANL)) |
| 753 |
} else {
|
|
| 754 | ! |
quote(ANL) |
| 755 |
}) |
|
| 756 |
) |
|
| 757 |
) %>% |
|
| 758 |
# x axis ordering according to number of missing values and alphabet |
|
| 759 | ! |
teal.code::eval_code( |
| 760 | ! |
quote( |
| 761 | ! |
expr = x_levels <- dplyr::filter(summary_plot_obs, isna) %>% |
| 762 | ! |
dplyr::arrange(n_pct, dplyr::desc(col)) %>% |
| 763 | ! |
dplyr::pull(col) %>% |
| 764 | ! |
create_cols_labels() |
| 765 |
) |
|
| 766 |
) |
|
| 767 | ||
| 768 |
# always set "**anyna**" level as the last one |
|
| 769 | ! |
if (isolate(input$any_na)) {
|
| 770 | ! |
qenv <- teal.code::eval_code( |
| 771 | ! |
qenv, |
| 772 | ! |
quote(x_levels <- c(setdiff(x_levels, "**anyna**"), "**anyna**")) |
| 773 |
) |
|
| 774 |
} |
|
| 775 | ||
| 776 | ! |
dev_ggplot2_args <- teal.widgets::ggplot2_args( |
| 777 | ! |
labs = list(x = "Variable", y = "Missing observations"), |
| 778 | ! |
theme = list(legend.position = "bottom", axis.text.x = quote(ggplot2::element_text(angle = 45, hjust = 1))) |
| 779 |
) |
|
| 780 | ||
| 781 | ! |
all_ggplot2_args <- teal.widgets::resolve_ggplot2_args( |
| 782 | ! |
user_plot = ggplot2_args[["Summary Obs"]], |
| 783 | ! |
user_default = ggplot2_args$default, |
| 784 | ! |
module_plot = dev_ggplot2_args |
| 785 |
) |
|
| 786 | ||
| 787 | ! |
parsed_ggplot2_args <- teal.widgets::parse_ggplot2_args( |
| 788 | ! |
all_ggplot2_args, |
| 789 | ! |
ggtheme = input$ggtheme |
| 790 |
) |
|
| 791 | ||
| 792 | ! |
qenv <- teal.code::eval_code( |
| 793 | ! |
qenv, |
| 794 | ! |
substitute( |
| 795 | ! |
summary_plot_top <- summary_plot_obs %>% |
| 796 | ! |
ggplot2::ggplot() + |
| 797 | ! |
ggplot2::aes( |
| 798 | ! |
x = factor(create_cols_labels(col), levels = x_levels), |
| 799 | ! |
y = n_pct, |
| 800 | ! |
fill = isna |
| 801 |
) + |
|
| 802 | ! |
ggplot2::geom_bar(position = "fill", stat = "identity") + |
| 803 | ! |
ggplot2::scale_fill_manual( |
| 804 | ! |
name = "", |
| 805 | ! |
values = c("grey90", c(getOption("ggplot2.discrete.colour")[2], "#ff2951ff")[1]),
|
| 806 | ! |
labels = c("Present", "Missing")
|
| 807 |
) + |
|
| 808 | ! |
ggplot2::scale_y_continuous( |
| 809 | ! |
labels = scales::percent_format(), |
| 810 | ! |
breaks = seq(0, 1, by = 0.1), |
| 811 | ! |
expand = c(0, 0) |
| 812 |
) + |
|
| 813 | ! |
ggplot2::geom_text( |
| 814 | ! |
ggplot2::aes(label = ifelse(isna == TRUE, sprintf("%d [%.02f%%]", n, n_pct), ""), y = 1),
|
| 815 | ! |
hjust = 1, |
| 816 | ! |
color = "black" |
| 817 |
) + |
|
| 818 | ! |
labs + |
| 819 | ! |
ggthemes + |
| 820 | ! |
themes + |
| 821 | ! |
ggplot2::coord_flip(), |
| 822 | ! |
env = list( |
| 823 | ! |
labs = parsed_ggplot2_args$labs, |
| 824 | ! |
themes = parsed_ggplot2_args$theme, |
| 825 | ! |
ggthemes = parsed_ggplot2_args$ggtheme |
| 826 |
) |
|
| 827 |
) |
|
| 828 |
) |
|
| 829 | ||
| 830 | ! |
if (isTRUE(input$if_patients_plot)) {
|
| 831 | ! |
qenv <- teal.code::eval_code( |
| 832 | ! |
qenv, |
| 833 | ! |
substitute( |
| 834 | ! |
expr = parent_keys <- keys, |
| 835 | ! |
env = list(keys = data_parent_keys()) |
| 836 |
) |
|
| 837 |
) %>% |
|
| 838 | ! |
teal.code::eval_code(quote(ndistinct_subjects <- dplyr::n_distinct(ANL[, parent_keys]))) %>% |
| 839 | ! |
teal.code::eval_code( |
| 840 | ! |
quote( |
| 841 | ! |
summary_plot_patients <- ANL[, c(parent_keys, analysis_vars)] %>% |
| 842 | ! |
dplyr::group_by_at(parent_keys) %>% |
| 843 | ! |
dplyr::summarise_all(anyNA) %>% |
| 844 | ! |
tidyr::pivot_longer(cols = !dplyr::all_of(parent_keys), names_to = "col", values_to = "anyna") %>% |
| 845 | ! |
dplyr::group_by_at(c("col")) %>%
|
| 846 | ! |
dplyr::summarise(count_na = sum(anyna)) %>% |
| 847 | ! |
dplyr::mutate(count_not_na = ndistinct_subjects - count_na) %>% |
| 848 | ! |
tidyr::pivot_longer(-c(col), names_to = "isna", values_to = "n") %>% |
| 849 | ! |
dplyr::mutate(isna = isna == "count_na", n_pct = n / ndistinct_subjects * 100) %>% |
| 850 | ! |
dplyr::arrange_at(c("isna", "n"), .funs = dplyr::desc)
|
| 851 |
) |
|
| 852 |
) |
|
| 853 | ||
| 854 | ! |
dev_ggplot2_args <- teal.widgets::ggplot2_args( |
| 855 | ! |
labs = list(x = "", y = "Missing patients"), |
| 856 | ! |
theme = list( |
| 857 | ! |
legend.position = "bottom", |
| 858 | ! |
axis.text.x = quote(ggplot2::element_text(angle = 45, hjust = 1)), |
| 859 | ! |
axis.text.y = quote(ggplot2::element_blank()) |
| 860 |
) |
|
| 861 |
) |
|
| 862 | ||
| 863 | ! |
all_ggplot2_args <- teal.widgets::resolve_ggplot2_args( |
| 864 | ! |
user_plot = ggplot2_args[["Summary Patients"]], |
| 865 | ! |
user_default = ggplot2_args$default, |
| 866 | ! |
module_plot = dev_ggplot2_args |
| 867 |
) |
|
| 868 | ||
| 869 | ! |
parsed_ggplot2_args <- teal.widgets::parse_ggplot2_args( |
| 870 | ! |
all_ggplot2_args, |
| 871 | ! |
ggtheme = input$ggtheme |
| 872 |
) |
|
| 873 | ||
| 874 | ! |
qenv <- teal.code::eval_code( |
| 875 | ! |
qenv, |
| 876 | ! |
substitute( |
| 877 | ! |
summary_plot_bottom <- summary_plot_patients %>% |
| 878 | ! |
ggplot2::ggplot() + |
| 879 | ! |
ggplot2::aes_( |
| 880 | ! |
x = ~ factor(create_cols_labels(col), levels = x_levels), |
| 881 | ! |
y = ~n_pct, |
| 882 | ! |
fill = ~isna |
| 883 |
) + |
|
| 884 | ! |
ggplot2::geom_bar(alpha = 1, stat = "identity", position = "fill") + |
| 885 | ! |
ggplot2::scale_y_continuous( |
| 886 | ! |
labels = scales::percent_format(), |
| 887 | ! |
breaks = seq(0, 1, by = 0.1), |
| 888 | ! |
expand = c(0, 0) |
| 889 |
) + |
|
| 890 | ! |
ggplot2::scale_fill_manual( |
| 891 | ! |
name = "", |
| 892 | ! |
values = c("grey90", c(getOption("ggplot2.discrete.colour")[2], "#ff2951ff")[1]),
|
| 893 | ! |
labels = c("Present", "Missing")
|
| 894 |
) + |
|
| 895 | ! |
ggplot2::geom_text( |
| 896 | ! |
ggplot2::aes(label = ifelse(isna == TRUE, sprintf("%d [%.02f%%]", n, n_pct), ""), y = 1),
|
| 897 | ! |
hjust = 1, |
| 898 | ! |
color = "black" |
| 899 |
) + |
|
| 900 | ! |
labs + |
| 901 | ! |
ggthemes + |
| 902 | ! |
themes + |
| 903 | ! |
ggplot2::coord_flip(), |
| 904 | ! |
env = list( |
| 905 | ! |
labs = parsed_ggplot2_args$labs, |
| 906 | ! |
themes = parsed_ggplot2_args$theme, |
| 907 | ! |
ggthemes = parsed_ggplot2_args$ggtheme |
| 908 |
) |
|
| 909 |
) |
|
| 910 |
) |
|
| 911 |
} |
|
| 912 | ||
| 913 | ! |
if (isTRUE(input$if_patients_plot)) {
|
| 914 | ! |
within(qenv, {
|
| 915 | ! |
g1 <- ggplot2::ggplotGrob(summary_plot_top) |
| 916 | ! |
g2 <- ggplot2::ggplotGrob(summary_plot_bottom) |
| 917 | ! |
summary_plot <- gridExtra::gtable_cbind(g1, g2, size = "first") |
| 918 | ! |
summary_plot$heights <- grid::unit.pmax(g1$heights, g2$heights) |
| 919 |
}) |
|
| 920 |
} else {
|
|
| 921 | ! |
within(qenv, {
|
| 922 | ! |
g1 <- ggplot2::ggplotGrob(summary_plot_top) |
| 923 | ! |
summary_plot <- g1 |
| 924 |
}) |
|
| 925 |
} |
|
| 926 |
}) |
|
| 927 | ||
| 928 | ! |
combination_plot_q <- reactive({
|
| 929 | ! |
req(input$summary_type == "Combinations", input$combination_cutoff, combination_cutoff_q()) |
| 930 | ! |
teal::validate_has_data(data_r(), 1) |
| 931 | ||
| 932 | ! |
qenv <- teal.code::eval_code( |
| 933 | ! |
combination_cutoff_q(), |
| 934 | ! |
substitute( |
| 935 | ! |
expr = data_combination_plot_cutoff <- combination_cutoff %>% |
| 936 | ! |
dplyr::filter(n >= combination_cutoff_value) %>% |
| 937 | ! |
dplyr::mutate(id = rank(-n, ties.method = "first")) %>% |
| 938 | ! |
tidyr::pivot_longer(-c(n, id), names_to = "key", values_to = "value") %>% |
| 939 | ! |
dplyr::arrange(n), |
| 940 | ! |
env = list(combination_cutoff_value = input$combination_cutoff) |
| 941 |
) |
|
| 942 |
) |
|
| 943 | ||
| 944 |
# find keys in dataset not selected in the UI and remove them from dataset |
|
| 945 | ! |
keys_not_selected <- setdiff(data_keys(), input$variables_select) |
| 946 | ! |
if (length(keys_not_selected) > 0) {
|
| 947 | ! |
qenv <- teal.code::eval_code( |
| 948 | ! |
qenv, |
| 949 | ! |
substitute( |
| 950 | ! |
expr = data_combination_plot_cutoff <- data_combination_plot_cutoff %>% |
| 951 | ! |
dplyr::filter(!key %in% keys_not_selected), |
| 952 | ! |
env = list(keys_not_selected = keys_not_selected) |
| 953 |
) |
|
| 954 |
) |
|
| 955 |
} |
|
| 956 | ||
| 957 | ! |
qenv <- teal.code::eval_code( |
| 958 | ! |
qenv, |
| 959 | ! |
quote( |
| 960 | ! |
labels <- data_combination_plot_cutoff %>% |
| 961 | ! |
dplyr::filter(key == key[[1]]) %>% |
| 962 | ! |
getElement(name = 1) |
| 963 |
) |
|
| 964 |
) |
|
| 965 | ||
| 966 | ! |
dev_ggplot2_args1 <- teal.widgets::ggplot2_args( |
| 967 | ! |
labs = list(x = "", y = ""), |
| 968 | ! |
theme = list( |
| 969 | ! |
legend.position = "bottom", |
| 970 | ! |
axis.text.x = quote(ggplot2::element_blank()) |
| 971 |
) |
|
| 972 |
) |
|
| 973 | ||
| 974 | ! |
all_ggplot2_args1 <- teal.widgets::resolve_ggplot2_args( |
| 975 | ! |
user_plot = ggplot2_args[["Combinations Hist"]], |
| 976 | ! |
user_default = ggplot2_args$default, |
| 977 | ! |
module_plot = dev_ggplot2_args1 |
| 978 |
) |
|
| 979 | ||
| 980 | ! |
parsed_ggplot2_args1 <- teal.widgets::parse_ggplot2_args( |
| 981 | ! |
all_ggplot2_args1, |
| 982 | ! |
ggtheme = "void" |
| 983 |
) |
|
| 984 | ||
| 985 | ! |
dev_ggplot2_args2 <- teal.widgets::ggplot2_args( |
| 986 | ! |
labs = list(x = "", y = ""), |
| 987 | ! |
theme = list( |
| 988 | ! |
legend.position = "bottom", |
| 989 | ! |
axis.text.x = quote(ggplot2::element_blank()), |
| 990 | ! |
axis.ticks = quote(ggplot2::element_blank()), |
| 991 | ! |
panel.grid.major = quote(ggplot2::element_blank()) |
| 992 |
) |
|
| 993 |
) |
|
| 994 | ||
| 995 | ! |
all_ggplot2_args2 <- teal.widgets::resolve_ggplot2_args( |
| 996 | ! |
user_plot = ggplot2_args[["Combinations Main"]], |
| 997 | ! |
user_default = ggplot2_args$default, |
| 998 | ! |
module_plot = dev_ggplot2_args2 |
| 999 |
) |
|
| 1000 | ||
| 1001 | ! |
parsed_ggplot2_args2 <- teal.widgets::parse_ggplot2_args( |
| 1002 | ! |
all_ggplot2_args2, |
| 1003 | ! |
ggtheme = input$ggtheme |
| 1004 |
) |
|
| 1005 | ||
| 1006 | ! |
qenv <- teal.code::eval_code( |
| 1007 | ! |
qenv, |
| 1008 | ! |
substitute( |
| 1009 | ! |
expr = {
|
| 1010 | ! |
combination_plot_top <- data_combination_plot_cutoff %>% |
| 1011 | ! |
dplyr::select(id, n) %>% |
| 1012 | ! |
dplyr::distinct() %>% |
| 1013 | ! |
ggplot2::ggplot(ggplot2::aes(x = id, y = n)) + |
| 1014 | ! |
ggplot2::geom_bar(stat = "identity", fill = c(getOption("ggplot2.discrete.colour")[2], "#ff2951ff")[1]) +
|
| 1015 | ! |
ggplot2::geom_text( |
| 1016 | ! |
ggplot2::aes(label = n), |
| 1017 | ! |
position = ggplot2::position_dodge(width = 0.9), |
| 1018 | ! |
vjust = -0.25 |
| 1019 |
) + |
|
| 1020 | ! |
ggplot2::ylim(c(0, max(data_combination_plot_cutoff$n) * 1.5)) + |
| 1021 | ! |
labs1 + |
| 1022 | ! |
ggthemes1 + |
| 1023 | ! |
themes1 |
| 1024 | ||
| 1025 | ! |
graph_number_rows <- length(unique(data_combination_plot_cutoff$id)) |
| 1026 | ! |
graph_number_cols <- nrow(data_combination_plot_cutoff) / graph_number_rows |
| 1027 | ||
| 1028 | ! |
combination_plot_bottom <- data_combination_plot_cutoff %>% ggplot2::ggplot() + |
| 1029 | ! |
ggplot2::aes(x = create_cols_labels(key), y = id - 0.5, fill = value) + |
| 1030 | ! |
ggplot2::geom_tile(alpha = 0.85, height = 0.95) + |
| 1031 | ! |
ggplot2::scale_fill_manual( |
| 1032 | ! |
name = "", |
| 1033 | ! |
values = c("grey90", c(getOption("ggplot2.discrete.colour")[2], "#ff2951ff")[1]),
|
| 1034 | ! |
labels = c("Present", "Missing")
|
| 1035 |
) + |
|
| 1036 | ! |
ggplot2::geom_hline(yintercept = seq_len(1 + graph_number_rows) - 1) + |
| 1037 | ! |
ggplot2::geom_vline(xintercept = seq_len(1 + graph_number_cols) - 0.5, linetype = "dotted") + |
| 1038 | ! |
ggplot2::coord_flip() + |
| 1039 | ! |
labs2 + |
| 1040 | ! |
ggthemes2 + |
| 1041 | ! |
themes2 |
| 1042 |
}, |
|
| 1043 | ! |
env = list( |
| 1044 | ! |
labs1 = parsed_ggplot2_args1$labs, |
| 1045 | ! |
themes1 = parsed_ggplot2_args1$theme, |
| 1046 | ! |
ggthemes1 = parsed_ggplot2_args1$ggtheme, |
| 1047 | ! |
labs2 = parsed_ggplot2_args2$labs, |
| 1048 | ! |
themes2 = parsed_ggplot2_args2$theme, |
| 1049 | ! |
ggthemes2 = parsed_ggplot2_args2$ggtheme |
| 1050 |
) |
|
| 1051 |
) |
|
| 1052 |
) |
|
| 1053 | ||
| 1054 | ! |
within(qenv, {
|
| 1055 | ! |
g1 <- ggplot2::ggplotGrob(combination_plot_top) |
| 1056 | ! |
g2 <- ggplot2::ggplotGrob(combination_plot_bottom) |
| 1057 | ||
| 1058 | ! |
combination_plot <- gridExtra::gtable_rbind(g1, g2, size = "last") |
| 1059 | ! |
combination_plot$heights[7] <- grid::unit(0.2, "null") # rescale to get the bar chart smaller |
| 1060 |
}) |
|
| 1061 |
}) |
|
| 1062 | ||
| 1063 | ! |
summary_table_q <- reactive({
|
| 1064 | ! |
req( |
| 1065 | ! |
input$summary_type == "By Variable Levels", # needed to trigger show r code update on tab change |
| 1066 | ! |
common_code_q() |
| 1067 |
) |
|
| 1068 | ! |
teal::validate_has_data(data_r(), 1) |
| 1069 | ||
| 1070 |
# extract the ANL dataset for use in further validation |
|
| 1071 | ! |
anl <- common_code_q()[["ANL"]] |
| 1072 | ||
| 1073 | ! |
group_var <- input$group_by_var |
| 1074 | ! |
validate( |
| 1075 | ! |
need( |
| 1076 | ! |
is.null(group_var) || |
| 1077 | ! |
length(unique(anl[[group_var]])) < 100, |
| 1078 | ! |
"Please select group-by variable with fewer than 100 unique values" |
| 1079 |
) |
|
| 1080 |
) |
|
| 1081 | ||
| 1082 | ! |
group_vals <- input$group_by_vals |
| 1083 | ! |
variables_select <- input$variables_select |
| 1084 | ! |
vars <- unique(variables_select, group_var) |
| 1085 | ! |
count_type <- input$count_type |
| 1086 | ||
| 1087 | ! |
if (!is.null(selected_vars()) && length(selected_vars()) != ncol(anl)) {
|
| 1088 | ! |
variables <- selected_vars() |
| 1089 |
} else {
|
|
| 1090 | ! |
variables <- colnames(anl) |
| 1091 |
} |
|
| 1092 | ||
| 1093 | ! |
summ_fn <- if (input$count_type == "counts") {
|
| 1094 | ! |
function(x) sum(is.na(x)) |
| 1095 |
} else {
|
|
| 1096 | ! |
function(x) round(sum(is.na(x)) / length(x), 4) |
| 1097 |
} |
|
| 1098 | ||
| 1099 | ! |
qenv <- if (!is.null(group_var)) {
|
| 1100 | ! |
common_code_libraries_q <- teal.code::eval_code( |
| 1101 | ! |
common_code_q(), |
| 1102 | ! |
'library("forcats");library("glue");' # nolint quotes
|
| 1103 |
) |
|
| 1104 | ! |
teal.code::eval_code( |
| 1105 | ! |
common_code_libraries_q, |
| 1106 | ! |
substitute( |
| 1107 | ! |
expr = {
|
| 1108 | ! |
summary_data <- ANL %>% |
| 1109 | ! |
dplyr::mutate(group_var_name := forcats::fct_na_value_to_level(as.factor(group_var_name), "NA")) %>% |
| 1110 | ! |
dplyr::group_by_at(group_var) %>% |
| 1111 | ! |
dplyr::filter(group_var_name %in% group_vals) |
| 1112 | ||
| 1113 | ! |
count_data <- dplyr::summarise(summary_data, n = dplyr::n()) |
| 1114 | ||
| 1115 | ! |
summary_data <- dplyr::summarise_all(summary_data, summ_fn) %>% |
| 1116 | ! |
dplyr::mutate(group_var_name := paste0(group_var, ":", group_var_name, "(N=", count_data$n, ")")) %>% |
| 1117 | ! |
tidyr::pivot_longer(!dplyr::all_of(group_var), names_to = "Variable", values_to = "out") %>% |
| 1118 | ! |
tidyr::pivot_wider(names_from = group_var, values_from = "out") %>% |
| 1119 | ! |
dplyr::mutate(`Variable label` = create_cols_labels(Variable, just_label = TRUE), .after = Variable) |
| 1120 |
}, |
|
| 1121 | ! |
env = list( |
| 1122 | ! |
group_var = group_var, group_var_name = as.name(group_var), group_vals = group_vals, summ_fn = summ_fn |
| 1123 |
) |
|
| 1124 |
) |
|
| 1125 |
) |
|
| 1126 |
} else {
|
|
| 1127 | ! |
teal.code::eval_code( |
| 1128 | ! |
common_code_q(), |
| 1129 | ! |
substitute( |
| 1130 | ! |
expr = summary_data <- ANL %>% |
| 1131 | ! |
dplyr::summarise_all(summ_fn) %>% |
| 1132 | ! |
tidyr::pivot_longer(dplyr::everything(), |
| 1133 | ! |
names_to = "Variable", |
| 1134 | ! |
values_to = paste0("Missing (N=", nrow(ANL), ")")
|
| 1135 |
) %>% |
|
| 1136 | ! |
dplyr::mutate(`Variable label` = create_cols_labels(Variable), .after = Variable), |
| 1137 | ! |
env = list(summ_fn = summ_fn) |
| 1138 |
) |
|
| 1139 |
) |
|
| 1140 |
} |
|
| 1141 | ||
| 1142 | ! |
within(qenv, {
|
| 1143 | ! |
table <- rtables::df_to_tt(summary_data) |
| 1144 | ! |
table |
| 1145 |
}) |
|
| 1146 |
}) |
|
| 1147 | ||
| 1148 | ! |
by_subject_plot_q <- reactive({
|
| 1149 |
# needed to trigger show r code update on tab change |
|
| 1150 | ! |
req(input$summary_type == "Grouped by Subject", common_code_q()) |
| 1151 | ||
| 1152 | ! |
teal::validate_has_data(data_r(), 1) |
| 1153 | ||
| 1154 | ! |
dev_ggplot2_args <- teal.widgets::ggplot2_args( |
| 1155 | ! |
labs = list(x = "", y = ""), |
| 1156 | ! |
theme = list(legend.position = "bottom", axis.text.x = quote(ggplot2::element_blank())) |
| 1157 |
) |
|
| 1158 | ||
| 1159 | ! |
all_ggplot2_args <- teal.widgets::resolve_ggplot2_args( |
| 1160 | ! |
user_plot = ggplot2_args[["By Subject"]], |
| 1161 | ! |
user_default = ggplot2_args$default, |
| 1162 | ! |
module_plot = dev_ggplot2_args |
| 1163 |
) |
|
| 1164 | ||
| 1165 | ! |
parsed_ggplot2_args <- teal.widgets::parse_ggplot2_args( |
| 1166 | ! |
all_ggplot2_args, |
| 1167 | ! |
ggtheme = input$ggtheme |
| 1168 |
) |
|
| 1169 | ||
| 1170 |
# Unlikely that `rlang` is not available, new hashing may be expensive |
|
| 1171 | ! |
hashing_function <- if (requireNamespace("rlang", quietly = TRUE)) {
|
| 1172 | ! |
quote(rlang::hash) |
| 1173 |
} else {
|
|
| 1174 | ! |
function(x) paste(as.integer(x), collapse = "") |
| 1175 |
} |
|
| 1176 | ||
| 1177 | ! |
teal.code::eval_code( |
| 1178 | ! |
common_code_q(), |
| 1179 | ! |
substitute( |
| 1180 | ! |
expr = parent_keys <- keys, |
| 1181 | ! |
env = list(keys = data_parent_keys()) |
| 1182 |
) |
|
| 1183 |
) %>% |
|
| 1184 | ! |
teal.code::eval_code( |
| 1185 | ! |
substitute( |
| 1186 | ! |
expr = analysis_vars <- setdiff(colnames(ANL), data_keys), |
| 1187 | ! |
env = list(data_keys = data_keys()) |
| 1188 |
) |
|
| 1189 |
) %>% |
|
| 1190 | ! |
teal.code::eval_code( |
| 1191 | ! |
substitute( |
| 1192 | ! |
expr = {
|
| 1193 | ! |
summary_plot_patients <- ANL[, c(parent_keys, analysis_vars)] %>% |
| 1194 | ! |
dplyr::group_by_at(parent_keys) %>% |
| 1195 | ! |
dplyr::mutate(id = dplyr::cur_group_id()) %>% |
| 1196 | ! |
dplyr::ungroup() %>% |
| 1197 | ! |
dplyr::group_by_at(c(parent_keys, "id")) %>% |
| 1198 | ! |
dplyr::summarise_all(anyNA) %>% |
| 1199 | ! |
dplyr::ungroup() |
| 1200 | ||
| 1201 |
# order subjects by decreasing number of missing and then by |
|
| 1202 |
# missingness pattern (defined using sha1) |
|
| 1203 | ! |
order_subjects <- summary_plot_patients %>% |
| 1204 | ! |
dplyr::select(-"id", -dplyr::all_of(parent_keys)) %>% |
| 1205 | ! |
dplyr::transmute( |
| 1206 | ! |
id = dplyr::row_number(), |
| 1207 | ! |
number_NA = apply(., 1, sum), |
| 1208 | ! |
sha = apply(., 1, hashing_function) |
| 1209 |
) %>% |
|
| 1210 | ! |
dplyr::arrange(dplyr::desc(number_NA), sha) %>% |
| 1211 | ! |
getElement(name = "id") |
| 1212 | ||
| 1213 |
# order columns by decreasing percent of missing values |
|
| 1214 | ! |
ordered_columns <- summary_plot_patients %>% |
| 1215 | ! |
dplyr::select(-"id", -dplyr::all_of(parent_keys)) %>% |
| 1216 | ! |
dplyr::summarise( |
| 1217 | ! |
column = create_cols_labels(colnames(.)), |
| 1218 | ! |
na_count = apply(., MARGIN = 2, FUN = sum), |
| 1219 | ! |
na_percent = na_count / nrow(.) * 100 |
| 1220 |
) %>% |
|
| 1221 | ! |
dplyr::arrange(na_percent, dplyr::desc(column)) |
| 1222 | ||
| 1223 | ! |
summary_plot_patients <- summary_plot_patients %>% |
| 1224 | ! |
tidyr::gather("col", "isna", -"id", -dplyr::all_of(parent_keys)) %>%
|
| 1225 | ! |
dplyr::mutate(col = create_cols_labels(col)) |
| 1226 |
}, |
|
| 1227 | ! |
env = list(hashing_function = hashing_function) |
| 1228 |
) |
|
| 1229 |
) %>% |
|
| 1230 | ! |
teal.code::eval_code( |
| 1231 | ! |
substitute( |
| 1232 | ! |
expr = {
|
| 1233 | ! |
by_subject_plot <- ggplot2::ggplot(summary_plot_patients, ggplot2::aes( |
| 1234 | ! |
x = factor(id, levels = order_subjects), |
| 1235 | ! |
y = factor(col, levels = ordered_columns[["column"]]), |
| 1236 | ! |
fill = isna |
| 1237 |
)) + |
|
| 1238 | ! |
ggplot2::geom_raster() + |
| 1239 | ! |
ggplot2::annotate( |
| 1240 | ! |
"text", |
| 1241 | ! |
x = length(order_subjects), |
| 1242 | ! |
y = seq_len(nrow(ordered_columns)), |
| 1243 | ! |
hjust = 1, |
| 1244 | ! |
label = sprintf("%d [%.02f%%]", ordered_columns[["na_count"]], ordered_columns[["na_percent"]])
|
| 1245 |
) + |
|
| 1246 | ! |
ggplot2::scale_fill_manual( |
| 1247 | ! |
name = "", |
| 1248 | ! |
values = c("grey90", c(getOption("ggplot2.discrete.colour")[2], "#ff2951ff")[1]),
|
| 1249 | ! |
labels = c("Present", "Missing (at least one)")
|
| 1250 |
) + |
|
| 1251 | ! |
labs + |
| 1252 | ! |
ggthemes + |
| 1253 | ! |
themes |
| 1254 |
}, |
|
| 1255 | ! |
env = list( |
| 1256 | ! |
labs = parsed_ggplot2_args$labs, |
| 1257 | ! |
themes = parsed_ggplot2_args$theme, |
| 1258 | ! |
ggthemes = parsed_ggplot2_args$ggtheme |
| 1259 |
) |
|
| 1260 |
) |
|
| 1261 |
) |
|
| 1262 |
}) |
|
| 1263 | ||
| 1264 |
# Decorated outputs |
|
| 1265 | ||
| 1266 |
# Summary_plot_q |
|
| 1267 | ! |
decorated_summary_plot_q <- srv_decorate_teal_data( |
| 1268 | ! |
id = "dec_summary_plot", |
| 1269 | ! |
data = summary_plot_q, |
| 1270 | ! |
decorators = select_decorators(decorators, "summary_plot"), |
| 1271 | ! |
expr = {
|
| 1272 | ! |
grid::grid.newpage() |
| 1273 | ! |
grid::grid.draw(summary_plot) |
| 1274 |
} |
|
| 1275 |
) |
|
| 1276 | ||
| 1277 | ! |
decorated_combination_plot_q <- srv_decorate_teal_data( |
| 1278 | ! |
id = "dec_combination_plot", |
| 1279 | ! |
data = combination_plot_q, |
| 1280 | ! |
decorators = select_decorators(decorators, "combination_plot"), |
| 1281 | ! |
expr = {
|
| 1282 | ! |
grid::grid.newpage() |
| 1283 | ! |
grid::grid.draw(combination_plot) |
| 1284 |
} |
|
| 1285 |
) |
|
| 1286 | ||
| 1287 | ! |
decorated_by_subject_plot_q <- srv_decorate_teal_data( |
| 1288 | ! |
id = "dec_by_subject_plot", |
| 1289 | ! |
data = by_subject_plot_q, |
| 1290 | ! |
decorators = select_decorators(decorators, "by_subject_plot"), |
| 1291 | ! |
expr = print(by_subject_plot) |
| 1292 |
) |
|
| 1293 | ||
| 1294 |
# Plots & tables reactives |
|
| 1295 | ||
| 1296 | ! |
summary_plot_r <- reactive({
|
| 1297 | ! |
req(decorated_summary_plot_q())[["summary_plot"]] |
| 1298 |
}) |
|
| 1299 | ||
| 1300 | ! |
combination_plot_r <- reactive({
|
| 1301 | ! |
req(decorated_combination_plot_q())[["combination_plot"]] |
| 1302 |
}) |
|
| 1303 | ||
| 1304 | ! |
summary_table_r <- reactive({
|
| 1305 | ! |
q <- req(summary_table_q()) |
| 1306 | ||
| 1307 | ! |
list( |
| 1308 | ! |
html = if (length(input$variables_select) == 0) {
|
| 1309 |
# so that zeroRecords message gets printed |
|
| 1310 |
# using tibble as it supports weird column names, such as " " |
|
| 1311 | ! |
DT::datatable( |
| 1312 | ! |
tibble::tibble(` ` = logical(0)), |
| 1313 | ! |
options = list( |
| 1314 | ! |
language = list(zeroRecords = "No variable selected."), |
| 1315 | ! |
pageLength = input$levels_table_rows |
| 1316 |
) |
|
| 1317 |
) |
|
| 1318 |
} else {
|
|
| 1319 | ! |
DT::datatable(q[["summary_data"]]) |
| 1320 |
}, |
|
| 1321 | ! |
report = q[["table"]] |
| 1322 |
) |
|
| 1323 |
}) |
|
| 1324 | ||
| 1325 | ! |
by_subject_plot_r <- reactive({
|
| 1326 | ! |
req(decorated_by_subject_plot_q()[["by_subject_plot"]]) |
| 1327 |
}) |
|
| 1328 | ||
| 1329 |
# Generate output |
|
| 1330 | ! |
pws1 <- teal.widgets::plot_with_settings_srv( |
| 1331 | ! |
id = "summary_plot", |
| 1332 | ! |
plot_r = summary_plot_r, |
| 1333 | ! |
height = plot_height, |
| 1334 | ! |
width = plot_width |
| 1335 |
) |
|
| 1336 | ||
| 1337 | ! |
pws2 <- teal.widgets::plot_with_settings_srv( |
| 1338 | ! |
id = "combination_plot", |
| 1339 | ! |
plot_r = combination_plot_r, |
| 1340 | ! |
height = plot_height, |
| 1341 | ! |
width = plot_width |
| 1342 |
) |
|
| 1343 | ||
| 1344 | ! |
output$levels_table <- DT::renderDataTable(summary_table_r()[["html"]]) |
| 1345 | ||
| 1346 | ! |
pws3 <- teal.widgets::plot_with_settings_srv( |
| 1347 | ! |
id = "by_subject_plot", |
| 1348 | ! |
plot_r = by_subject_plot_r, |
| 1349 | ! |
height = plot_height, |
| 1350 | ! |
width = plot_width |
| 1351 |
) |
|
| 1352 | ||
| 1353 | ! |
decorated_final_q <- reactive({
|
| 1354 | ! |
sum_type <- req(input$summary_type) |
| 1355 | ! |
if (sum_type == "Summary") {
|
| 1356 | ! |
decorated_summary_plot_q() |
| 1357 | ! |
} else if (sum_type == "Combinations") {
|
| 1358 | ! |
decorated_combination_plot_q() |
| 1359 | ! |
} else if (sum_type == "By Variable Levels") {
|
| 1360 | ! |
summary_table_q() |
| 1361 | ! |
} else if (sum_type == "Grouped by Subject") {
|
| 1362 | ! |
decorated_by_subject_plot_q() |
| 1363 |
} |
|
| 1364 |
}) |
|
| 1365 | ||
| 1366 |
# Render R code. |
|
| 1367 | ! |
source_code_r <- reactive(teal.code::get_code(req(decorated_final_q()))) |
| 1368 | ||
| 1369 | ! |
teal.widgets::verbatim_popup_srv( |
| 1370 | ! |
id = "rcode", |
| 1371 | ! |
verbatim_content = source_code_r, |
| 1372 | ! |
title = "Show R Code for Missing Data" |
| 1373 |
) |
|
| 1374 | ||
| 1375 |
### REPORTER |
|
| 1376 | ! |
if (with_reporter) {
|
| 1377 | ! |
card_fun <- function(comment, label) {
|
| 1378 | ! |
card <- teal::TealReportCard$new() |
| 1379 | ! |
sum_type <- input$summary_type |
| 1380 | ! |
title <- if (sum_type == "By Variable Levels") paste0(sum_type, " Table") else paste0(sum_type, " Plot") |
| 1381 | ! |
title_dataname <- paste(title, dataname, sep = " - ") |
| 1382 | ! |
label <- if (label == "") {
|
| 1383 | ! |
paste("Missing Data", sum_type, dataname, sep = " - ")
|
| 1384 |
} else {
|
|
| 1385 | ! |
label |
| 1386 |
} |
|
| 1387 | ! |
card$set_name(label) |
| 1388 | ! |
card$append_text(title_dataname, "header2") |
| 1389 | ! |
if (with_filter) card$append_fs(filter_panel_api$get_filter_state()) |
| 1390 | ! |
if (sum_type == "Summary") {
|
| 1391 | ! |
card$append_text("Plot", "header3")
|
| 1392 | ! |
card$append_plot(summary_plot_r(), dim = pws1$dim()) |
| 1393 | ! |
} else if (sum_type == "Combinations") {
|
| 1394 | ! |
card$append_text("Plot", "header3")
|
| 1395 | ! |
card$append_plot(combination_plot_r(), dim = pws2$dim()) |
| 1396 | ! |
} else if (sum_type == "By Variable Levels") {
|
| 1397 | ! |
card$append_text("Table", "header3")
|
| 1398 | ! |
if (nrow(summary_table_q()[["summary_data"]]) == 0L) {
|
| 1399 | ! |
card$append_text("No data available for table.")
|
| 1400 |
} else {
|
|
| 1401 | ! |
card$append_table(summary_table_r()[["report"]]) |
| 1402 |
} |
|
| 1403 | ! |
} else if (sum_type == "Grouped by Subject") {
|
| 1404 | ! |
card$append_text("Plot", "header3")
|
| 1405 | ! |
card$append_plot(by_subject_plot_r(), dim = pws3$dim()) |
| 1406 |
} |
|
| 1407 | ! |
if (!comment == "") {
|
| 1408 | ! |
card$append_text("Comment", "header3")
|
| 1409 | ! |
card$append_text(comment) |
| 1410 |
} |
|
| 1411 | ! |
card$append_src(source_code_r()) |
| 1412 | ! |
card |
| 1413 |
} |
|
| 1414 | ! |
teal.reporter::add_card_button_srv("add_reporter", reporter = reporter, card_fun = card_fun)
|
| 1415 |
} |
|
| 1416 |
### |
|
| 1417 |
}) |
|
| 1418 |
} |
| 1 |
#' `teal` module: Data table viewer |
|
| 2 |
#' |
|
| 3 |
#' Module provides a dynamic and interactive way to view `data.frame`s in a `teal` application. |
|
| 4 |
#' It uses the `DT` package to display data tables in a paginated, searchable, and sortable format, |
|
| 5 |
#' which helps to enhance data exploration and analysis. |
|
| 6 |
#' |
|
| 7 |
#' The `DT` package has an option `DT.TOJSON_ARGS` to show `Inf` and `NA` in data tables. |
|
| 8 |
#' Configure the `DT.TOJSON_ARGS` option via |
|
| 9 |
#' `options(DT.TOJSON_ARGS = list(na = "string"))` before running the module. |
|
| 10 |
#' Note though that sorting of numeric columns with `NA`/`Inf` will be lexicographic not numerical. |
|
| 11 |
#' |
|
| 12 |
#' @inheritParams teal::module |
|
| 13 |
#' @inheritParams shared_params |
|
| 14 |
#' @param variables_selected (`named list`) Character vectors of the variables (i.e. columns) |
|
| 15 |
#' which should be initially shown for each dataset. |
|
| 16 |
#' Names of list elements should correspond to the names of the datasets available in the app. |
|
| 17 |
#' If no entry is specified for a dataset, the first six variables from that |
|
| 18 |
#' dataset will initially be shown. |
|
| 19 |
#' @param datasets_selected (`character`) `r lifecycle::badge("deprecated")` A vector of datasets which should be
|
|
| 20 |
#' shown and in what order. Use `datanames` instead. |
|
| 21 |
#' @param dt_args (`named list`) Additional arguments to be passed to [DT::datatable()] |
|
| 22 |
#' (must not include `data` or `options`). |
|
| 23 |
#' @param dt_options (`named list`) The `options` argument to `DT::datatable`. By default |
|
| 24 |
#' `list(searching = FALSE, pageLength = 30, lengthMenu = c(5, 15, 30, 100), scrollX = TRUE)` |
|
| 25 |
#' @param server_rendering (`logical`) should the data table be rendered server side |
|
| 26 |
#' (see `server` argument of [DT::renderDataTable()]) |
|
| 27 |
#' |
|
| 28 |
#' @inherit shared_params return |
|
| 29 |
#' |
|
| 30 |
#' @examplesShinylive |
|
| 31 |
#' library(teal.modules.general) |
|
| 32 |
#' interactive <- function() TRUE |
|
| 33 |
#' {{ next_example }}
|
|
| 34 |
#' @examples |
|
| 35 |
#' # general data example |
|
| 36 |
#' data <- teal_data() |
|
| 37 |
#' data <- within(data, {
|
|
| 38 |
#' require(nestcolor) |
|
| 39 |
#' iris <- iris |
|
| 40 |
#' }) |
|
| 41 |
#' |
|
| 42 |
#' app <- init( |
|
| 43 |
#' data = data, |
|
| 44 |
#' modules = modules( |
|
| 45 |
#' tm_data_table( |
|
| 46 |
#' variables_selected = list( |
|
| 47 |
#' iris = c("Sepal.Length", "Sepal.Width", "Petal.Length", "Petal.Width", "Species")
|
|
| 48 |
#' ), |
|
| 49 |
#' dt_args = list(caption = "IRIS Table Caption") |
|
| 50 |
#' ) |
|
| 51 |
#' ) |
|
| 52 |
#' ) |
|
| 53 |
#' if (interactive()) {
|
|
| 54 |
#' shinyApp(app$ui, app$server) |
|
| 55 |
#' } |
|
| 56 |
#' |
|
| 57 |
#' @examplesShinylive |
|
| 58 |
#' library(teal.modules.general) |
|
| 59 |
#' interactive <- function() TRUE |
|
| 60 |
#' {{ next_example }}
|
|
| 61 |
#' @examples |
|
| 62 |
#' # CDISC data example |
|
| 63 |
#' data <- teal_data() |
|
| 64 |
#' data <- within(data, {
|
|
| 65 |
#' require(nestcolor) |
|
| 66 |
#' ADSL <- teal.data::rADSL |
|
| 67 |
#' }) |
|
| 68 |
#' join_keys(data) <- default_cdisc_join_keys[names(data)] |
|
| 69 |
#' |
|
| 70 |
#' app <- init( |
|
| 71 |
#' data = data, |
|
| 72 |
#' modules = modules( |
|
| 73 |
#' tm_data_table( |
|
| 74 |
#' variables_selected = list(ADSL = c("STUDYID", "USUBJID", "SUBJID", "SITEID", "AGE", "SEX")),
|
|
| 75 |
#' dt_args = list(caption = "ADSL Table Caption") |
|
| 76 |
#' ) |
|
| 77 |
#' ) |
|
| 78 |
#' ) |
|
| 79 |
#' if (interactive()) {
|
|
| 80 |
#' shinyApp(app$ui, app$server) |
|
| 81 |
#' } |
|
| 82 |
#' |
|
| 83 |
#' @export |
|
| 84 |
#' |
|
| 85 |
tm_data_table <- function(label = "Data Table", |
|
| 86 |
variables_selected = list(), |
|
| 87 |
datasets_selected = deprecated(), |
|
| 88 |
datanames = if (missing(datasets_selected)) "all" else datasets_selected, |
|
| 89 |
dt_args = list(), |
|
| 90 |
dt_options = list( |
|
| 91 |
searching = FALSE, |
|
| 92 |
pageLength = 30, |
|
| 93 |
lengthMenu = c(5, 15, 30, 100), |
|
| 94 |
scrollX = TRUE |
|
| 95 |
), |
|
| 96 |
server_rendering = FALSE, |
|
| 97 |
pre_output = NULL, |
|
| 98 |
post_output = NULL, |
|
| 99 |
transformators = list()) {
|
|
| 100 | ! |
message("Initializing tm_data_table")
|
| 101 | ||
| 102 |
# Start of assertions |
|
| 103 | ! |
checkmate::assert_string(label) |
| 104 | ||
| 105 | ! |
checkmate::assert_list(variables_selected, min.len = 0, types = "character", names = "named") |
| 106 | ! |
if (length(variables_selected) > 0) {
|
| 107 | ! |
lapply(seq_along(variables_selected), function(i) {
|
| 108 | ! |
checkmate::assert_character(variables_selected[[i]], min.chars = 1, min.len = 1) |
| 109 | ! |
if (!is.null(names(variables_selected[[i]]))) {
|
| 110 | ! |
checkmate::assert_names(names(variables_selected[[i]])) |
| 111 |
} |
|
| 112 |
}) |
|
| 113 |
} |
|
| 114 | ! |
if (!missing(datasets_selected)) {
|
| 115 | ! |
lifecycle::deprecate_stop( |
| 116 | ! |
when = "0.4.0", |
| 117 | ! |
what = "tm_data_table(datasets_selected)", |
| 118 | ! |
with = "tm_data_table(datanames)", |
| 119 | ! |
details = 'Use tm_data_table(datanames = "all") to keep the previous behavior and avoid this warning.', |
| 120 |
) |
|
| 121 |
} |
|
| 122 | ! |
checkmate::assert_character(datanames, min.len = 0, min.chars = 1, null.ok = TRUE) |
| 123 | ! |
checkmate::assert( |
| 124 | ! |
checkmate::check_list(dt_args, len = 0), |
| 125 | ! |
checkmate::check_subset(names(dt_args), choices = names(formals(DT::datatable))) |
| 126 |
) |
|
| 127 | ! |
checkmate::assert_list(dt_options, names = "named") |
| 128 | ! |
checkmate::assert_flag(server_rendering) |
| 129 | ! |
checkmate::assert_multi_class(pre_output, c("shiny.tag", "shiny.tag.list", "html"), null.ok = TRUE)
|
| 130 | ! |
checkmate::assert_multi_class(post_output, c("shiny.tag", "shiny.tag.list", "html"), null.ok = TRUE)
|
| 131 | ||
| 132 |
# End of assertions |
|
| 133 | ||
| 134 | ! |
ans <- module( |
| 135 | ! |
label, |
| 136 | ! |
server = srv_page_data_table, |
| 137 | ! |
ui = ui_page_data_table, |
| 138 | ! |
datanames = datanames, |
| 139 | ! |
server_args = list( |
| 140 | ! |
datanames = if (is.null(datanames)) "all" else datanames, |
| 141 | ! |
variables_selected = variables_selected, |
| 142 | ! |
dt_args = dt_args, |
| 143 | ! |
dt_options = dt_options, |
| 144 | ! |
server_rendering = server_rendering |
| 145 |
), |
|
| 146 | ! |
ui_args = list( |
| 147 | ! |
pre_output = pre_output, |
| 148 | ! |
post_output = post_output |
| 149 |
), |
|
| 150 | ! |
transformators = transformators |
| 151 |
) |
|
| 152 | ! |
attr(ans, "teal_bookmarkable") <- TRUE |
| 153 | ! |
ans |
| 154 |
} |
|
| 155 | ||
| 156 |
# UI page module |
|
| 157 |
ui_page_data_table <- function(id, pre_output = NULL, post_output = NULL) {
|
|
| 158 | ! |
ns <- NS(id) |
| 159 | ||
| 160 | ! |
tagList( |
| 161 | ! |
teal.widgets::standard_layout( |
| 162 | ! |
output = teal.widgets::white_small_well( |
| 163 | ! |
bslib::page_fluid( |
| 164 | ! |
checkboxInput( |
| 165 | ! |
ns("if_distinct"),
|
| 166 | ! |
"Show only distinct rows:", |
| 167 | ! |
value = FALSE |
| 168 |
) |
|
| 169 |
), |
|
| 170 | ! |
bslib::page_fluid( |
| 171 | ! |
uiOutput(ns("dataset_table"))
|
| 172 |
) |
|
| 173 |
), |
|
| 174 | ! |
pre_output = pre_output, |
| 175 | ! |
post_output = post_output |
| 176 |
) |
|
| 177 |
) |
|
| 178 |
} |
|
| 179 | ||
| 180 |
# Server page module |
|
| 181 |
srv_page_data_table <- function(id, |
|
| 182 |
data, |
|
| 183 |
datanames, |
|
| 184 |
variables_selected, |
|
| 185 |
dt_args, |
|
| 186 |
dt_options, |
|
| 187 |
server_rendering) {
|
|
| 188 | ! |
checkmate::assert_class(data, "reactive") |
| 189 | ! |
checkmate::assert_class(isolate(data()), "teal_data") |
| 190 | ! |
moduleServer(id, function(input, output, session) {
|
| 191 | ! |
teal.logger::log_shiny_input_changes(input, namespace = "teal.modules.general") |
| 192 | ||
| 193 | ! |
if_filtered <- reactive(as.logical(input$if_filtered)) |
| 194 | ! |
if_distinct <- reactive(as.logical(input$if_distinct)) |
| 195 | ||
| 196 | ! |
datanames <- Filter(function(name) {
|
| 197 | ! |
is.data.frame(isolate(data())[[name]]) |
| 198 | ! |
}, if (identical(datanames, "all")) names(isolate(data())) else datanames) |
| 199 | ||
| 200 | ||
| 201 | ! |
output$dataset_table <- renderUI({
|
| 202 | ! |
do.call( |
| 203 | ! |
tabsetPanel, |
| 204 | ! |
c( |
| 205 | ! |
list(id = session$ns("dataname_tab")),
|
| 206 | ! |
lapply( |
| 207 | ! |
datanames, |
| 208 | ! |
function(x) {
|
| 209 | ! |
dataset <- isolate(data()[[x]]) |
| 210 | ! |
choices <- names(dataset) |
| 211 | ! |
labels <- vapply( |
| 212 | ! |
dataset, |
| 213 | ! |
function(x) ifelse(is.null(attr(x, "label")), "", attr(x, "label")), |
| 214 | ! |
character(1) |
| 215 |
) |
|
| 216 | ! |
names(choices) <- ifelse( |
| 217 | ! |
is.na(labels) | labels == "", |
| 218 | ! |
choices, |
| 219 | ! |
paste(choices, labels, sep = ": ") |
| 220 |
) |
|
| 221 | ! |
variables_selected <- if (!is.null(variables_selected[[x]])) {
|
| 222 | ! |
variables_selected[[x]] |
| 223 |
} else {
|
|
| 224 | ! |
utils::head(choices) |
| 225 |
} |
|
| 226 | ! |
tabPanel( |
| 227 | ! |
title = x, |
| 228 | ! |
bslib::layout_columns( |
| 229 | ! |
col_widths = 12, |
| 230 | ! |
ui_data_table( |
| 231 | ! |
id = session$ns(x), |
| 232 | ! |
choices = choices, |
| 233 | ! |
selected = variables_selected |
| 234 |
) |
|
| 235 |
) |
|
| 236 |
) |
|
| 237 |
} |
|
| 238 |
) |
|
| 239 |
) |
|
| 240 |
) |
|
| 241 |
}) |
|
| 242 | ||
| 243 | ! |
lapply( |
| 244 | ! |
datanames, |
| 245 | ! |
function(x) {
|
| 246 | ! |
srv_data_table( |
| 247 | ! |
id = x, |
| 248 | ! |
data = data, |
| 249 | ! |
dataname = x, |
| 250 | ! |
if_filtered = if_filtered, |
| 251 | ! |
if_distinct = if_distinct, |
| 252 | ! |
dt_args = dt_args, |
| 253 | ! |
dt_options = dt_options, |
| 254 | ! |
server_rendering = server_rendering |
| 255 |
) |
|
| 256 |
} |
|
| 257 |
) |
|
| 258 |
}) |
|
| 259 |
} |
|
| 260 | ||
| 261 |
# UI function for the data_table module |
|
| 262 |
ui_data_table <- function(id, choices, selected) {
|
|
| 263 | ! |
ns <- NS(id) |
| 264 | ||
| 265 | ! |
if (!is.null(selected)) {
|
| 266 | ! |
all_choices <- choices |
| 267 | ! |
choices <- c(selected, setdiff(choices, selected)) |
| 268 | ! |
names(choices) <- names(all_choices)[match(choices, all_choices)] |
| 269 |
} |
|
| 270 | ||
| 271 | ! |
tagList( |
| 272 | ! |
teal.widgets::get_dt_rows(ns("data_table"), ns("dt_rows")),
|
| 273 | ! |
bslib::page_fluid( |
| 274 | ! |
teal.widgets::optionalSelectInput( |
| 275 | ! |
ns("variables"),
|
| 276 | ! |
"Select variables:", |
| 277 | ! |
choices = choices, |
| 278 | ! |
selected = selected, |
| 279 | ! |
multiple = TRUE, |
| 280 | ! |
width = "100%" |
| 281 |
) |
|
| 282 |
), |
|
| 283 | ! |
bslib::page_fluid( |
| 284 | ! |
DT::dataTableOutput(ns("data_table"), width = "100%")
|
| 285 |
) |
|
| 286 |
) |
|
| 287 |
} |
|
| 288 | ||
| 289 |
# Server function for the data_table module |
|
| 290 |
srv_data_table <- function(id, |
|
| 291 |
data, |
|
| 292 |
dataname, |
|
| 293 |
if_filtered, |
|
| 294 |
if_distinct, |
|
| 295 |
dt_args, |
|
| 296 |
dt_options, |
|
| 297 |
server_rendering) {
|
|
| 298 | ! |
moduleServer(id, function(input, output, session) {
|
| 299 | ! |
iv <- shinyvalidate::InputValidator$new() |
| 300 | ! |
iv$add_rule("variables", shinyvalidate::sv_required("Please select valid variable names"))
|
| 301 | ! |
iv$add_rule("variables", shinyvalidate::sv_in_set(
|
| 302 | ! |
set = names(isolate(data())[[dataname]]), message_fmt = "Not all selected variables exist in the data" |
| 303 |
)) |
|
| 304 | ! |
iv$enable() |
| 305 | ||
| 306 | ! |
data_table_data <- reactive({
|
| 307 | ! |
df <- data()[[dataname]] |
| 308 | ||
| 309 | ! |
teal::validate_has_data(df, min_nrow = 1L, msg = paste("data", dataname, "is empty"))
|
| 310 | ! |
qenv <- teal.code::eval_code( |
| 311 | ! |
data(), |
| 312 | ! |
'library("dplyr");library("DT")' # nolint quotes
|
| 313 |
) |
|
| 314 | ! |
teal.code::eval_code( |
| 315 | ! |
qenv, |
| 316 | ! |
substitute( |
| 317 | ! |
expr = {
|
| 318 | ! |
variables <- vars |
| 319 | ! |
dataframe_selected <- if (if_distinct) {
|
| 320 | ! |
dplyr::count(dataname, dplyr::across(dplyr::all_of(variables))) |
| 321 |
} else {
|
|
| 322 | ! |
dataname[variables] |
| 323 |
} |
|
| 324 | ! |
dt_args <- args |
| 325 | ! |
dt_args$options <- dt_options |
| 326 | ! |
if (!is.null(dt_rows)) {
|
| 327 | ! |
dt_args$options$pageLength <- dt_rows |
| 328 |
} |
|
| 329 | ! |
dt_args$data <- dataframe_selected |
| 330 | ! |
table <- do.call(DT::datatable, dt_args) |
| 331 |
}, |
|
| 332 | ! |
env = list( |
| 333 | ! |
dataname = as.name(dataname), |
| 334 | ! |
if_distinct = if_distinct(), |
| 335 | ! |
vars = input$variables, |
| 336 | ! |
args = dt_args, |
| 337 | ! |
dt_options = dt_options, |
| 338 | ! |
dt_rows = input$dt_rows |
| 339 |
) |
|
| 340 |
) |
|
| 341 |
) |
|
| 342 |
}) |
|
| 343 | ||
| 344 | ! |
output$data_table <- DT::renderDataTable(server = server_rendering, {
|
| 345 | ! |
teal::validate_inputs(iv) |
| 346 | ! |
req(data_table_data())[["table"]] |
| 347 |
}) |
|
| 348 |
}) |
|
| 349 |
} |
| 1 |
#' `teal` module: Distribution analysis |
|
| 2 |
#' |
|
| 3 |
#' Module is designed to explore the distribution of a single variable within a given dataset. |
|
| 4 |
#' It offers several tools, such as histograms, Q-Q plots, and various statistical tests to |
|
| 5 |
#' visually and statistically analyze the variable's distribution. |
|
| 6 |
#' |
|
| 7 |
#' @inheritParams teal::module |
|
| 8 |
#' @inheritParams teal.widgets::standard_layout |
|
| 9 |
#' @inheritParams shared_params |
|
| 10 |
#' |
|
| 11 |
#' @param dist_var (`data_extract_spec` or `list` of multiple `data_extract_spec`) |
|
| 12 |
#' Variable(s) for which the distribution will be analyzed. |
|
| 13 |
#' @param strata_var (`data_extract_spec` or `list` of multiple `data_extract_spec`) |
|
| 14 |
#' Categorical variable used to split the distribution analysis. |
|
| 15 |
#' @param group_var (`data_extract_spec` or `list` of multiple `data_extract_spec`) |
|
| 16 |
#' Variable used for faceting plot into multiple panels. |
|
| 17 |
#' @param freq (`logical`) optional, whether to display frequency (`TRUE`) or density (`FALSE`). |
|
| 18 |
#' Defaults to density (`FALSE`). |
|
| 19 |
#' @param bins (`integer(1)` or `integer(3)`) optional, specifies the number of bins for the histogram. |
|
| 20 |
#' - When the length of `bins` is one: The histogram bins will have a fixed size based on the `bins` provided. |
|
| 21 |
#' - When the length of `bins` is three: The histogram bins are dynamically adjusted based on vector of `value`, `min`, |
|
| 22 |
#' and `max`. |
|
| 23 |
#' Defaults to `c(30L, 1L, 100L)`. |
|
| 24 |
#' |
|
| 25 |
#' @param ggplot2_args `r roxygen_ggplot2_args_param("Histogram", "QQplot")`
|
|
| 26 |
#' |
|
| 27 |
#' @inherit shared_params return |
|
| 28 |
#' |
|
| 29 |
#' @section Decorating Module: |
|
| 30 |
#' |
|
| 31 |
#' This module generates the following objects, which can be modified in place using decorators:: |
|
| 32 |
#' - `histogram_plot` (`ggplot`) |
|
| 33 |
#' - `qq_plot` (`ggplot`) |
|
| 34 |
#' |
|
| 35 |
#' A Decorator is applied to the specific output using a named list of `teal_transform_module` objects. |
|
| 36 |
#' The name of this list corresponds to the name of the output to which the decorator is applied. |
|
| 37 |
#' See code snippet below: |
|
| 38 |
#' |
|
| 39 |
#' ``` |
|
| 40 |
#' tm_g_distribution( |
|
| 41 |
#' ..., # arguments for module |
|
| 42 |
#' decorators = list( |
|
| 43 |
#' histogram_plot = teal_transform_module(...), # applied only to `histogram_plot` output |
|
| 44 |
#' qq_plot = teal_transform_module(...) # applied only to `qq_plot` output |
|
| 45 |
#' ) |
|
| 46 |
#' ) |
|
| 47 |
#' ``` |
|
| 48 |
#' |
|
| 49 |
#' For additional details and examples of decorators, refer to the vignette |
|
| 50 |
#' `vignette("decorate-module-output", package = "teal.modules.general")`.
|
|
| 51 |
#' |
|
| 52 |
#' To learn more please refer to the vignette |
|
| 53 |
#' `vignette("transform-module-output", package = "teal")` or the [`teal::teal_transform_module()`] documentation.
|
|
| 54 |
#' |
|
| 55 |
#' @examplesShinylive |
|
| 56 |
#' library(teal.modules.general) |
|
| 57 |
#' interactive <- function() TRUE |
|
| 58 |
#' {{ next_example }}
|
|
| 59 |
# nolint start: line_length_linter. |
|
| 60 |
#' @examples |
|
| 61 |
# nolint end: line_length_linter. |
|
| 62 |
#' # general data example |
|
| 63 |
#' data <- teal_data() |
|
| 64 |
#' data <- within(data, {
|
|
| 65 |
#' iris <- iris |
|
| 66 |
#' }) |
|
| 67 |
#' |
|
| 68 |
#' app <- init( |
|
| 69 |
#' data = data, |
|
| 70 |
#' modules = list( |
|
| 71 |
#' tm_g_distribution( |
|
| 72 |
#' dist_var = data_extract_spec( |
|
| 73 |
#' dataname = "iris", |
|
| 74 |
#' select = select_spec(variable_choices("iris"), "Petal.Length")
|
|
| 75 |
#' ) |
|
| 76 |
#' ) |
|
| 77 |
#' ) |
|
| 78 |
#' ) |
|
| 79 |
#' if (interactive()) {
|
|
| 80 |
#' shinyApp(app$ui, app$server) |
|
| 81 |
#' } |
|
| 82 |
#' |
|
| 83 |
#' @examplesShinylive |
|
| 84 |
#' library(teal.modules.general) |
|
| 85 |
#' interactive <- function() TRUE |
|
| 86 |
#' {{ next_example }}
|
|
| 87 |
# nolint start: line_length_linter. |
|
| 88 |
#' @examples |
|
| 89 |
# nolint end: line_length_linter. |
|
| 90 |
#' # CDISC data example |
|
| 91 |
#' data <- teal_data() |
|
| 92 |
#' data <- within(data, {
|
|
| 93 |
#' ADSL <- teal.data::rADSL |
|
| 94 |
#' }) |
|
| 95 |
#' join_keys(data) <- default_cdisc_join_keys[names(data)] |
|
| 96 |
#' |
|
| 97 |
#' vars1 <- choices_selected( |
|
| 98 |
#' variable_choices(data[["ADSL"]], c("ARM", "COUNTRY", "SEX")),
|
|
| 99 |
#' selected = NULL |
|
| 100 |
#' ) |
|
| 101 |
#' |
|
| 102 |
#' app <- init( |
|
| 103 |
#' data = data, |
|
| 104 |
#' modules = modules( |
|
| 105 |
#' tm_g_distribution( |
|
| 106 |
#' dist_var = data_extract_spec( |
|
| 107 |
#' dataname = "ADSL", |
|
| 108 |
#' select = select_spec( |
|
| 109 |
#' choices = variable_choices(data[["ADSL"]], c("AGE", "BMRKR1")),
|
|
| 110 |
#' selected = "BMRKR1", |
|
| 111 |
#' multiple = FALSE, |
|
| 112 |
#' fixed = FALSE |
|
| 113 |
#' ) |
|
| 114 |
#' ), |
|
| 115 |
#' strata_var = data_extract_spec( |
|
| 116 |
#' dataname = "ADSL", |
|
| 117 |
#' filter = filter_spec( |
|
| 118 |
#' vars = vars1, |
|
| 119 |
#' multiple = TRUE |
|
| 120 |
#' ) |
|
| 121 |
#' ), |
|
| 122 |
#' group_var = data_extract_spec( |
|
| 123 |
#' dataname = "ADSL", |
|
| 124 |
#' filter = filter_spec( |
|
| 125 |
#' vars = vars1, |
|
| 126 |
#' multiple = TRUE |
|
| 127 |
#' ) |
|
| 128 |
#' ) |
|
| 129 |
#' ) |
|
| 130 |
#' ) |
|
| 131 |
#' ) |
|
| 132 |
#' if (interactive()) {
|
|
| 133 |
#' shinyApp(app$ui, app$server) |
|
| 134 |
#' } |
|
| 135 |
#' |
|
| 136 |
#' @export |
|
| 137 |
#' |
|
| 138 |
tm_g_distribution <- function(label = "Distribution Module", |
|
| 139 |
dist_var, |
|
| 140 |
strata_var = NULL, |
|
| 141 |
group_var = NULL, |
|
| 142 |
freq = FALSE, |
|
| 143 |
ggtheme = c("gray", "bw", "linedraw", "light", "dark", "minimal", "classic", "void"),
|
|
| 144 |
ggplot2_args = teal.widgets::ggplot2_args(), |
|
| 145 |
bins = c(30L, 1L, 100L), |
|
| 146 |
plot_height = c(600, 200, 2000), |
|
| 147 |
plot_width = NULL, |
|
| 148 |
pre_output = NULL, |
|
| 149 |
post_output = NULL, |
|
| 150 |
transformators = list(), |
|
| 151 |
decorators = list()) {
|
|
| 152 | ! |
message("Initializing tm_g_distribution")
|
| 153 | ||
| 154 |
# Normalize the parameters |
|
| 155 | ! |
if (inherits(dist_var, "data_extract_spec")) dist_var <- list(dist_var) |
| 156 | ! |
if (inherits(strata_var, "data_extract_spec")) strata_var <- list(strata_var) |
| 157 | ! |
if (inherits(group_var, "data_extract_spec")) group_var <- list(group_var) |
| 158 | ! |
if (inherits(ggplot2_args, "ggplot2_args")) ggplot2_args <- list(default = ggplot2_args) |
| 159 | ||
| 160 |
# Start of assertions |
|
| 161 | ! |
checkmate::assert_string(label) |
| 162 | ||
| 163 | ! |
checkmate::assert_list(dist_var, "data_extract_spec") |
| 164 | ! |
checkmate::assert_false(dist_var[[1L]]$select$multiple) |
| 165 | ||
| 166 | ! |
checkmate::assert_list(strata_var, types = "data_extract_spec", null.ok = TRUE) |
| 167 | ! |
checkmate::assert_list(group_var, types = "data_extract_spec", null.ok = TRUE) |
| 168 | ! |
checkmate::assert_flag(freq) |
| 169 | ! |
ggtheme <- match.arg(ggtheme) |
| 170 | ||
| 171 | ! |
plot_choices <- c("Histogram", "QQplot")
|
| 172 | ! |
checkmate::assert_list(ggplot2_args, types = "ggplot2_args") |
| 173 | ! |
checkmate::assert_subset(names(ggplot2_args), c("default", plot_choices))
|
| 174 | ||
| 175 | ! |
if (length(bins) == 1) {
|
| 176 | ! |
checkmate::assert_numeric(bins, any.missing = FALSE, lower = 1) |
| 177 |
} else {
|
|
| 178 | ! |
checkmate::assert_numeric(bins, len = 3, any.missing = FALSE, lower = 1) |
| 179 | ! |
checkmate::assert_numeric(bins[1], lower = bins[2], upper = bins[3], .var.name = "bins") |
| 180 |
} |
|
| 181 | ||
| 182 | ! |
checkmate::assert_numeric(plot_height, len = 3, any.missing = FALSE, finite = TRUE) |
| 183 | ! |
checkmate::assert_numeric(plot_height[1], lower = plot_height[2], upper = plot_height[3], .var.name = "plot_height") |
| 184 | ! |
checkmate::assert_numeric(plot_width, len = 3, any.missing = FALSE, null.ok = TRUE, finite = TRUE) |
| 185 | ! |
checkmate::assert_numeric( |
| 186 | ! |
plot_width[1], |
| 187 | ! |
lower = plot_width[2], upper = plot_width[3], null.ok = TRUE, .var.name = "plot_width" |
| 188 |
) |
|
| 189 | ||
| 190 | ! |
checkmate::assert_multi_class(pre_output, c("shiny.tag", "shiny.tag.list", "html"), null.ok = TRUE)
|
| 191 | ! |
checkmate::assert_multi_class(post_output, c("shiny.tag", "shiny.tag.list", "html"), null.ok = TRUE)
|
| 192 | ||
| 193 | ! |
assert_decorators(decorators, names = c("histogram_plot", "qq_plot"))
|
| 194 | ||
| 195 |
# End of assertions |
|
| 196 | ||
| 197 |
# Make UI args |
|
| 198 | ! |
args <- as.list(environment()) |
| 199 | ||
| 200 | ! |
data_extract_list <- list( |
| 201 | ! |
dist_var = dist_var, |
| 202 | ! |
strata_var = strata_var, |
| 203 | ! |
group_var = group_var |
| 204 |
) |
|
| 205 | ||
| 206 | ! |
ans <- module( |
| 207 | ! |
label = label, |
| 208 | ! |
server = srv_distribution, |
| 209 | ! |
server_args = c( |
| 210 | ! |
data_extract_list, |
| 211 | ! |
list( |
| 212 | ! |
plot_height = plot_height, |
| 213 | ! |
plot_width = plot_width, |
| 214 | ! |
ggplot2_args = ggplot2_args, |
| 215 | ! |
decorators = decorators |
| 216 |
) |
|
| 217 |
), |
|
| 218 | ! |
ui = ui_distribution, |
| 219 | ! |
ui_args = args, |
| 220 | ! |
transformators = transformators, |
| 221 | ! |
datanames = teal.transform::get_extract_datanames(data_extract_list) |
| 222 |
) |
|
| 223 | ! |
attr(ans, "teal_bookmarkable") <- TRUE |
| 224 | ! |
ans |
| 225 |
} |
|
| 226 | ||
| 227 |
# UI function for the distribution module |
|
| 228 |
ui_distribution <- function(id, ...) {
|
|
| 229 | ! |
args <- list(...) |
| 230 | ! |
ns <- NS(id) |
| 231 | ! |
is_single_dataset_value <- teal.transform::is_single_dataset(args$dist_var, args$strata_var, args$group_var) |
| 232 | ||
| 233 | ! |
teal.widgets::standard_layout( |
| 234 | ! |
output = teal.widgets::white_small_well( |
| 235 | ! |
tabsetPanel( |
| 236 | ! |
id = ns("tabs"),
|
| 237 | ! |
tabPanel("Histogram", teal.widgets::plot_with_settings_ui(id = ns("hist_plot"))),
|
| 238 | ! |
tabPanel("QQplot", teal.widgets::plot_with_settings_ui(id = ns("qq_plot")))
|
| 239 |
), |
|
| 240 | ! |
tags$h3("Statistics Table"),
|
| 241 | ! |
DT::dataTableOutput(ns("summary_table")),
|
| 242 | ! |
tags$h3("Tests"),
|
| 243 | ! |
conditionalPanel( |
| 244 | ! |
sprintf("input['%s'].length === 0", ns("dist_tests")),
|
| 245 | ! |
div( |
| 246 | ! |
id = ns("please_select_a_test"),
|
| 247 | ! |
"Please select a test" |
| 248 |
) |
|
| 249 |
), |
|
| 250 | ! |
conditionalPanel( |
| 251 | ! |
sprintf("input['%s'].length > 0", ns("dist_tests")),
|
| 252 | ! |
DT::dataTableOutput(ns("t_stats"))
|
| 253 |
) |
|
| 254 |
), |
|
| 255 | ! |
encoding = tags$div( |
| 256 |
### Reporter |
|
| 257 | ! |
teal.reporter::add_card_button_ui(ns("add_reporter"), label = "Add Report Card"),
|
| 258 | ! |
tags$br(), tags$br(), |
| 259 |
### |
|
| 260 | ! |
tags$label("Encodings", class = "text-primary"),
|
| 261 | ! |
teal.transform::datanames_input(args[c("dist_var", "strata_var")]),
|
| 262 | ! |
teal.transform::data_extract_ui( |
| 263 | ! |
id = ns("dist_i"),
|
| 264 | ! |
label = "Variable", |
| 265 | ! |
data_extract_spec = args$dist_var, |
| 266 | ! |
is_single_dataset = is_single_dataset_value |
| 267 |
), |
|
| 268 | ! |
if (!is.null(args$group_var)) {
|
| 269 | ! |
tagList( |
| 270 | ! |
teal.transform::data_extract_ui( |
| 271 | ! |
id = ns("group_i"),
|
| 272 | ! |
label = "Group by", |
| 273 | ! |
data_extract_spec = args$group_var, |
| 274 | ! |
is_single_dataset = is_single_dataset_value |
| 275 |
), |
|
| 276 | ! |
uiOutput(ns("scales_types_ui"))
|
| 277 |
) |
|
| 278 |
}, |
|
| 279 | ! |
if (!is.null(args$strata_var)) {
|
| 280 | ! |
teal.transform::data_extract_ui( |
| 281 | ! |
id = ns("strata_i"),
|
| 282 | ! |
label = "Stratify by", |
| 283 | ! |
data_extract_spec = args$strata_var, |
| 284 | ! |
is_single_dataset = is_single_dataset_value |
| 285 |
) |
|
| 286 |
}, |
|
| 287 | ! |
bslib::accordion( |
| 288 | ! |
conditionalPanel( |
| 289 | ! |
condition = paste0("input['", ns("tabs"), "'] == 'Histogram'"),
|
| 290 | ! |
bslib::accordion_panel( |
| 291 | ! |
"Histogram", |
| 292 | ! |
teal.widgets::optionalSliderInputValMinMax(ns("bins"), "Bins", args$bins, ticks = FALSE, step = 1),
|
| 293 | ! |
shinyWidgets::prettyRadioButtons( |
| 294 | ! |
ns("main_type"),
|
| 295 | ! |
label = "Plot Type:", |
| 296 | ! |
choices = c("Density", "Frequency"),
|
| 297 | ! |
selected = if (!args$freq) "Density" else "Frequency", |
| 298 | ! |
bigger = FALSE, |
| 299 | ! |
inline = TRUE |
| 300 |
), |
|
| 301 | ! |
checkboxInput(ns("add_dens"), label = "Overlay Density", value = TRUE),
|
| 302 | ! |
ui_decorate_teal_data( |
| 303 | ! |
ns("d_density"),
|
| 304 | ! |
decorators = select_decorators(args$decorators, "histogram_plot") |
| 305 |
) |
|
| 306 |
) |
|
| 307 |
), |
|
| 308 | ! |
conditionalPanel( |
| 309 | ! |
condition = paste0("input['", ns("tabs"), "'] == 'QQplot'"),
|
| 310 | ! |
bslib::accordion_panel( |
| 311 | ! |
"QQ Plot", |
| 312 | ! |
checkboxInput(ns("qq_line"), label = "Add diagonal line(s)", TRUE),
|
| 313 | ! |
ui_decorate_teal_data( |
| 314 | ! |
ns("d_qq"),
|
| 315 | ! |
decorators = select_decorators(args$decorators, "qq_plot") |
| 316 |
), |
|
| 317 | ! |
collapsed = FALSE |
| 318 |
) |
|
| 319 |
), |
|
| 320 | ! |
conditionalPanel( |
| 321 | ! |
condition = paste0("input['", ns("main_type"), "'] == 'Density'"),
|
| 322 | ! |
bslib::accordion_panel( |
| 323 | ! |
"Theoretical Distribution", |
| 324 | ! |
teal.widgets::optionalSelectInput( |
| 325 | ! |
ns("t_dist"),
|
| 326 | ! |
tags$div( |
| 327 | ! |
tagList( |
| 328 | ! |
"Distribution:", |
| 329 | ! |
bslib::tooltip( |
| 330 | ! |
icon("circle-info"),
|
| 331 | ! |
tags$span( |
| 332 | ! |
"Default parameters are optimized with MASS::fitdistr function." |
| 333 |
) |
|
| 334 |
) |
|
| 335 |
) |
|
| 336 |
), |
|
| 337 | ! |
choices = c("normal", "lognormal", "gamma", "unif"),
|
| 338 | ! |
selected = NULL, |
| 339 | ! |
multiple = FALSE |
| 340 |
), |
|
| 341 | ! |
numericInput(ns("dist_param1"), label = "param1", value = NULL),
|
| 342 | ! |
numericInput(ns("dist_param2"), label = "param2", value = NULL),
|
| 343 | ! |
tags$span(actionButton(ns("params_reset"), "Default params")),
|
| 344 | ! |
collapsed = FALSE |
| 345 |
) |
|
| 346 |
), |
|
| 347 | ! |
bslib::accordion_panel( |
| 348 | ! |
title = "Tests", |
| 349 | ! |
teal.widgets::optionalSelectInput( |
| 350 | ! |
ns("dist_tests"),
|
| 351 | ! |
"Tests:", |
| 352 | ! |
choices = c( |
| 353 | ! |
"Shapiro-Wilk", |
| 354 | ! |
if (!is.null(args$strata_var)) "t-test (two-samples, not paired)", |
| 355 | ! |
if (!is.null(args$strata_var)) "one-way ANOVA", |
| 356 | ! |
if (!is.null(args$strata_var)) "Fligner-Killeen", |
| 357 | ! |
if (!is.null(args$strata_var)) "F-test", |
| 358 | ! |
"Kolmogorov-Smirnov (one-sample)", |
| 359 | ! |
"Anderson-Darling (one-sample)", |
| 360 | ! |
"Cramer-von Mises (one-sample)", |
| 361 | ! |
if (!is.null(args$strata_var)) "Kolmogorov-Smirnov (two-samples)" |
| 362 |
), |
|
| 363 | ! |
selected = NULL |
| 364 |
) |
|
| 365 |
), |
|
| 366 | ! |
bslib::accordion_panel( |
| 367 | ! |
title = "Statistics Table", |
| 368 | ! |
sliderInput(ns("roundn"), "Round to n digits", min = 0, max = 10, value = 2)
|
| 369 |
), |
|
| 370 | ! |
bslib::accordion_panel( |
| 371 | ! |
title = "Plot settings", |
| 372 | ! |
selectInput( |
| 373 | ! |
inputId = ns("ggtheme"),
|
| 374 | ! |
label = "Theme (by ggplot):", |
| 375 | ! |
choices = ggplot_themes, |
| 376 | ! |
selected = args$ggtheme, |
| 377 | ! |
multiple = FALSE |
| 378 |
) |
|
| 379 |
) |
|
| 380 |
) |
|
| 381 |
), |
|
| 382 | ! |
forms = tagList( |
| 383 | ! |
teal.widgets::verbatim_popup_ui(ns("rcode"), "Show R code")
|
| 384 |
), |
|
| 385 | ! |
pre_output = args$pre_output, |
| 386 | ! |
post_output = args$post_output |
| 387 |
) |
|
| 388 |
} |
|
| 389 | ||
| 390 |
# Server function for the distribution module |
|
| 391 |
srv_distribution <- function(id, |
|
| 392 |
data, |
|
| 393 |
reporter, |
|
| 394 |
filter_panel_api, |
|
| 395 |
dist_var, |
|
| 396 |
strata_var, |
|
| 397 |
group_var, |
|
| 398 |
plot_height, |
|
| 399 |
plot_width, |
|
| 400 |
ggplot2_args, |
|
| 401 |
decorators) {
|
|
| 402 | ! |
with_reporter <- !missing(reporter) && inherits(reporter, "Reporter") |
| 403 | ! |
with_filter <- !missing(filter_panel_api) && inherits(filter_panel_api, "FilterPanelAPI") |
| 404 | ! |
checkmate::assert_class(data, "reactive") |
| 405 | ! |
checkmate::assert_class(isolate(data()), "teal_data") |
| 406 | ! |
moduleServer(id, function(input, output, session) {
|
| 407 | ! |
teal.logger::log_shiny_input_changes(input, namespace = "teal.modules.general") |
| 408 | ||
| 409 | ! |
setBookmarkExclude("params_reset")
|
| 410 | ||
| 411 | ! |
ns <- session$ns |
| 412 | ||
| 413 | ! |
rule_req <- function(value) {
|
| 414 | ! |
if (isTRUE(input$dist_tests %in% c( |
| 415 | ! |
"Fligner-Killeen", |
| 416 | ! |
"t-test (two-samples, not paired)", |
| 417 | ! |
"F-test", |
| 418 | ! |
"Kolmogorov-Smirnov (two-samples)", |
| 419 | ! |
"one-way ANOVA" |
| 420 |
))) {
|
|
| 421 | ! |
if (!shinyvalidate::input_provided(value)) {
|
| 422 | ! |
"Please select stratify variable." |
| 423 |
} |
|
| 424 |
} |
|
| 425 |
} |
|
| 426 | ! |
rule_dupl <- function(...) {
|
| 427 | ! |
if (identical(input$dist_tests, "Fligner-Killeen")) {
|
| 428 | ! |
strata <- selector_list()$strata_i()$select |
| 429 | ! |
group <- selector_list()$group_i()$select |
| 430 | ! |
if (isTRUE(strata == group)) {
|
| 431 | ! |
"Please select different variables for strata and group." |
| 432 |
} |
|
| 433 |
} |
|
| 434 |
} |
|
| 435 | ||
| 436 | ! |
selector_list <- teal.transform::data_extract_multiple_srv( |
| 437 | ! |
data_extract = list( |
| 438 | ! |
dist_i = dist_var, |
| 439 | ! |
strata_i = strata_var, |
| 440 | ! |
group_i = group_var |
| 441 |
), |
|
| 442 | ! |
data, |
| 443 | ! |
select_validation_rule = list( |
| 444 | ! |
dist_i = shinyvalidate::sv_required("Please select a variable")
|
| 445 |
), |
|
| 446 | ! |
filter_validation_rule = list( |
| 447 | ! |
strata_i = shinyvalidate::compose_rules( |
| 448 | ! |
rule_req, |
| 449 | ! |
rule_dupl |
| 450 |
), |
|
| 451 | ! |
group_i = rule_dupl |
| 452 |
) |
|
| 453 |
) |
|
| 454 | ||
| 455 | ! |
iv_r <- reactive({
|
| 456 | ! |
iv <- shinyvalidate::InputValidator$new() |
| 457 | ! |
teal.transform::compose_and_enable_validators(iv, selector_list, validator_names = "dist_i") |
| 458 |
}) |
|
| 459 | ||
| 460 | ! |
iv_r_dist <- reactive({
|
| 461 | ! |
iv <- shinyvalidate::InputValidator$new() |
| 462 | ! |
teal.transform::compose_and_enable_validators( |
| 463 | ! |
iv, selector_list, |
| 464 | ! |
validator_names = c("strata_i", "group_i")
|
| 465 |
) |
|
| 466 |
}) |
|
| 467 | ! |
rule_dist_1 <- function(value) {
|
| 468 | ! |
if (!is.null(input$t_dist)) {
|
| 469 | ! |
switch(input$t_dist, |
| 470 | ! |
"normal" = if (!shinyvalidate::input_provided(value)) "mean is required", |
| 471 | ! |
"lognormal" = if (!shinyvalidate::input_provided(value)) "meanlog is required", |
| 472 | ! |
"gamma" = {
|
| 473 | ! |
if (!shinyvalidate::input_provided(value)) "shape is required" else if (value <= 0) "shape must be positive" |
| 474 |
}, |
|
| 475 | ! |
"unif" = NULL |
| 476 |
) |
|
| 477 |
} |
|
| 478 |
} |
|
| 479 | ! |
rule_dist_2 <- function(value) {
|
| 480 | ! |
if (!is.null(input$t_dist)) {
|
| 481 | ! |
switch(input$t_dist, |
| 482 | ! |
"normal" = {
|
| 483 | ! |
if (!shinyvalidate::input_provided(value)) {
|
| 484 | ! |
"sd is required" |
| 485 | ! |
} else if (value < 0) {
|
| 486 | ! |
"sd must be non-negative" |
| 487 |
} |
|
| 488 |
}, |
|
| 489 | ! |
"lognormal" = {
|
| 490 | ! |
if (!shinyvalidate::input_provided(value)) {
|
| 491 | ! |
"sdlog is required" |
| 492 | ! |
} else if (value < 0) {
|
| 493 | ! |
"sdlog must be non-negative" |
| 494 |
} |
|
| 495 |
}, |
|
| 496 | ! |
"gamma" = {
|
| 497 | ! |
if (!shinyvalidate::input_provided(value)) {
|
| 498 | ! |
"rate is required" |
| 499 | ! |
} else if (value <= 0) {
|
| 500 | ! |
"rate must be positive" |
| 501 |
} |
|
| 502 |
}, |
|
| 503 | ! |
"unif" = NULL |
| 504 |
) |
|
| 505 |
} |
|
| 506 |
} |
|
| 507 | ||
| 508 | ! |
rule_dist <- function(value) {
|
| 509 | ! |
if (isTRUE(input$tabs == "QQplot") || |
| 510 | ! |
isTRUE(input$dist_tests %in% c( |
| 511 | ! |
"Kolmogorov-Smirnov (one-sample)", |
| 512 | ! |
"Anderson-Darling (one-sample)", |
| 513 | ! |
"Cramer-von Mises (one-sample)" |
| 514 |
))) {
|
|
| 515 | ! |
if (!shinyvalidate::input_provided(value)) {
|
| 516 | ! |
"Please select the theoretical distribution." |
| 517 |
} |
|
| 518 |
} |
|
| 519 |
} |
|
| 520 | ||
| 521 | ! |
iv_dist <- shinyvalidate::InputValidator$new() |
| 522 | ! |
iv_dist$add_rule("t_dist", rule_dist)
|
| 523 | ! |
iv_dist$add_rule("dist_param1", rule_dist_1)
|
| 524 | ! |
iv_dist$add_rule("dist_param2", rule_dist_2)
|
| 525 | ! |
iv_dist$enable() |
| 526 | ||
| 527 | ! |
anl_merged_input <- teal.transform::merge_expression_srv( |
| 528 | ! |
selector_list = selector_list, |
| 529 | ! |
datasets = data |
| 530 |
) |
|
| 531 | ||
| 532 | ! |
qenv <- reactive( |
| 533 | ! |
teal.code::eval_code(data(), 'library("ggplot2");library("dplyr")') # nolint quotes
|
| 534 |
) |
|
| 535 | ||
| 536 | ! |
anl_merged_q <- reactive({
|
| 537 | ! |
req(anl_merged_input()) |
| 538 | ! |
qenv() %>% |
| 539 | ! |
teal.code::eval_code(as.expression(anl_merged_input()$expr)) |
| 540 |
}) |
|
| 541 | ||
| 542 | ! |
merged <- list( |
| 543 | ! |
anl_input_r = anl_merged_input, |
| 544 | ! |
anl_q_r = anl_merged_q |
| 545 |
) |
|
| 546 | ||
| 547 | ! |
output$scales_types_ui <- renderUI({
|
| 548 | ! |
if ("group_i" %in% names(selector_list()) && length(selector_list()$group_i()$filters[[1]]$selected) > 0) {
|
| 549 | ! |
shinyWidgets::prettyRadioButtons( |
| 550 | ! |
ns("scales_type"),
|
| 551 | ! |
label = "Scales:", |
| 552 | ! |
choices = c("Fixed", "Free"),
|
| 553 | ! |
selected = "Fixed", |
| 554 | ! |
bigger = FALSE, |
| 555 | ! |
inline = TRUE |
| 556 |
) |
|
| 557 |
} |
|
| 558 |
}) |
|
| 559 | ||
| 560 | ! |
observeEvent( |
| 561 | ! |
eventExpr = list( |
| 562 | ! |
input$t_dist, |
| 563 | ! |
input$params_reset, |
| 564 | ! |
selector_list()$dist_i()$select |
| 565 |
), |
|
| 566 | ! |
handlerExpr = {
|
| 567 | ! |
params <- |
| 568 | ! |
if (length(input$t_dist) != 0) {
|
| 569 | ! |
get_dist_params <- function(x, dist) {
|
| 570 | ! |
if (dist == "unif") {
|
| 571 | ! |
return(stats::setNames(range(x, na.rm = TRUE), c("min", "max")))
|
| 572 |
} |
|
| 573 | ! |
tryCatch( |
| 574 | ! |
MASS::fitdistr(x, densfun = dist)$estimate, |
| 575 | ! |
error = function(e) c(param1 = NA_real_, param2 = NA_real_) |
| 576 |
) |
|
| 577 |
} |
|
| 578 | ||
| 579 | ! |
ANL <- merged$anl_q_r()[["ANL"]] |
| 580 | ! |
round(get_dist_params(as.numeric(stats::na.omit(ANL[[merge_vars()$dist_var]])), input$t_dist), 2) |
| 581 |
} else {
|
|
| 582 | ! |
c("param1" = NA_real_, "param2" = NA_real_)
|
| 583 |
} |
|
| 584 | ||
| 585 | ! |
params_vals <- unname(params) |
| 586 | ! |
map_distr_nams <- list( |
| 587 | ! |
normal = c("mean", "sd"),
|
| 588 | ! |
lognormal = c("meanlog", "sdlog"),
|
| 589 | ! |
gamma = c("shape", "rate"),
|
| 590 | ! |
unif = c("min", "max")
|
| 591 |
) |
|
| 592 | ||
| 593 | ! |
if (!is.null(input$t_dist) && input$t_dist %in% names(map_distr_nams)) {
|
| 594 | ! |
params_names <- map_distr_nams[[input$t_dist]] |
| 595 |
} else {
|
|
| 596 | ! |
params_names <- names(params) |
| 597 |
} |
|
| 598 | ||
| 599 | ! |
updateNumericInput( |
| 600 | ! |
inputId = "dist_param1", |
| 601 | ! |
label = params_names[1], |
| 602 | ! |
value = restoreInput(ns("dist_param1"), params_vals[1])
|
| 603 |
) |
|
| 604 | ! |
updateNumericInput( |
| 605 | ! |
inputId = "dist_param2", |
| 606 | ! |
label = params_names[2], |
| 607 | ! |
value = restoreInput(ns("dist_param1"), params_vals[2])
|
| 608 |
) |
|
| 609 |
}, |
|
| 610 | ! |
ignoreInit = TRUE |
| 611 |
) |
|
| 612 | ||
| 613 | ! |
observeEvent(input$params_reset, {
|
| 614 | ! |
updateActionButton(inputId = "params_reset", label = "Reset params") |
| 615 |
}) |
|
| 616 | ||
| 617 | ! |
merge_vars <- reactive({
|
| 618 | ! |
teal::validate_inputs(iv_r()) |
| 619 | ||
| 620 | ! |
dist_var <- as.vector(merged$anl_input_r()$columns_source$dist_i) |
| 621 | ! |
s_var <- as.vector(merged$anl_input_r()$columns_source$strata_i) |
| 622 | ! |
g_var <- as.vector(merged$anl_input_r()$columns_source$group_i) |
| 623 | ||
| 624 | ! |
dist_var_name <- if (length(dist_var)) as.name(dist_var) else NULL |
| 625 | ! |
s_var_name <- if (length(s_var)) as.name(s_var) else NULL |
| 626 | ! |
g_var_name <- if (length(g_var)) as.name(g_var) else NULL |
| 627 | ||
| 628 | ! |
list( |
| 629 | ! |
dist_var = dist_var, |
| 630 | ! |
s_var = s_var, |
| 631 | ! |
g_var = g_var, |
| 632 | ! |
dist_var_name = dist_var_name, |
| 633 | ! |
s_var_name = s_var_name, |
| 634 | ! |
g_var_name = g_var_name |
| 635 |
) |
|
| 636 |
}) |
|
| 637 | ||
| 638 |
# common qenv |
|
| 639 | ! |
common_q <- reactive({
|
| 640 |
# Create a private stack for this function only. |
|
| 641 | ||
| 642 | ! |
ANL <- merged$anl_q_r()[["ANL"]] |
| 643 | ! |
dist_var <- merge_vars()$dist_var |
| 644 | ! |
s_var <- merge_vars()$s_var |
| 645 | ! |
g_var <- merge_vars()$g_var |
| 646 | ||
| 647 | ! |
dist_var_name <- merge_vars()$dist_var_name |
| 648 | ! |
s_var_name <- merge_vars()$s_var_name |
| 649 | ! |
g_var_name <- merge_vars()$g_var_name |
| 650 | ||
| 651 | ! |
roundn <- input$roundn |
| 652 | ! |
dist_param1 <- input$dist_param1 |
| 653 | ! |
dist_param2 <- input$dist_param2 |
| 654 |
# isolated as dist_param1/dist_param2 already triggered the reactivity |
|
| 655 | ! |
t_dist <- isolate(input$t_dist) |
| 656 | ||
| 657 | ! |
qenv <- merged$anl_q_r() |
| 658 | ||
| 659 | ! |
if (length(g_var) > 0) {
|
| 660 | ! |
validate( |
| 661 | ! |
need( |
| 662 | ! |
inherits(ANL[[g_var]], c("integer", "factor", "character")),
|
| 663 | ! |
"Group by variable must be `factor`, `character`, or `integer`" |
| 664 |
) |
|
| 665 |
) |
|
| 666 | ! |
qenv <- teal.code::eval_code(qenv, 'library("forcats")') # nolint quotes
|
| 667 | ! |
qenv <- teal.code::eval_code( |
| 668 | ! |
qenv, |
| 669 | ! |
substitute( |
| 670 | ! |
expr = ANL[[g_var]] <- forcats::fct_na_value_to_level(as.factor(ANL[[g_var]]), "NA"), |
| 671 | ! |
env = list(g_var = g_var) |
| 672 |
) |
|
| 673 |
) |
|
| 674 |
} |
|
| 675 | ||
| 676 | ! |
if (length(s_var) > 0) {
|
| 677 | ! |
validate( |
| 678 | ! |
need( |
| 679 | ! |
inherits(ANL[[s_var]], c("integer", "factor", "character")),
|
| 680 | ! |
"Stratify by variable must be `factor`, `character`, or `integer`" |
| 681 |
) |
|
| 682 |
) |
|
| 683 | ||
| 684 | ! |
qenv <- teal.code::eval_code(qenv, 'library("forcats")') # nolint quotes
|
| 685 | ! |
qenv <- teal.code::eval_code( |
| 686 | ! |
qenv, |
| 687 | ! |
substitute( |
| 688 | ! |
expr = ANL[[s_var]] <- forcats::fct_na_value_to_level(as.factor(ANL[[s_var]]), "NA"), |
| 689 | ! |
env = list(s_var = s_var) |
| 690 |
) |
|
| 691 |
) |
|
| 692 |
} |
|
| 693 | ||
| 694 | ! |
validate(need(is.numeric(ANL[[dist_var]]), "Please select a numeric variable.")) |
| 695 | ! |
teal::validate_has_data(ANL, 1, complete = TRUE) |
| 696 | ||
| 697 | ! |
if (length(t_dist) != 0) {
|
| 698 | ! |
map_distr_nams <- list( |
| 699 | ! |
normal = c("mean", "sd"),
|
| 700 | ! |
lognormal = c("meanlog", "sdlog"),
|
| 701 | ! |
gamma = c("shape", "rate"),
|
| 702 | ! |
unif = c("min", "max")
|
| 703 |
) |
|
| 704 | ! |
params_names_raw <- map_distr_nams[[t_dist]] |
| 705 | ||
| 706 | ! |
qenv <- teal.code::eval_code( |
| 707 | ! |
qenv, |
| 708 | ! |
substitute( |
| 709 | ! |
expr = {
|
| 710 | ! |
params <- as.list(c(dist_param1, dist_param2)) |
| 711 | ! |
names(params) <- params_names_raw |
| 712 |
}, |
|
| 713 | ! |
env = list( |
| 714 | ! |
dist_param1 = dist_param1, |
| 715 | ! |
dist_param2 = dist_param2, |
| 716 | ! |
params_names_raw = params_names_raw |
| 717 |
) |
|
| 718 |
) |
|
| 719 |
) |
|
| 720 |
} |
|
| 721 | ||
| 722 | ! |
if (length(s_var) == 0 && length(g_var) == 0) {
|
| 723 | ! |
teal.code::eval_code( |
| 724 | ! |
qenv, |
| 725 | ! |
substitute( |
| 726 | ! |
expr = {
|
| 727 | ! |
summary_table_data <- ANL %>% |
| 728 | ! |
dplyr::summarise( |
| 729 | ! |
min = round(min(dist_var_name, na.rm = TRUE), roundn), |
| 730 | ! |
median = round(stats::median(dist_var_name, na.rm = TRUE), roundn), |
| 731 | ! |
mean = round(mean(dist_var_name, na.rm = TRUE), roundn), |
| 732 | ! |
max = round(max(dist_var_name, na.rm = TRUE), roundn), |
| 733 | ! |
sd = round(stats::sd(dist_var_name, na.rm = TRUE), roundn), |
| 734 | ! |
count = dplyr::n() |
| 735 |
) |
|
| 736 |
}, |
|
| 737 | ! |
env = list( |
| 738 | ! |
dist_var_name = as.name(dist_var), |
| 739 | ! |
roundn = roundn |
| 740 |
) |
|
| 741 |
) |
|
| 742 |
) |
|
| 743 |
} else {
|
|
| 744 | ! |
teal.code::eval_code( |
| 745 | ! |
qenv, |
| 746 | ! |
substitute( |
| 747 | ! |
expr = {
|
| 748 | ! |
strata_vars <- strata_vars_raw |
| 749 | ! |
summary_table_data <- ANL %>% |
| 750 | ! |
dplyr::group_by_at(dplyr::vars(dplyr::any_of(strata_vars))) %>% |
| 751 | ! |
dplyr::summarise( |
| 752 | ! |
min = round(min(dist_var_name, na.rm = TRUE), roundn), |
| 753 | ! |
median = round(stats::median(dist_var_name, na.rm = TRUE), roundn), |
| 754 | ! |
mean = round(mean(dist_var_name, na.rm = TRUE), roundn), |
| 755 | ! |
max = round(max(dist_var_name, na.rm = TRUE), roundn), |
| 756 | ! |
sd = round(stats::sd(dist_var_name, na.rm = TRUE), roundn), |
| 757 | ! |
count = dplyr::n() |
| 758 |
) |
|
| 759 |
}, |
|
| 760 | ! |
env = list( |
| 761 | ! |
dist_var_name = dist_var_name, |
| 762 | ! |
strata_vars_raw = c(g_var, s_var), |
| 763 | ! |
roundn = roundn |
| 764 |
) |
|
| 765 |
) |
|
| 766 |
) |
|
| 767 |
} |
|
| 768 |
}) |
|
| 769 | ||
| 770 |
# distplot qenv ---- |
|
| 771 | ! |
dist_q <- eventReactive( |
| 772 | ! |
eventExpr = {
|
| 773 | ! |
common_q() |
| 774 | ! |
input$scales_type |
| 775 | ! |
input$main_type |
| 776 | ! |
input$bins |
| 777 | ! |
input$add_dens |
| 778 | ! |
is.null(input$ggtheme) |
| 779 |
}, |
|
| 780 | ! |
valueExpr = {
|
| 781 | ! |
dist_var <- merge_vars()$dist_var |
| 782 | ! |
s_var <- merge_vars()$s_var |
| 783 | ! |
g_var <- merge_vars()$g_var |
| 784 | ! |
dist_var_name <- merge_vars()$dist_var_name |
| 785 | ! |
s_var_name <- merge_vars()$s_var_name |
| 786 | ! |
g_var_name <- merge_vars()$g_var_name |
| 787 | ! |
t_dist <- input$t_dist |
| 788 | ! |
dist_param1 <- input$dist_param1 |
| 789 | ! |
dist_param2 <- input$dist_param2 |
| 790 | ||
| 791 | ! |
scales_type <- input$scales_type |
| 792 | ||
| 793 | ! |
ndensity <- 512 |
| 794 | ! |
main_type_var <- input$main_type |
| 795 | ! |
bins_var <- input$bins |
| 796 | ! |
add_dens_var <- input$add_dens |
| 797 | ! |
ggtheme <- input$ggtheme |
| 798 | ||
| 799 | ! |
teal::validate_inputs(iv_dist) |
| 800 | ||
| 801 | ! |
qenv <- common_q() |
| 802 | ||
| 803 | ! |
m_type <- if (main_type_var == "Density") "density" else "count" |
| 804 | ||
| 805 | ! |
plot_call <- if (length(s_var) == 0 && length(g_var) == 0) {
|
| 806 | ! |
substitute( |
| 807 | ! |
expr = ggplot2::ggplot(ANL, ggplot2::aes(dist_var_name)) + |
| 808 | ! |
ggplot2::geom_histogram( |
| 809 | ! |
position = "identity", ggplot2::aes(y = ggplot2::after_stat(m_type)), bins = bins_var, alpha = 0.3 |
| 810 |
), |
|
| 811 | ! |
env = list( |
| 812 | ! |
m_type = as.name(m_type), bins_var = bins_var, dist_var_name = as.name(dist_var) |
| 813 |
) |
|
| 814 |
) |
|
| 815 | ! |
} else if (length(s_var) != 0 && length(g_var) == 0) {
|
| 816 | ! |
substitute( |
| 817 | ! |
expr = ggplot2::ggplot(ANL, ggplot2::aes(dist_var_name, col = s_var_name)) + |
| 818 | ! |
ggplot2::geom_histogram( |
| 819 | ! |
position = "identity", ggplot2::aes(y = ggplot2::after_stat(m_type), fill = s_var), |
| 820 | ! |
bins = bins_var, alpha = 0.3 |
| 821 |
), |
|
| 822 | ! |
env = list( |
| 823 | ! |
m_type = as.name(m_type), |
| 824 | ! |
bins_var = bins_var, |
| 825 | ! |
dist_var_name = dist_var_name, |
| 826 | ! |
s_var = as.name(s_var), |
| 827 | ! |
s_var_name = s_var_name |
| 828 |
) |
|
| 829 |
) |
|
| 830 | ! |
} else if (length(s_var) == 0 && length(g_var) != 0) {
|
| 831 | ! |
req(scales_type) |
| 832 | ! |
substitute( |
| 833 | ! |
expr = ggplot2::ggplot(ANL[ANL[[g_var]] != "NA", ], ggplot2::aes(dist_var_name)) + |
| 834 | ! |
ggplot2::geom_histogram( |
| 835 | ! |
position = "identity", ggplot2::aes(y = ggplot2::after_stat(m_type)), bins = bins_var, alpha = 0.3 |
| 836 |
) + |
|
| 837 | ! |
ggplot2::facet_wrap(~g_var_name, ncol = 1, scales = scales_raw), |
| 838 | ! |
env = list( |
| 839 | ! |
m_type = as.name(m_type), |
| 840 | ! |
bins_var = bins_var, |
| 841 | ! |
dist_var_name = dist_var_name, |
| 842 | ! |
g_var = g_var, |
| 843 | ! |
g_var_name = g_var_name, |
| 844 | ! |
scales_raw = tolower(scales_type) |
| 845 |
) |
|
| 846 |
) |
|
| 847 |
} else {
|
|
| 848 | ! |
req(scales_type) |
| 849 | ! |
substitute( |
| 850 | ! |
expr = ggplot2::ggplot(ANL[ANL[[g_var]] != "NA", ], ggplot2::aes(dist_var_name, col = s_var_name)) + |
| 851 | ! |
ggplot2::geom_histogram( |
| 852 | ! |
position = "identity", |
| 853 | ! |
ggplot2::aes(y = ggplot2::after_stat(m_type), fill = s_var), bins = bins_var, alpha = 0.3 |
| 854 |
) + |
|
| 855 | ! |
ggplot2::facet_wrap(~g_var_name, ncol = 1, scales = scales_raw), |
| 856 | ! |
env = list( |
| 857 | ! |
m_type = as.name(m_type), |
| 858 | ! |
bins_var = bins_var, |
| 859 | ! |
dist_var_name = dist_var_name, |
| 860 | ! |
g_var = g_var, |
| 861 | ! |
s_var = as.name(s_var), |
| 862 | ! |
g_var_name = g_var_name, |
| 863 | ! |
s_var_name = s_var_name, |
| 864 | ! |
scales_raw = tolower(scales_type) |
| 865 |
) |
|
| 866 |
) |
|
| 867 |
} |
|
| 868 | ||
| 869 | ! |
if (add_dens_var) {
|
| 870 | ! |
plot_call <- substitute( |
| 871 | ! |
expr = plot_call + |
| 872 | ! |
ggplot2::stat_density( |
| 873 | ! |
ggplot2::aes(y = ggplot2::after_stat(const * m_type2)), |
| 874 | ! |
geom = "line", |
| 875 | ! |
position = "identity", |
| 876 | ! |
alpha = 0.5, |
| 877 | ! |
size = 2, |
| 878 | ! |
n = ndensity |
| 879 |
), |
|
| 880 | ! |
env = list( |
| 881 | ! |
plot_call = plot_call, |
| 882 | ! |
const = if (main_type_var == "Density") {
|
| 883 | ! |
1 |
| 884 |
} else {
|
|
| 885 | ! |
diff(range(qenv[["ANL"]][[dist_var]], na.rm = TRUE)) / bins_var |
| 886 |
}, |
|
| 887 | ! |
m_type2 = if (main_type_var == "Density") as.name("density") else as.name("count"),
|
| 888 | ! |
ndensity = ndensity |
| 889 |
) |
|
| 890 |
) |
|
| 891 |
} |
|
| 892 | ||
| 893 | ! |
if (length(t_dist) != 0 && main_type_var == "Density" && length(g_var) == 0 && length(s_var) == 0) {
|
| 894 | ! |
qenv <- teal.code::eval_code(qenv, 'library("ggpp")') # nolint quotes
|
| 895 | ! |
qenv <- teal.code::eval_code( |
| 896 | ! |
qenv, |
| 897 | ! |
substitute( |
| 898 | ! |
df_params <- as.data.frame(append(params, list(name = t_dist))), |
| 899 | ! |
env = list(t_dist = t_dist) |
| 900 |
) |
|
| 901 |
) |
|
| 902 | ! |
datas <- quote(data.frame(x = 0.7, y = 1, tb = I(list(df_params = df_params)))) |
| 903 | ! |
label <- quote(tb) |
| 904 | ||
| 905 | ! |
plot_call <- substitute( |
| 906 | ! |
expr = plot_call + ggpp::geom_table_npc( |
| 907 | ! |
data = data, |
| 908 | ! |
ggplot2::aes(npcx = x, npcy = y, label = label), |
| 909 | ! |
hjust = 0, vjust = 1, size = 4 |
| 910 |
), |
|
| 911 | ! |
env = list(plot_call = plot_call, data = datas, label = label) |
| 912 |
) |
|
| 913 |
} |
|
| 914 | ||
| 915 | ! |
if ( |
| 916 | ! |
length(s_var) == 0 && |
| 917 | ! |
length(g_var) == 0 && |
| 918 | ! |
main_type_var == "Density" && |
| 919 | ! |
length(t_dist) != 0 && |
| 920 | ! |
main_type_var == "Density" |
| 921 |
) {
|
|
| 922 | ! |
map_dist <- stats::setNames( |
| 923 | ! |
c("dnorm", "dlnorm", "dgamma", "dunif"),
|
| 924 | ! |
c("normal", "lognormal", "gamma", "unif")
|
| 925 |
) |
|
| 926 | ! |
plot_call <- substitute( |
| 927 | ! |
expr = plot_call + stat_function( |
| 928 | ! |
data = data.frame(x = range(ANL[[dist_var]]), color = mapped_dist), |
| 929 | ! |
ggplot2::aes(x, color = color), |
| 930 | ! |
fun = mapped_dist_name, |
| 931 | ! |
n = ndensity, |
| 932 | ! |
size = 2, |
| 933 | ! |
args = params |
| 934 |
) + |
|
| 935 | ! |
ggplot2::scale_color_manual(values = stats::setNames("blue", mapped_dist), aesthetics = "color"),
|
| 936 | ! |
env = list( |
| 937 | ! |
plot_call = plot_call, |
| 938 | ! |
dist_var = dist_var, |
| 939 | ! |
ndensity = ndensity, |
| 940 | ! |
mapped_dist = unname(map_dist[t_dist]), |
| 941 | ! |
mapped_dist_name = as.name(unname(map_dist[t_dist])) |
| 942 |
) |
|
| 943 |
) |
|
| 944 |
} |
|
| 945 | ||
| 946 | ! |
all_ggplot2_args <- teal.widgets::resolve_ggplot2_args( |
| 947 | ! |
user_plot = ggplot2_args[["Histogram"]], |
| 948 | ! |
user_default = ggplot2_args$default |
| 949 |
) |
|
| 950 | ||
| 951 | ! |
parsed_ggplot2_args <- teal.widgets::parse_ggplot2_args( |
| 952 | ! |
all_ggplot2_args, |
| 953 | ! |
ggtheme = ggtheme |
| 954 |
) |
|
| 955 | ||
| 956 | ! |
teal.code::eval_code( |
| 957 | ! |
qenv, |
| 958 | ! |
substitute( |
| 959 | ! |
expr = histogram_plot <- plot_call, |
| 960 | ! |
env = list(plot_call = Reduce(function(x, y) call("+", x, y), c(plot_call, parsed_ggplot2_args)))
|
| 961 |
) |
|
| 962 |
) |
|
| 963 |
} |
|
| 964 |
) |
|
| 965 | ||
| 966 |
# qqplot qenv ---- |
|
| 967 | ! |
qq_q <- eventReactive( |
| 968 | ! |
eventExpr = {
|
| 969 | ! |
common_q() |
| 970 | ! |
input$scales_type |
| 971 | ! |
input$qq_line |
| 972 | ! |
is.null(input$ggtheme) |
| 973 | ! |
input$tabs |
| 974 |
}, |
|
| 975 | ! |
valueExpr = {
|
| 976 | ! |
dist_var <- merge_vars()$dist_var |
| 977 | ! |
s_var <- merge_vars()$s_var |
| 978 | ! |
g_var <- merge_vars()$g_var |
| 979 | ! |
dist_var_name <- merge_vars()$dist_var_name |
| 980 | ! |
s_var_name <- merge_vars()$s_var_name |
| 981 | ! |
g_var_name <- merge_vars()$g_var_name |
| 982 | ! |
dist_param1 <- input$dist_param1 |
| 983 | ! |
dist_param2 <- input$dist_param2 |
| 984 | ||
| 985 | ! |
scales_type <- input$scales_type |
| 986 | ! |
ggtheme <- input$ggtheme |
| 987 | ||
| 988 | ! |
teal::validate_inputs(iv_r_dist(), iv_dist) |
| 989 | ! |
t_dist <- req(input$t_dist) # Not validated when tab is not selected |
| 990 | ! |
qenv <- common_q() |
| 991 | ||
| 992 | ! |
plot_call <- if (length(s_var) == 0 && length(g_var) == 0) {
|
| 993 | ! |
substitute( |
| 994 | ! |
expr = ggplot2::ggplot(ANL, ggplot2::aes_string(sample = dist_var)), |
| 995 | ! |
env = list(dist_var = dist_var) |
| 996 |
) |
|
| 997 | ! |
} else if (length(s_var) != 0 && length(g_var) == 0) {
|
| 998 | ! |
substitute( |
| 999 | ! |
expr = ggplot2::ggplot(ANL, ggplot2::aes_string(sample = dist_var, color = s_var)), |
| 1000 | ! |
env = list(dist_var = dist_var, s_var = s_var) |
| 1001 |
) |
|
| 1002 | ! |
} else if (length(s_var) == 0 && length(g_var) != 0) {
|
| 1003 | ! |
substitute( |
| 1004 | ! |
expr = ggplot2::ggplot(ANL[ANL[[g_var]] != "NA", ], ggplot2::aes_string(sample = dist_var)) + |
| 1005 | ! |
ggplot2::facet_wrap(~g_var_name, ncol = 1, scales = scales_raw), |
| 1006 | ! |
env = list( |
| 1007 | ! |
dist_var = dist_var, |
| 1008 | ! |
g_var = g_var, |
| 1009 | ! |
g_var_name = g_var_name, |
| 1010 | ! |
scales_raw = tolower(scales_type) |
| 1011 |
) |
|
| 1012 |
) |
|
| 1013 |
} else {
|
|
| 1014 | ! |
substitute( |
| 1015 | ! |
expr = ggplot2::ggplot(ANL[ANL[[g_var]] != "NA", ], ggplot2::aes_string(sample = dist_var, color = s_var)) + |
| 1016 | ! |
ggplot2::facet_wrap(~g_var_name, ncol = 1, scales = scales_raw), |
| 1017 | ! |
env = list( |
| 1018 | ! |
dist_var = dist_var, |
| 1019 | ! |
g_var = g_var, |
| 1020 | ! |
s_var = s_var, |
| 1021 | ! |
g_var_name = g_var_name, |
| 1022 | ! |
scales_raw = tolower(scales_type) |
| 1023 |
) |
|
| 1024 |
) |
|
| 1025 |
} |
|
| 1026 | ||
| 1027 | ! |
map_dist <- stats::setNames( |
| 1028 | ! |
c("qnorm", "qlnorm", "qgamma", "qunif"),
|
| 1029 | ! |
c("normal", "lognormal", "gamma", "unif")
|
| 1030 |
) |
|
| 1031 | ||
| 1032 | ! |
plot_call <- substitute( |
| 1033 | ! |
expr = plot_call + |
| 1034 | ! |
ggplot2::stat_qq(distribution = mapped_dist, dparams = params), |
| 1035 | ! |
env = list(plot_call = plot_call, mapped_dist = as.name(unname(map_dist[t_dist]))) |
| 1036 |
) |
|
| 1037 | ||
| 1038 | ! |
if (length(t_dist) != 0 && length(g_var) == 0 && length(s_var) == 0) {
|
| 1039 | ! |
qenv <- teal.code::eval_code(qenv, 'library("ggpp")') # nolint quotes
|
| 1040 | ! |
qenv <- teal.code::eval_code( |
| 1041 | ! |
qenv, |
| 1042 | ! |
substitute( |
| 1043 | ! |
df_params <- as.data.frame(append(params, list(name = t_dist))), |
| 1044 | ! |
env = list(t_dist = t_dist) |
| 1045 |
) |
|
| 1046 |
) |
|
| 1047 | ! |
datas <- quote(data.frame(x = 0.7, y = 1, tb = I(list(df_params = df_params)))) |
| 1048 | ! |
label <- quote(tb) |
| 1049 | ||
| 1050 | ! |
plot_call <- substitute( |
| 1051 | ! |
expr = plot_call + |
| 1052 | ! |
ggpp::geom_table_npc( |
| 1053 | ! |
data = data, |
| 1054 | ! |
ggplot2::aes(npcx = x, npcy = y, label = label), |
| 1055 | ! |
hjust = 0, |
| 1056 | ! |
vjust = 1, |
| 1057 | ! |
size = 4 |
| 1058 |
), |
|
| 1059 | ! |
env = list( |
| 1060 | ! |
plot_call = plot_call, |
| 1061 | ! |
data = datas, |
| 1062 | ! |
label = label |
| 1063 |
) |
|
| 1064 |
) |
|
| 1065 |
} |
|
| 1066 | ||
| 1067 | ! |
if (isTRUE(input$qq_line)) {
|
| 1068 | ! |
plot_call <- substitute( |
| 1069 | ! |
expr = plot_call + |
| 1070 | ! |
ggplot2::stat_qq_line(distribution = mapped_dist, dparams = params), |
| 1071 | ! |
env = list(plot_call = plot_call, mapped_dist = as.name(unname(map_dist[t_dist]))) |
| 1072 |
) |
|
| 1073 |
} |
|
| 1074 | ||
| 1075 | ! |
all_ggplot2_args <- teal.widgets::resolve_ggplot2_args( |
| 1076 | ! |
user_plot = ggplot2_args[["QQplot"]], |
| 1077 | ! |
user_default = ggplot2_args$default, |
| 1078 | ! |
module_plot = teal.widgets::ggplot2_args(labs = list(x = "theoretical", y = "sample")) |
| 1079 |
) |
|
| 1080 | ||
| 1081 | ! |
parsed_ggplot2_args <- teal.widgets::parse_ggplot2_args( |
| 1082 | ! |
all_ggplot2_args, |
| 1083 | ! |
ggtheme = ggtheme |
| 1084 |
) |
|
| 1085 | ||
| 1086 | ! |
teal.code::eval_code( |
| 1087 | ! |
qenv, |
| 1088 | ! |
substitute( |
| 1089 | ! |
expr = qq_plot <- plot_call, |
| 1090 | ! |
env = list(plot_call = Reduce(function(x, y) call("+", x, y), c(plot_call, parsed_ggplot2_args)))
|
| 1091 |
) |
|
| 1092 |
) |
|
| 1093 |
} |
|
| 1094 |
) |
|
| 1095 | ||
| 1096 |
# test qenv ---- |
|
| 1097 | ! |
test_q <- eventReactive( |
| 1098 | ! |
ignoreNULL = FALSE, |
| 1099 | ! |
eventExpr = {
|
| 1100 | ! |
common_q() |
| 1101 | ! |
input$dist_param1 |
| 1102 | ! |
input$dist_param2 |
| 1103 | ! |
input$dist_tests |
| 1104 |
}, |
|
| 1105 | ! |
valueExpr = {
|
| 1106 |
# Create a private stack for this function only. |
|
| 1107 | ! |
ANL <- common_q()[["ANL"]] |
| 1108 | ||
| 1109 | ! |
dist_var <- merge_vars()$dist_var |
| 1110 | ! |
s_var <- merge_vars()$s_var |
| 1111 | ! |
g_var <- merge_vars()$g_var |
| 1112 | ||
| 1113 | ! |
dist_var_name <- merge_vars()$dist_var_name |
| 1114 | ! |
s_var_name <- merge_vars()$s_var_name |
| 1115 | ! |
g_var_name <- merge_vars()$g_var_name |
| 1116 | ||
| 1117 | ! |
dist_param1 <- input$dist_param1 |
| 1118 | ! |
dist_param2 <- input$dist_param2 |
| 1119 | ! |
dist_tests <- input$dist_tests |
| 1120 | ! |
t_dist <- input$t_dist |
| 1121 | ||
| 1122 | ! |
req(dist_tests) |
| 1123 | ||
| 1124 | ! |
teal::validate_inputs(iv_dist) |
| 1125 | ||
| 1126 | ! |
if (length(s_var) > 0 || length(g_var) > 0) {
|
| 1127 | ! |
counts <- ANL %>% |
| 1128 | ! |
dplyr::group_by_at(dplyr::vars(dplyr::any_of(c(s_var, g_var)))) %>% |
| 1129 | ! |
dplyr::summarise(n = dplyr::n()) |
| 1130 | ||
| 1131 | ! |
validate(need(all(counts$n > 5), "Please select strata*group with at least 5 observation each.")) |
| 1132 |
} |
|
| 1133 | ||
| 1134 | ||
| 1135 | ! |
if (dist_tests %in% c( |
| 1136 | ! |
"t-test (two-samples, not paired)", |
| 1137 | ! |
"F-test", |
| 1138 | ! |
"Kolmogorov-Smirnov (two-samples)" |
| 1139 |
)) {
|
|
| 1140 | ! |
if (length(g_var) == 0 && length(s_var) > 0) {
|
| 1141 | ! |
validate(need( |
| 1142 | ! |
length(unique(ANL[[s_var]])) == 2, |
| 1143 | ! |
"Please select stratify variable with 2 levels." |
| 1144 |
)) |
|
| 1145 |
} |
|
| 1146 | ! |
if (length(g_var) > 0 && length(s_var) > 0) {
|
| 1147 | ! |
validate(need( |
| 1148 | ! |
all(stats::na.omit(as.vector( |
| 1149 | ! |
tapply(ANL[[s_var]], list(ANL[[g_var]]), function(x) length(unique(x))) == 2 |
| 1150 |
))), |
|
| 1151 | ! |
"Please select stratify variable with 2 levels, per each group." |
| 1152 |
)) |
|
| 1153 |
} |
|
| 1154 |
} |
|
| 1155 | ||
| 1156 | ! |
map_dist <- stats::setNames( |
| 1157 | ! |
c("pnorm", "plnorm", "pgamma", "punif"),
|
| 1158 | ! |
c("normal", "lognormal", "gamma", "unif")
|
| 1159 |
) |
|
| 1160 | ! |
sks_args <- list( |
| 1161 | ! |
test = quote(stats::ks.test), |
| 1162 | ! |
args = bquote(append(list(.[[.(dist_var)]], .(map_dist[t_dist])), params)), |
| 1163 | ! |
groups = c(g_var, s_var) |
| 1164 |
) |
|
| 1165 | ! |
ssw_args <- list( |
| 1166 | ! |
test = quote(stats::shapiro.test), |
| 1167 | ! |
args = bquote(list(.[[.(dist_var)]])), |
| 1168 | ! |
groups = c(g_var, s_var) |
| 1169 |
) |
|
| 1170 | ! |
mfil_args <- list( |
| 1171 | ! |
test = quote(stats::fligner.test), |
| 1172 | ! |
args = bquote(list(.[[.(dist_var)]], .[[.(s_var)]])), |
| 1173 | ! |
groups = c(g_var) |
| 1174 |
) |
|
| 1175 | ! |
sad_args <- list( |
| 1176 | ! |
test = quote(goftest::ad.test), |
| 1177 | ! |
args = bquote(append(list(.[[.(dist_var)]], .(map_dist[t_dist])), params)), |
| 1178 | ! |
groups = c(g_var, s_var) |
| 1179 |
) |
|
| 1180 | ! |
scvm_args <- list( |
| 1181 | ! |
test = quote(goftest::cvm.test), |
| 1182 | ! |
args = bquote(append(list(.[[.(dist_var)]], .(map_dist[t_dist])), params)), |
| 1183 | ! |
groups = c(g_var, s_var) |
| 1184 |
) |
|
| 1185 | ! |
manov_args <- list( |
| 1186 | ! |
test = quote(stats::aov), |
| 1187 | ! |
args = bquote(list(stats::formula(.(dist_var_name) ~ .(s_var_name)), .)), |
| 1188 | ! |
groups = c(g_var) |
| 1189 |
) |
|
| 1190 | ! |
mt_args <- list( |
| 1191 | ! |
test = quote(stats::t.test), |
| 1192 | ! |
args = bquote(unname(split(.[[.(dist_var)]], .[[.(s_var)]], drop = TRUE))), |
| 1193 | ! |
groups = c(g_var) |
| 1194 |
) |
|
| 1195 | ! |
mv_args <- list( |
| 1196 | ! |
test = quote(stats::var.test), |
| 1197 | ! |
args = bquote(unname(split(.[[.(dist_var)]], .[[.(s_var)]], drop = TRUE))), |
| 1198 | ! |
groups = c(g_var) |
| 1199 |
) |
|
| 1200 | ! |
mks_args <- list( |
| 1201 | ! |
test = quote(stats::ks.test), |
| 1202 | ! |
args = bquote(unname(split(.[[.(dist_var)]], .[[.(s_var)]], drop = TRUE))), |
| 1203 | ! |
groups = c(g_var) |
| 1204 |
) |
|
| 1205 | ||
| 1206 | ! |
tests_base <- switch(dist_tests, |
| 1207 | ! |
"Kolmogorov-Smirnov (one-sample)" = sks_args, |
| 1208 | ! |
"Shapiro-Wilk" = ssw_args, |
| 1209 | ! |
"Fligner-Killeen" = mfil_args, |
| 1210 | ! |
"one-way ANOVA" = manov_args, |
| 1211 | ! |
"t-test (two-samples, not paired)" = mt_args, |
| 1212 | ! |
"F-test" = mv_args, |
| 1213 | ! |
"Kolmogorov-Smirnov (two-samples)" = mks_args, |
| 1214 | ! |
"Anderson-Darling (one-sample)" = sad_args, |
| 1215 | ! |
"Cramer-von Mises (one-sample)" = scvm_args |
| 1216 |
) |
|
| 1217 | ||
| 1218 | ! |
env <- list( |
| 1219 | ! |
t_test = t_dist, |
| 1220 | ! |
dist_var = dist_var, |
| 1221 | ! |
g_var = g_var, |
| 1222 | ! |
s_var = s_var, |
| 1223 | ! |
args = tests_base$args, |
| 1224 | ! |
groups = tests_base$groups, |
| 1225 | ! |
test = tests_base$test, |
| 1226 | ! |
dist_var_name = dist_var_name, |
| 1227 | ! |
g_var_name = g_var_name, |
| 1228 | ! |
s_var_name = s_var_name |
| 1229 |
) |
|
| 1230 | ||
| 1231 | ! |
qenv <- common_q() |
| 1232 | ||
| 1233 | ! |
if (length(s_var) == 0 && length(g_var) == 0) {
|
| 1234 | ! |
qenv <- teal.code::eval_code(qenv, 'library("generics")') # nolint quotes
|
| 1235 | ! |
qenv <- teal.code::eval_code( |
| 1236 | ! |
qenv, |
| 1237 | ! |
substitute( |
| 1238 | ! |
expr = {
|
| 1239 | ! |
test_table_data <- ANL %>% |
| 1240 | ! |
dplyr::select(dist_var) %>% |
| 1241 | ! |
with(., generics::glance(do.call(test, args))) %>% |
| 1242 | ! |
dplyr::mutate_if(is.numeric, round, 3) |
| 1243 |
}, |
|
| 1244 | ! |
env = env |
| 1245 |
) |
|
| 1246 |
) |
|
| 1247 |
} else {
|
|
| 1248 | ! |
qenv <- teal.code::eval_code(qenv, 'library("tidyr")') # nolint quotes
|
| 1249 | ! |
qenv <- teal.code::eval_code( |
| 1250 | ! |
qenv, |
| 1251 | ! |
substitute( |
| 1252 | ! |
expr = {
|
| 1253 | ! |
test_table_data <- ANL %>% |
| 1254 | ! |
dplyr::select(dist_var, s_var, g_var) %>% |
| 1255 | ! |
dplyr::group_by_at(dplyr::vars(dplyr::any_of(groups))) %>% |
| 1256 | ! |
dplyr::do(tests = generics::glance(do.call(test, args))) %>% |
| 1257 | ! |
tidyr::unnest(tests) %>% |
| 1258 | ! |
dplyr::mutate_if(is.numeric, round, 3) |
| 1259 |
}, |
|
| 1260 | ! |
env = env |
| 1261 |
) |
|
| 1262 |
) |
|
| 1263 |
} |
|
| 1264 |
} |
|
| 1265 |
) |
|
| 1266 | ||
| 1267 |
# outputs ---- |
|
| 1268 | ! |
output_dist_q <- reactive(c(common_q(), req(dist_q()))) |
| 1269 | ! |
output_qq_q <- reactive(c(common_q(), req(qq_q()))) |
| 1270 | ||
| 1271 |
# Summary table listing has to be created separately to allow for qenv join |
|
| 1272 | ! |
output_summary_q <- reactive({
|
| 1273 | ! |
if (iv_r()$is_valid()) {
|
| 1274 | ! |
within(common_q(), {
|
| 1275 | ! |
summary_table <- rtables::df_to_tt(summary_table_data) |
| 1276 | ! |
summary_table |
| 1277 |
}) |
|
| 1278 |
} else {
|
|
| 1279 | ! |
within( |
| 1280 | ! |
common_q(), |
| 1281 | ! |
summary_table <- rtables::rtable(header = rtables::rheader(colnames(summary_table_data))) |
| 1282 |
) |
|
| 1283 |
} |
|
| 1284 |
}) |
|
| 1285 | ||
| 1286 | ! |
output_test_q <- reactive({
|
| 1287 |
# wrapped in if since could lead into validate error - we do want to continue |
|
| 1288 | ! |
test_q_out <- try(test_q(), silent = TRUE) |
| 1289 | ! |
if (inherits(test_q_out, c("try-error", "error"))) {
|
| 1290 | ! |
within( |
| 1291 | ! |
common_q(), |
| 1292 | ! |
test_table <- rtables::rtable(header = rtables::rheader("No data available in table"), rtables::rrow())
|
| 1293 |
) |
|
| 1294 |
} else {
|
|
| 1295 | ! |
within(c(common_q(), test_q_out), {
|
| 1296 | ! |
test_table <- rtables::df_to_tt(test_table_data) |
| 1297 | ! |
test_table |
| 1298 |
}) |
|
| 1299 |
} |
|
| 1300 |
}) |
|
| 1301 | ||
| 1302 | ! |
decorated_output_dist_q <- srv_decorate_teal_data( |
| 1303 | ! |
"d_density", |
| 1304 | ! |
data = output_dist_q, |
| 1305 | ! |
decorators = select_decorators(decorators, "histogram_plot"), |
| 1306 | ! |
expr = print(histogram_plot) |
| 1307 |
) |
|
| 1308 | ||
| 1309 | ! |
decorated_output_qq_q <- srv_decorate_teal_data( |
| 1310 | ! |
"d_qq", |
| 1311 | ! |
data = output_qq_q, |
| 1312 | ! |
decorators = select_decorators(decorators, "qq_plot"), |
| 1313 | ! |
expr = print(qq_plot) |
| 1314 |
) |
|
| 1315 | ||
| 1316 | ! |
decorated_output_q <- reactive({
|
| 1317 | ! |
tab <- req(input$tabs) # tab is NULL upon app launch, hence will crash without this statement |
| 1318 | ! |
test_q_out <- try(test_q(), silent = TRUE) |
| 1319 | ! |
test_q_out <- output_test_q() |
| 1320 | ||
| 1321 | ! |
out_q <- switch(tab, |
| 1322 | ! |
Histogram = decorated_output_dist_q(), |
| 1323 | ! |
QQplot = decorated_output_qq_q() |
| 1324 |
) |
|
| 1325 | ! |
c(out_q, output_summary_q(), test_q_out) |
| 1326 |
}) |
|
| 1327 | ||
| 1328 | ! |
dist_r <- reactive(req(decorated_output_dist_q())[["histogram_plot"]]) |
| 1329 | ||
| 1330 | ! |
qq_r <- reactive(req(decorated_output_qq_q())[["qq_plot"]]) |
| 1331 | ||
| 1332 | ! |
summary_r <- reactive({
|
| 1333 | ! |
q <- req(output_summary_q()) |
| 1334 | ||
| 1335 | ! |
list( |
| 1336 | ! |
html = DT::datatable( |
| 1337 | ! |
q[["summary_table_data"]], |
| 1338 | ! |
options = list( |
| 1339 | ! |
autoWidth = TRUE, |
| 1340 | ! |
columnDefs = list(list(width = "200px", targets = "_all")) |
| 1341 |
), |
|
| 1342 | ! |
rownames = FALSE |
| 1343 |
), |
|
| 1344 | ! |
report = q[["summary_table"]] |
| 1345 |
) |
|
| 1346 |
}) |
|
| 1347 | ||
| 1348 | ! |
output$summary_table <- DT::renderDataTable(summary_r()[["html"]]) |
| 1349 | ||
| 1350 | ! |
tests_r <- reactive({
|
| 1351 | ! |
q <- req(output_test_q()) |
| 1352 | ||
| 1353 | ! |
list( |
| 1354 | ! |
html = DT::datatable(q[["test_table_data"]]), |
| 1355 | ! |
report = q[["test_table"]] |
| 1356 |
) |
|
| 1357 |
}) |
|
| 1358 | ||
| 1359 | ! |
pws1 <- teal.widgets::plot_with_settings_srv( |
| 1360 | ! |
id = "hist_plot", |
| 1361 | ! |
plot_r = dist_r, |
| 1362 | ! |
height = plot_height, |
| 1363 | ! |
width = plot_width, |
| 1364 | ! |
brushing = FALSE |
| 1365 |
) |
|
| 1366 | ||
| 1367 | ! |
pws2 <- teal.widgets::plot_with_settings_srv( |
| 1368 | ! |
id = "qq_plot", |
| 1369 | ! |
plot_r = qq_r, |
| 1370 | ! |
height = plot_height, |
| 1371 | ! |
width = plot_width, |
| 1372 | ! |
brushing = FALSE |
| 1373 |
) |
|
| 1374 | ||
| 1375 | ! |
output$t_stats <- DT::renderDataTable(tests_r()[["html"]]) |
| 1376 | ||
| 1377 |
# Render R code. |
|
| 1378 | ! |
source_code_r <- reactive(teal.code::get_code(req(decorated_output_q()))) |
| 1379 | ||
| 1380 | ! |
teal.widgets::verbatim_popup_srv( |
| 1381 | ! |
id = "rcode", |
| 1382 | ! |
verbatim_content = source_code_r, |
| 1383 | ! |
title = "R Code for distribution" |
| 1384 |
) |
|
| 1385 | ||
| 1386 |
### REPORTER |
|
| 1387 | ! |
if (with_reporter) {
|
| 1388 | ! |
card_fun <- function(comment, label) {
|
| 1389 | ! |
card <- teal::report_card_template( |
| 1390 | ! |
title = "Distribution Plot", |
| 1391 | ! |
label = label, |
| 1392 | ! |
with_filter = with_filter, |
| 1393 | ! |
filter_panel_api = filter_panel_api |
| 1394 |
) |
|
| 1395 | ! |
card$append_text("Plot", "header3")
|
| 1396 | ! |
if (input$tabs == "Histogram") {
|
| 1397 | ! |
card$append_plot(dist_r(), dim = pws1$dim()) |
| 1398 | ! |
} else if (input$tabs == "QQplot") {
|
| 1399 | ! |
card$append_plot(qq_r(), dim = pws2$dim()) |
| 1400 |
} |
|
| 1401 | ! |
card$append_text("Statistics table", "header3")
|
| 1402 | ! |
card$append_table(summary_r()[["report"]]) |
| 1403 | ! |
tests_error <- tryCatch(expr = tests_r(), error = function(e) "error") |
| 1404 | ! |
if (!identical(tests_error, "error")) {
|
| 1405 | ! |
card$append_text("Tests table", "header3")
|
| 1406 | ! |
card$append_table(tests_r()[["report"]]) |
| 1407 |
} |
|
| 1408 | ||
| 1409 | ! |
if (!comment == "") {
|
| 1410 | ! |
card$append_text("Comment", "header3")
|
| 1411 | ! |
card$append_text(comment) |
| 1412 |
} |
|
| 1413 | ! |
card$append_src(source_code_r()) |
| 1414 | ! |
card |
| 1415 |
} |
|
| 1416 | ! |
teal.reporter::add_card_button_srv("add_reporter", reporter = reporter, card_fun = card_fun)
|
| 1417 |
} |
|
| 1418 |
### |
|
| 1419 |
}) |
|
| 1420 |
} |
| 1 |
#' `teal` module: Response plot |
|
| 2 |
#' |
|
| 3 |
#' Generates a response plot for a given `response` and `x` variables. |
|
| 4 |
#' This module allows users customize and add annotations to the plot depending |
|
| 5 |
#' on the module's arguments. |
|
| 6 |
#' It supports showing the counts grouped by other variable facets (by row / column), |
|
| 7 |
#' swapping the coordinates, show count annotations and displaying the response plot |
|
| 8 |
#' as frequency or density. |
|
| 9 |
#' |
|
| 10 |
#' @inheritParams teal::module |
|
| 11 |
#' @inheritParams shared_params |
|
| 12 |
#' @param response (`data_extract_spec` or `list` of multiple `data_extract_spec`) |
|
| 13 |
#' Which variable to use as the response. |
|
| 14 |
#' You can define one fixed column by setting `fixed = TRUE` inside the `select_spec`. |
|
| 15 |
#' |
|
| 16 |
#' The `data_extract_spec` must not allow multiple selection in this case. |
|
| 17 |
#' @param x (`data_extract_spec` or `list` of multiple `data_extract_spec`) |
|
| 18 |
#' Specifies which variable to use on the X-axis of the response plot. |
|
| 19 |
#' Allow the user to select multiple columns from the `data` allowed in teal. |
|
| 20 |
#' |
|
| 21 |
#' The `data_extract_spec` must not allow multiple selection in this case. |
|
| 22 |
#' @param row_facet (`data_extract_spec` or `list` of multiple `data_extract_spec`) |
|
| 23 |
#' optional specification of the data variable(s) to use for faceting rows. |
|
| 24 |
#' @param col_facet (`data_extract_spec` or `list` of multiple `data_extract_spec`) |
|
| 25 |
#' optional specification of the data variable(s) to use for faceting columns. |
|
| 26 |
#' @param coord_flip (`logical(1)`) |
|
| 27 |
#' Indicates whether to flip coordinates between `x` and `response`. |
|
| 28 |
#' The default value is `FALSE` and it will show the `x` variable on the x-axis |
|
| 29 |
#' and the `response` variable on the y-axis. |
|
| 30 |
#' @param count_labels (`logical(1)`) |
|
| 31 |
#' Indicates whether to show count labels. |
|
| 32 |
#' Defaults to `TRUE`. |
|
| 33 |
#' @param freq (`logical(1)`) |
|
| 34 |
#' Indicates whether to display frequency (`TRUE`) or density (`FALSE`). |
|
| 35 |
#' Defaults to density (`FALSE`). |
|
| 36 |
#' |
|
| 37 |
#' @inherit shared_params return |
|
| 38 |
#' |
|
| 39 |
#' @note For more examples, please see the vignette "Using response plot" via |
|
| 40 |
#' `vignette("using-response-plot", package = "teal.modules.general")`.
|
|
| 41 |
#' |
|
| 42 |
#' @section Decorating Module: |
|
| 43 |
#' |
|
| 44 |
#' This module generates the following objects, which can be modified in place using decorators: |
|
| 45 |
#' - `plot` (`ggplot`) |
|
| 46 |
#' |
|
| 47 |
#' A Decorator is applied to the specific output using a named list of `teal_transform_module` objects. |
|
| 48 |
#' The name of this list corresponds to the name of the output to which the decorator is applied. |
|
| 49 |
#' See code snippet below: |
|
| 50 |
#' |
|
| 51 |
#' ``` |
|
| 52 |
#' tm_g_response( |
|
| 53 |
#' ..., # arguments for module |
|
| 54 |
#' decorators = list( |
|
| 55 |
#' plot = teal_transform_module(...) # applied to the `plot` output |
|
| 56 |
#' ) |
|
| 57 |
#' ) |
|
| 58 |
#' ``` |
|
| 59 |
#' |
|
| 60 |
#' For additional details and examples of decorators, refer to the vignette |
|
| 61 |
#' `vignette("decorate-module-output", package = "teal.modules.general")`.
|
|
| 62 |
#' |
|
| 63 |
#' To learn more please refer to the vignette |
|
| 64 |
#' `vignette("transform-module-output", package = "teal")` or the [`teal::teal_transform_module()`] documentation.
|
|
| 65 |
#' |
|
| 66 |
#' @examplesShinylive |
|
| 67 |
#' library(teal.modules.general) |
|
| 68 |
#' interactive <- function() TRUE |
|
| 69 |
#' {{ next_example }}
|
|
| 70 |
#' @examples |
|
| 71 |
#' # general data example |
|
| 72 |
#' data <- teal_data() |
|
| 73 |
#' data <- within(data, {
|
|
| 74 |
#' require(nestcolor) |
|
| 75 |
#' mtcars <- mtcars |
|
| 76 |
#' for (v in c("cyl", "vs", "am", "gear")) {
|
|
| 77 |
#' mtcars[[v]] <- as.factor(mtcars[[v]]) |
|
| 78 |
#' } |
|
| 79 |
#' }) |
|
| 80 |
#' |
|
| 81 |
#' app <- init( |
|
| 82 |
#' data = data, |
|
| 83 |
#' modules = modules( |
|
| 84 |
#' tm_g_response( |
|
| 85 |
#' label = "Response Plots", |
|
| 86 |
#' response = data_extract_spec( |
|
| 87 |
#' dataname = "mtcars", |
|
| 88 |
#' select = select_spec( |
|
| 89 |
#' label = "Select variable:", |
|
| 90 |
#' choices = variable_choices(data[["mtcars"]], c("cyl", "gear")),
|
|
| 91 |
#' selected = "cyl", |
|
| 92 |
#' multiple = FALSE, |
|
| 93 |
#' fixed = FALSE |
|
| 94 |
#' ) |
|
| 95 |
#' ), |
|
| 96 |
#' x = data_extract_spec( |
|
| 97 |
#' dataname = "mtcars", |
|
| 98 |
#' select = select_spec( |
|
| 99 |
#' label = "Select variable:", |
|
| 100 |
#' choices = variable_choices(data[["mtcars"]], c("vs", "am")),
|
|
| 101 |
#' selected = "vs", |
|
| 102 |
#' multiple = FALSE, |
|
| 103 |
#' fixed = FALSE |
|
| 104 |
#' ) |
|
| 105 |
#' ) |
|
| 106 |
#' ) |
|
| 107 |
#' ) |
|
| 108 |
#' ) |
|
| 109 |
#' if (interactive()) {
|
|
| 110 |
#' shinyApp(app$ui, app$server) |
|
| 111 |
#' } |
|
| 112 |
#' |
|
| 113 |
#' @examplesShinylive |
|
| 114 |
#' library(teal.modules.general) |
|
| 115 |
#' interactive <- function() TRUE |
|
| 116 |
#' {{ next_example }}
|
|
| 117 |
#' @examples |
|
| 118 |
#' # CDISC data example |
|
| 119 |
#' data <- teal_data() |
|
| 120 |
#' data <- within(data, {
|
|
| 121 |
#' require(nestcolor) |
|
| 122 |
#' ADSL <- teal.data::rADSL |
|
| 123 |
#' }) |
|
| 124 |
#' join_keys(data) <- default_cdisc_join_keys[names(data)] |
|
| 125 |
#' |
|
| 126 |
#' app <- init( |
|
| 127 |
#' data = data, |
|
| 128 |
#' modules = modules( |
|
| 129 |
#' tm_g_response( |
|
| 130 |
#' label = "Response Plots", |
|
| 131 |
#' response = data_extract_spec( |
|
| 132 |
#' dataname = "ADSL", |
|
| 133 |
#' select = select_spec( |
|
| 134 |
#' label = "Select variable:", |
|
| 135 |
#' choices = variable_choices(data[["ADSL"]], c("BMRKR2", "COUNTRY")),
|
|
| 136 |
#' selected = "BMRKR2", |
|
| 137 |
#' multiple = FALSE, |
|
| 138 |
#' fixed = FALSE |
|
| 139 |
#' ) |
|
| 140 |
#' ), |
|
| 141 |
#' x = data_extract_spec( |
|
| 142 |
#' dataname = "ADSL", |
|
| 143 |
#' select = select_spec( |
|
| 144 |
#' label = "Select variable:", |
|
| 145 |
#' choices = variable_choices(data[["ADSL"]], c("SEX", "RACE")),
|
|
| 146 |
#' selected = "RACE", |
|
| 147 |
#' multiple = FALSE, |
|
| 148 |
#' fixed = FALSE |
|
| 149 |
#' ) |
|
| 150 |
#' ) |
|
| 151 |
#' ) |
|
| 152 |
#' ) |
|
| 153 |
#' ) |
|
| 154 |
#' if (interactive()) {
|
|
| 155 |
#' shinyApp(app$ui, app$server) |
|
| 156 |
#' } |
|
| 157 |
#' |
|
| 158 |
#' @export |
|
| 159 |
#' |
|
| 160 |
tm_g_response <- function(label = "Response Plot", |
|
| 161 |
response, |
|
| 162 |
x, |
|
| 163 |
row_facet = NULL, |
|
| 164 |
col_facet = NULL, |
|
| 165 |
coord_flip = FALSE, |
|
| 166 |
count_labels = TRUE, |
|
| 167 |
rotate_xaxis_labels = FALSE, |
|
| 168 |
freq = FALSE, |
|
| 169 |
plot_height = c(600, 400, 5000), |
|
| 170 |
plot_width = NULL, |
|
| 171 |
ggtheme = c("gray", "bw", "linedraw", "light", "dark", "minimal", "classic", "void"),
|
|
| 172 |
ggplot2_args = teal.widgets::ggplot2_args(), |
|
| 173 |
pre_output = NULL, |
|
| 174 |
post_output = NULL, |
|
| 175 |
transformators = list(), |
|
| 176 |
decorators = list()) {
|
|
| 177 | ! |
message("Initializing tm_g_response")
|
| 178 | ||
| 179 |
# Normalize the parameters |
|
| 180 | ! |
if (inherits(response, "data_extract_spec")) response <- list(response) |
| 181 | ! |
if (inherits(x, "data_extract_spec")) x <- list(x) |
| 182 | ! |
if (inherits(row_facet, "data_extract_spec")) row_facet <- list(row_facet) |
| 183 | ! |
if (inherits(col_facet, "data_extract_spec")) col_facet <- list(col_facet) |
| 184 | ||
| 185 |
# Start of assertions |
|
| 186 | ! |
checkmate::assert_string(label) |
| 187 | ||
| 188 | ! |
checkmate::assert_list(response, types = "data_extract_spec") |
| 189 | ! |
if (!all(vapply(response, function(x) !("" %in% x$select$choices), logical(1)))) {
|
| 190 | ! |
stop("'response' should not allow empty values")
|
| 191 |
} |
|
| 192 | ! |
assert_single_selection(response) |
| 193 | ||
| 194 | ! |
checkmate::assert_list(x, types = "data_extract_spec") |
| 195 | ! |
if (!all(vapply(x, function(x) !("" %in% x$select$choices), logical(1)))) {
|
| 196 | ! |
stop("'x' should not allow empty values")
|
| 197 |
} |
|
| 198 | ! |
assert_single_selection(x) |
| 199 | ||
| 200 | ! |
checkmate::assert_list(row_facet, types = "data_extract_spec", null.ok = TRUE) |
| 201 | ! |
checkmate::assert_list(col_facet, types = "data_extract_spec", null.ok = TRUE) |
| 202 | ! |
checkmate::assert_flag(coord_flip) |
| 203 | ! |
checkmate::assert_flag(count_labels) |
| 204 | ! |
checkmate::assert_flag(rotate_xaxis_labels) |
| 205 | ! |
checkmate::assert_flag(freq) |
| 206 | ||
| 207 | ! |
checkmate::assert_numeric(plot_height, len = 3, any.missing = FALSE, finite = TRUE) |
| 208 | ! |
checkmate::assert_numeric(plot_height[1], lower = plot_height[2], upper = plot_height[3], .var.name = "plot_height") |
| 209 | ! |
checkmate::assert_numeric(plot_width, len = 3, any.missing = FALSE, null.ok = TRUE, finite = TRUE) |
| 210 | ! |
checkmate::assert_numeric( |
| 211 | ! |
plot_width[1], |
| 212 | ! |
lower = plot_width[2], upper = plot_width[3], null.ok = TRUE, .var.name = "plot_width" |
| 213 |
) |
|
| 214 | ||
| 215 | ! |
ggtheme <- match.arg(ggtheme) |
| 216 | ! |
checkmate::assert_class(ggplot2_args, "ggplot2_args") |
| 217 | ||
| 218 | ! |
checkmate::assert_multi_class(pre_output, c("shiny.tag", "shiny.tag.list", "html"), null.ok = TRUE)
|
| 219 | ! |
checkmate::assert_multi_class(post_output, c("shiny.tag", "shiny.tag.list", "html"), null.ok = TRUE)
|
| 220 | ||
| 221 | ! |
assert_decorators(decorators, "plot") |
| 222 |
# End of assertions |
|
| 223 | ||
| 224 |
# Make UI args |
|
| 225 | ! |
args <- as.list(environment()) |
| 226 | ||
| 227 | ! |
data_extract_list <- list( |
| 228 | ! |
response = response, |
| 229 | ! |
x = x, |
| 230 | ! |
row_facet = row_facet, |
| 231 | ! |
col_facet = col_facet |
| 232 |
) |
|
| 233 | ||
| 234 | ! |
ans <- module( |
| 235 | ! |
label = label, |
| 236 | ! |
server = srv_g_response, |
| 237 | ! |
ui = ui_g_response, |
| 238 | ! |
ui_args = args, |
| 239 | ! |
server_args = c( |
| 240 | ! |
data_extract_list, |
| 241 | ! |
list( |
| 242 | ! |
plot_height = plot_height, |
| 243 | ! |
plot_width = plot_width, |
| 244 | ! |
ggplot2_args = ggplot2_args, |
| 245 | ! |
decorators = decorators |
| 246 |
) |
|
| 247 |
), |
|
| 248 | ! |
transformators = transformators, |
| 249 | ! |
datanames = teal.transform::get_extract_datanames(data_extract_list) |
| 250 |
) |
|
| 251 | ! |
attr(ans, "teal_bookmarkable") <- TRUE |
| 252 | ! |
ans |
| 253 |
} |
|
| 254 | ||
| 255 |
# UI function for the response module |
|
| 256 |
ui_g_response <- function(id, ...) {
|
|
| 257 | ! |
ns <- NS(id) |
| 258 | ! |
args <- list(...) |
| 259 | ! |
is_single_dataset_value <- teal.transform::is_single_dataset(args$response, args$x, args$row_facet, args$col_facet) |
| 260 | ||
| 261 | ! |
teal.widgets::standard_layout( |
| 262 | ! |
output = teal.widgets::white_small_well( |
| 263 | ! |
teal.widgets::plot_with_settings_ui(id = ns("myplot"))
|
| 264 |
), |
|
| 265 | ! |
encoding = tags$div( |
| 266 |
### Reporter |
|
| 267 | ! |
teal.reporter::add_card_button_ui(ns("add_reporter"), label = "Add Report Card"),
|
| 268 | ! |
tags$br(), tags$br(), |
| 269 |
### |
|
| 270 | ! |
tags$label("Encodings", class = "text-primary"),
|
| 271 | ! |
teal.transform::datanames_input(args[c("response", "x", "row_facet", "col_facet")]),
|
| 272 | ! |
teal.transform::data_extract_ui( |
| 273 | ! |
id = ns("response"),
|
| 274 | ! |
label = "Response variable", |
| 275 | ! |
data_extract_spec = args$response, |
| 276 | ! |
is_single_dataset = is_single_dataset_value |
| 277 |
), |
|
| 278 | ! |
teal.transform::data_extract_ui( |
| 279 | ! |
id = ns("x"),
|
| 280 | ! |
label = "X variable", |
| 281 | ! |
data_extract_spec = args$x, |
| 282 | ! |
is_single_dataset = is_single_dataset_value |
| 283 |
), |
|
| 284 | ! |
if (!is.null(args$row_facet)) {
|
| 285 | ! |
teal.transform::data_extract_ui( |
| 286 | ! |
id = ns("row_facet"),
|
| 287 | ! |
label = "Row facetting", |
| 288 | ! |
data_extract_spec = args$row_facet, |
| 289 | ! |
is_single_dataset = is_single_dataset_value |
| 290 |
) |
|
| 291 |
}, |
|
| 292 | ! |
if (!is.null(args$col_facet)) {
|
| 293 | ! |
teal.transform::data_extract_ui( |
| 294 | ! |
id = ns("col_facet"),
|
| 295 | ! |
label = "Column facetting", |
| 296 | ! |
data_extract_spec = args$col_facet, |
| 297 | ! |
is_single_dataset = is_single_dataset_value |
| 298 |
) |
|
| 299 |
}, |
|
| 300 | ! |
shinyWidgets::radioGroupButtons( |
| 301 | ! |
inputId = ns("freq"),
|
| 302 | ! |
label = NULL, |
| 303 | ! |
choices = c("frequency", "density"),
|
| 304 | ! |
selected = ifelse(args$freq, "frequency", "density"), |
| 305 | ! |
justified = TRUE |
| 306 |
), |
|
| 307 | ! |
ui_decorate_teal_data(ns("decorator"), decorators = select_decorators(args$decorators, "plot")),
|
| 308 | ! |
bslib::accordion( |
| 309 | ! |
open = TRUE, |
| 310 | ! |
bslib::accordion_panel( |
| 311 | ! |
title = "Plot settings", |
| 312 | ! |
checkboxInput(ns("count_labels"), "Add count labels", value = args$count_labels),
|
| 313 | ! |
checkboxInput(ns("coord_flip"), "Swap axes", value = args$coord_flip),
|
| 314 | ! |
checkboxInput(ns("rotate_xaxis_labels"), "Rotate X axis labels", value = args$rotate_xaxis_labels),
|
| 315 | ! |
selectInput( |
| 316 | ! |
inputId = ns("ggtheme"),
|
| 317 | ! |
label = "Theme (by ggplot):", |
| 318 | ! |
choices = ggplot_themes, |
| 319 | ! |
selected = args$ggtheme, |
| 320 | ! |
multiple = FALSE |
| 321 |
) |
|
| 322 |
) |
|
| 323 |
) |
|
| 324 |
), |
|
| 325 | ! |
forms = tagList( |
| 326 | ! |
teal.widgets::verbatim_popup_ui(ns("rcode"), "Show R code")
|
| 327 |
), |
|
| 328 | ! |
pre_output = args$pre_output, |
| 329 | ! |
post_output = args$post_output |
| 330 |
) |
|
| 331 |
} |
|
| 332 | ||
| 333 |
# Server function for the response module |
|
| 334 |
srv_g_response <- function(id, |
|
| 335 |
data, |
|
| 336 |
reporter, |
|
| 337 |
filter_panel_api, |
|
| 338 |
response, |
|
| 339 |
x, |
|
| 340 |
row_facet, |
|
| 341 |
col_facet, |
|
| 342 |
plot_height, |
|
| 343 |
plot_width, |
|
| 344 |
ggplot2_args, |
|
| 345 |
decorators) {
|
|
| 346 | ! |
with_reporter <- !missing(reporter) && inherits(reporter, "Reporter") |
| 347 | ! |
with_filter <- !missing(filter_panel_api) && inherits(filter_panel_api, "FilterPanelAPI") |
| 348 | ! |
checkmate::assert_class(data, "reactive") |
| 349 | ! |
checkmate::assert_class(isolate(data()), "teal_data") |
| 350 | ! |
moduleServer(id, function(input, output, session) {
|
| 351 | ! |
teal.logger::log_shiny_input_changes(input, namespace = "teal.modules.general") |
| 352 | ||
| 353 | ! |
data_extract <- list(response = response, x = x, row_facet = row_facet, col_facet = col_facet) |
| 354 | ||
| 355 | ! |
rule_diff <- function(other) {
|
| 356 | ! |
function(value) {
|
| 357 | ! |
if (other %in% names(selector_list())) {
|
| 358 | ! |
othervalue <- selector_list()[[other]]()[["select"]] |
| 359 | ! |
if (!is.null(othervalue)) {
|
| 360 | ! |
if (identical(value, othervalue)) {
|
| 361 | ! |
"Row and column facetting variables must be different." |
| 362 |
} |
|
| 363 |
} |
|
| 364 |
} |
|
| 365 |
} |
|
| 366 |
} |
|
| 367 | ||
| 368 | ! |
selector_list <- teal.transform::data_extract_multiple_srv( |
| 369 | ! |
data_extract = data_extract, |
| 370 | ! |
datasets = data, |
| 371 | ! |
select_validation_rule = list( |
| 372 | ! |
response = shinyvalidate::sv_required("Please define a column for the response variable"),
|
| 373 | ! |
x = shinyvalidate::sv_required("Please define a column for X variable"),
|
| 374 | ! |
row_facet = shinyvalidate::compose_rules( |
| 375 | ! |
shinyvalidate::sv_optional(), |
| 376 | ! |
~ if (length(.) > 1) "There must be 1 or no row facetting variable.", |
| 377 | ! |
rule_diff("col_facet")
|
| 378 |
), |
|
| 379 | ! |
col_facet = shinyvalidate::compose_rules( |
| 380 | ! |
shinyvalidate::sv_optional(), |
| 381 | ! |
~ if (length(.) > 1) "There must be 1 or no column facetting variable.", |
| 382 | ! |
rule_diff("row_facet")
|
| 383 |
) |
|
| 384 |
) |
|
| 385 |
) |
|
| 386 | ||
| 387 | ! |
iv_r <- reactive({
|
| 388 | ! |
iv <- shinyvalidate::InputValidator$new() |
| 389 | ! |
iv$add_rule("ggtheme", shinyvalidate::sv_required("Please select a theme"))
|
| 390 | ! |
teal.transform::compose_and_enable_validators(iv, selector_list) |
| 391 |
}) |
|
| 392 | ||
| 393 | ! |
anl_merged_input <- teal.transform::merge_expression_srv( |
| 394 | ! |
selector_list = selector_list, |
| 395 | ! |
datasets = data |
| 396 |
) |
|
| 397 | ||
| 398 | ! |
qenv <- reactive( |
| 399 | ! |
teal.code::eval_code(data(), 'library("ggplot2");library("dplyr")') # nolint quotes
|
| 400 |
) |
|
| 401 | ||
| 402 | ! |
anl_merged_q <- reactive({
|
| 403 | ! |
req(anl_merged_input()) |
| 404 | ! |
qenv() %>% |
| 405 | ! |
teal.code::eval_code(as.expression(anl_merged_input()$expr)) |
| 406 |
}) |
|
| 407 | ||
| 408 | ! |
merged <- list( |
| 409 | ! |
anl_input_r = anl_merged_input, |
| 410 | ! |
anl_q_r = anl_merged_q |
| 411 |
) |
|
| 412 | ||
| 413 | ! |
output_q <- reactive({
|
| 414 | ! |
teal::validate_inputs(iv_r()) |
| 415 | ||
| 416 | ! |
qenv <- merged$anl_q_r() |
| 417 | ! |
ANL <- qenv[["ANL"]] |
| 418 | ! |
resp_var <- as.vector(merged$anl_input_r()$columns_source$response) |
| 419 | ! |
x <- as.vector(merged$anl_input_r()$columns_source$x) |
| 420 | ||
| 421 | ! |
validate(need(is.factor(ANL[[resp_var]]), "Please select a factor variable as the response.")) |
| 422 | ! |
validate(need(is.factor(ANL[[x]]), "Please select a factor variable as the X-Variable.")) |
| 423 | ! |
teal::validate_has_data(ANL, 10) |
| 424 | ! |
teal::validate_has_data(ANL[, c(resp_var, x)], 10, complete = TRUE, allow_inf = FALSE) |
| 425 | ||
| 426 | ! |
row_facet_name <- if (length(merged$anl_input_r()$columns_source$row_facet) == 0) {
|
| 427 | ! |
character(0) |
| 428 |
} else {
|
|
| 429 | ! |
as.vector(merged$anl_input_r()$columns_source$row_facet) |
| 430 |
} |
|
| 431 | ! |
col_facet_name <- if (length(merged$anl_input_r()$columns_source$col_facet) == 0) {
|
| 432 | ! |
character(0) |
| 433 |
} else {
|
|
| 434 | ! |
as.vector(merged$anl_input_r()$columns_source$col_facet) |
| 435 |
} |
|
| 436 | ||
| 437 | ! |
freq <- input$freq == "frequency" |
| 438 | ! |
swap_axes <- input$coord_flip |
| 439 | ! |
counts <- input$count_labels |
| 440 | ! |
rotate_xaxis_labels <- input$rotate_xaxis_labels |
| 441 | ! |
ggtheme <- input$ggtheme |
| 442 | ||
| 443 | ! |
arg_position <- if (freq) "stack" else "fill" |
| 444 | ||
| 445 | ! |
rowf <- if (length(row_facet_name) != 0) as.name(row_facet_name) |
| 446 | ! |
colf <- if (length(col_facet_name) != 0) as.name(col_facet_name) |
| 447 | ! |
resp_cl <- as.name(resp_var) |
| 448 | ! |
x_cl <- as.name(x) |
| 449 | ||
| 450 | ! |
if (swap_axes) {
|
| 451 | ! |
qenv <- teal.code::eval_code( |
| 452 | ! |
qenv, |
| 453 | ! |
substitute( |
| 454 | ! |
expr = ANL[[x]] <- with(ANL, forcats::fct_rev(x_cl)), |
| 455 | ! |
env = list(x = x, x_cl = x_cl) |
| 456 |
) |
|
| 457 |
) |
|
| 458 |
} |
|
| 459 | ||
| 460 | ! |
qenv <- teal.code::eval_code( |
| 461 | ! |
qenv, |
| 462 | ! |
substitute( |
| 463 | ! |
expr = ANL[[resp_var]] <- factor(ANL[[resp_var]]), |
| 464 | ! |
env = list(resp_var = resp_var) |
| 465 |
) |
|
| 466 |
) %>% |
|
| 467 |
# rowf and colf will be a NULL if not set by a user |
|
| 468 | ! |
teal.code::eval_code( |
| 469 | ! |
substitute( |
| 470 | ! |
expr = ANL2 <- ANL %>% |
| 471 | ! |
dplyr::group_by_at(dplyr::vars(x_cl, resp_cl, rowf, colf)) %>% |
| 472 | ! |
dplyr::summarise(ns = dplyr::n()) %>% |
| 473 | ! |
dplyr::group_by_at(dplyr::vars(x_cl, rowf, colf)) %>% |
| 474 | ! |
dplyr::mutate(sums = sum(ns), percent = round(ns / sums * 100, 1)), |
| 475 | ! |
env = list(x_cl = x_cl, resp_cl = resp_cl, rowf = rowf, colf = colf) |
| 476 |
) |
|
| 477 |
) %>% |
|
| 478 | ! |
teal.code::eval_code( |
| 479 | ! |
substitute( |
| 480 | ! |
expr = ANL3 <- ANL %>% |
| 481 | ! |
dplyr::group_by_at(dplyr::vars(x_cl, rowf, colf)) %>% |
| 482 | ! |
dplyr::summarise(ns = dplyr::n()), |
| 483 | ! |
env = list(x_cl = x_cl, rowf = rowf, colf = colf) |
| 484 |
) |
|
| 485 |
) |
|
| 486 | ||
| 487 | ! |
plot_call <- substitute( |
| 488 | ! |
expr = ggplot2::ggplot(ANL2, ggplot2::aes(x = x_cl, y = ns)) + |
| 489 | ! |
ggplot2::geom_bar(ggplot2::aes(fill = resp_cl), stat = "identity", position = arg_position), |
| 490 | ! |
env = list( |
| 491 | ! |
x_cl = x_cl, |
| 492 | ! |
resp_cl = resp_cl, |
| 493 | ! |
arg_position = arg_position |
| 494 |
) |
|
| 495 |
) |
|
| 496 | ||
| 497 | ! |
if (!freq) {
|
| 498 | ! |
plot_call <- substitute( |
| 499 | ! |
plot_call + ggplot2::expand_limits(y = c(0, 1.1)), |
| 500 | ! |
env = list(plot_call = plot_call) |
| 501 |
) |
|
| 502 |
} |
|
| 503 | ||
| 504 | ! |
if (counts) {
|
| 505 | ! |
plot_call <- substitute( |
| 506 | ! |
expr = plot_call + |
| 507 | ! |
ggplot2::geom_text( |
| 508 | ! |
data = ANL2, |
| 509 | ! |
ggplot2::aes(label = ns, x = x_cl, y = ns, group = resp_cl), |
| 510 | ! |
col = "white", |
| 511 | ! |
vjust = "middle", |
| 512 | ! |
hjust = "middle", |
| 513 | ! |
position = position_anl2_value |
| 514 |
) + |
|
| 515 | ! |
ggplot2::geom_text( |
| 516 | ! |
data = ANL3, ggplot2::aes(label = ns, x = x_cl, y = anl3_y), |
| 517 | ! |
hjust = hjust_value, |
| 518 | ! |
vjust = vjust_value, |
| 519 | ! |
position = position_anl3_value |
| 520 |
), |
|
| 521 | ! |
env = list( |
| 522 | ! |
plot_call = plot_call, |
| 523 | ! |
x_cl = x_cl, |
| 524 | ! |
resp_cl = resp_cl, |
| 525 | ! |
hjust_value = if (swap_axes) "left" else "middle", |
| 526 | ! |
vjust_value = if (swap_axes) "middle" else -1, |
| 527 | ! |
position_anl2_value = if (!freq) quote(position_fill(0.5)) else quote(position_stack(0.5)), # nolint: line_length. |
| 528 | ! |
anl3_y = if (!freq) 1.1 else as.name("ns"),
|
| 529 | ! |
position_anl3_value = if (!freq) "fill" else "stack" |
| 530 |
) |
|
| 531 |
) |
|
| 532 |
} |
|
| 533 | ||
| 534 | ! |
if (swap_axes) {
|
| 535 | ! |
plot_call <- substitute(plot_call + coord_flip(), env = list(plot_call = plot_call)) |
| 536 |
} |
|
| 537 | ||
| 538 | ! |
facet_cl <- facet_ggplot_call(row_facet_name, col_facet_name) |
| 539 | ||
| 540 | ! |
if (!is.null(facet_cl)) {
|
| 541 | ! |
plot_call <- substitute(expr = plot_call + facet_cl, env = list(plot_call = plot_call, facet_cl = facet_cl)) |
| 542 |
} |
|
| 543 | ||
| 544 | ! |
dev_ggplot2_args <- teal.widgets::ggplot2_args( |
| 545 | ! |
labs = list( |
| 546 | ! |
x = varname_w_label(x, ANL), |
| 547 | ! |
y = varname_w_label(resp_var, ANL, prefix = "Proportion of "), |
| 548 | ! |
fill = varname_w_label(resp_var, ANL) |
| 549 |
), |
|
| 550 | ! |
theme = list(legend.position = "bottom") |
| 551 |
) |
|
| 552 | ||
| 553 | ! |
if (rotate_xaxis_labels) {
|
| 554 | ! |
dev_ggplot2_args$theme[["axis.text.x"]] <- quote(ggplot2::element_text(angle = 45, hjust = 1)) |
| 555 |
} |
|
| 556 | ||
| 557 | ! |
all_ggplot2_args <- teal.widgets::resolve_ggplot2_args( |
| 558 | ! |
user_plot = ggplot2_args, |
| 559 | ! |
module_plot = dev_ggplot2_args |
| 560 |
) |
|
| 561 | ||
| 562 | ! |
parsed_ggplot2_args <- teal.widgets::parse_ggplot2_args( |
| 563 | ! |
all_ggplot2_args, |
| 564 | ! |
ggtheme = ggtheme |
| 565 |
) |
|
| 566 | ||
| 567 | ! |
plot_call <- substitute(expr = {
|
| 568 | ! |
plot <- plot_call + labs + ggthemes + themes |
| 569 | ! |
}, env = list( |
| 570 | ! |
plot_call = plot_call, |
| 571 | ! |
labs = parsed_ggplot2_args$labs, |
| 572 | ! |
themes = parsed_ggplot2_args$theme, |
| 573 | ! |
ggthemes = parsed_ggplot2_args$ggtheme |
| 574 |
)) |
|
| 575 | ||
| 576 | ! |
teal.code::eval_code(qenv, plot_call) |
| 577 |
}) |
|
| 578 | ||
| 579 | ! |
decorated_output_plot_q <- srv_decorate_teal_data( |
| 580 | ! |
id = "decorator", |
| 581 | ! |
data = output_q, |
| 582 | ! |
decorators = select_decorators(decorators, "plot"), |
| 583 | ! |
expr = plot |
| 584 |
) |
|
| 585 | ||
| 586 | ! |
plot_r <- reactive(req(decorated_output_plot_q())[["plot"]]) |
| 587 | ||
| 588 |
# Insert the plot into a plot_with_settings module from teal.widgets |
|
| 589 | ! |
pws <- teal.widgets::plot_with_settings_srv( |
| 590 | ! |
id = "myplot", |
| 591 | ! |
plot_r = plot_r, |
| 592 | ! |
height = plot_height, |
| 593 | ! |
width = plot_width |
| 594 |
) |
|
| 595 | ||
| 596 |
# Render R code. |
|
| 597 | ! |
source_code_r <- reactive(teal.code::get_code(req(decorated_output_plot_q()))) |
| 598 | ||
| 599 | ! |
teal.widgets::verbatim_popup_srv( |
| 600 | ! |
id = "rcode", |
| 601 | ! |
verbatim_content = source_code_r, |
| 602 | ! |
title = "Show R Code for Response" |
| 603 |
) |
|
| 604 | ||
| 605 |
### REPORTER |
|
| 606 | ! |
if (with_reporter) {
|
| 607 | ! |
card_fun <- function(comment, label) {
|
| 608 | ! |
card <- teal::report_card_template( |
| 609 | ! |
title = "Response Plot", |
| 610 | ! |
label = label, |
| 611 | ! |
with_filter = with_filter, |
| 612 | ! |
filter_panel_api = filter_panel_api |
| 613 |
) |
|
| 614 | ! |
card$append_text("Plot", "header3")
|
| 615 | ! |
card$append_plot(plot_r(), dim = pws$dim()) |
| 616 | ! |
if (!comment == "") {
|
| 617 | ! |
card$append_text("Comment", "header3")
|
| 618 | ! |
card$append_text(comment) |
| 619 |
} |
|
| 620 | ! |
card$append_src(source_code_r()) |
| 621 | ! |
card |
| 622 |
} |
|
| 623 | ! |
teal.reporter::add_card_button_srv("add_reporter", reporter = reporter, card_fun = card_fun)
|
| 624 |
} |
|
| 625 |
### |
|
| 626 |
}) |
|
| 627 |
} |
| 1 |
#' `teal` module: File viewer |
|
| 2 |
#' |
|
| 3 |
#' The file viewer module provides a tool to view static files. |
|
| 4 |
#' Supported formats include text formats, `PDF`, `PNG` `APNG`, |
|
| 5 |
#' `JPEG` `SVG`, `WEBP`, `GIF` and `BMP`. |
|
| 6 |
#' |
|
| 7 |
#' @inheritParams teal::module |
|
| 8 |
#' @inheritParams shared_params |
|
| 9 |
#' @param input_path (`list`) of the input paths, optional. Each element can be: |
|
| 10 |
#' |
|
| 11 |
#' Paths can be specified as absolute paths or relative to the running directory of the application. |
|
| 12 |
#' Default to the current working directory if not supplied. |
|
| 13 |
#' |
|
| 14 |
#' @inherit shared_params return |
|
| 15 |
#' |
|
| 16 |
#' @examplesShinylive |
|
| 17 |
#' library(teal.modules.general) |
|
| 18 |
#' interactive <- function() TRUE |
|
| 19 |
#' {{ next_example }}
|
|
| 20 |
#' @examples |
|
| 21 |
#' data <- teal_data() |
|
| 22 |
#' data <- within(data, {
|
|
| 23 |
#' data <- data.frame(1) |
|
| 24 |
#' }) |
|
| 25 |
#' |
|
| 26 |
#' app <- init( |
|
| 27 |
#' data = data, |
|
| 28 |
#' modules = modules( |
|
| 29 |
#' tm_file_viewer( |
|
| 30 |
#' input_path = list( |
|
| 31 |
#' folder = system.file("sample_files", package = "teal.modules.general"),
|
|
| 32 |
#' png = system.file("sample_files/sample_file.png", package = "teal.modules.general"),
|
|
| 33 |
#' txt = system.file("sample_files/sample_file.txt", package = "teal.modules.general"),
|
|
| 34 |
#' url = "https://fda.gov/files/drugs/published/Portable-Document-Format-Specifications.pdf" |
|
| 35 |
#' ) |
|
| 36 |
#' ) |
|
| 37 |
#' ) |
|
| 38 |
#' ) |
|
| 39 |
#' if (interactive()) {
|
|
| 40 |
#' shinyApp(app$ui, app$server) |
|
| 41 |
#' } |
|
| 42 |
#' |
|
| 43 |
#' @export |
|
| 44 |
#' |
|
| 45 |
tm_file_viewer <- function(label = "File Viewer Module", |
|
| 46 |
input_path = list("Current Working Directory" = ".")) {
|
|
| 47 | ! |
message("Initializing tm_file_viewer")
|
| 48 | ||
| 49 |
# Normalize the parameters |
|
| 50 | ! |
if (length(label) == 0 || identical(label, "")) label <- " " |
| 51 | ! |
if (length(input_path) == 0 || identical(input_path, "")) input_path <- list() |
| 52 | ||
| 53 |
# Start of assertions |
|
| 54 | ! |
checkmate::assert_string(label) |
| 55 | ||
| 56 | ! |
checkmate::assert( |
| 57 | ! |
checkmate::check_list(input_path, types = "character", min.len = 0), |
| 58 | ! |
checkmate::check_character(input_path, min.len = 1) |
| 59 |
) |
|
| 60 | ! |
if (length(input_path) > 0) {
|
| 61 | ! |
valid_url <- function(url_input, timeout = 2) {
|
| 62 | ! |
con <- try(url(url_input), silent = TRUE) |
| 63 | ! |
check <- suppressWarnings(try(open.connection(con, open = "rt", timeout = timeout), silent = TRUE)[1]) |
| 64 | ! |
try(close.connection(con), silent = TRUE) |
| 65 | ! |
is.null(check) |
| 66 |
} |
|
| 67 | ! |
idx <- vapply(input_path, function(x) file.exists(x) || valid_url(x), logical(1)) |
| 68 | ||
| 69 | ! |
if (!all(idx)) {
|
| 70 | ! |
warning( |
| 71 | ! |
paste0( |
| 72 | ! |
"Non-existent file or url path. Please provide valid paths for:\n", |
| 73 | ! |
paste0(input_path[!idx], collapse = "\n") |
| 74 |
) |
|
| 75 |
) |
|
| 76 |
} |
|
| 77 | ! |
input_path <- input_path[idx] |
| 78 |
} else {
|
|
| 79 | ! |
warning( |
| 80 | ! |
"No file or url paths were provided." |
| 81 |
) |
|
| 82 |
} |
|
| 83 |
# End of assertions |
|
| 84 | ||
| 85 |
# Make UI args |
|
| 86 | ! |
args <- as.list(environment()) |
| 87 | ||
| 88 | ! |
ans <- module( |
| 89 | ! |
label = label, |
| 90 | ! |
server = srv_viewer, |
| 91 | ! |
server_args = list(input_path = input_path), |
| 92 | ! |
ui = ui_viewer, |
| 93 | ! |
ui_args = args, |
| 94 | ! |
datanames = NULL |
| 95 |
) |
|
| 96 | ! |
attr(ans, "teal_bookmarkable") <- FALSE |
| 97 | ! |
ans |
| 98 |
} |
|
| 99 | ||
| 100 |
# UI function for the file viewer module |
|
| 101 |
ui_viewer <- function(id, ...) {
|
|
| 102 | ! |
args <- list(...) |
| 103 | ! |
ns <- NS(id) |
| 104 | ||
| 105 | ! |
tagList( |
| 106 | ! |
teal.widgets::standard_layout( |
| 107 | ! |
output = tags$div( |
| 108 | ! |
uiOutput(ns("output"))
|
| 109 |
), |
|
| 110 | ! |
encoding = tags$div( |
| 111 | ! |
style = "overflow-y: hidden; overflow-x: auto;", |
| 112 | ! |
tags$label("Encodings", class = "text-primary"),
|
| 113 | ! |
shinyTree::shinyTree( |
| 114 | ! |
ns("tree"),
|
| 115 | ! |
dragAndDrop = FALSE, |
| 116 | ! |
sort = FALSE, |
| 117 | ! |
theme = "proton", |
| 118 | ! |
multiple = FALSE, |
| 119 | ! |
search = TRUE |
| 120 |
) |
|
| 121 |
) |
|
| 122 |
) |
|
| 123 |
) |
|
| 124 |
} |
|
| 125 | ||
| 126 |
# Server function for the file viewer module |
|
| 127 |
srv_viewer <- function(id, input_path) {
|
|
| 128 | ! |
moduleServer(id, function(input, output, session) {
|
| 129 | ! |
teal.logger::log_shiny_input_changes(input, namespace = "teal.modules.general") |
| 130 | ||
| 131 | ! |
temp_dir <- tempfile() |
| 132 | ! |
if (!dir.exists(temp_dir)) {
|
| 133 | ! |
dir.create(temp_dir, recursive = TRUE) |
| 134 |
} |
|
| 135 | ! |
addResourcePath(basename(temp_dir), temp_dir) |
| 136 | ||
| 137 | ! |
test_path_text <- function(selected_path, type) {
|
| 138 | ! |
out <- tryCatch( |
| 139 | ! |
expr = {
|
| 140 | ! |
if (type != "url") {
|
| 141 | ! |
selected_path <- normalizePath(selected_path, winslash = "/") |
| 142 |
} |
|
| 143 | ! |
readLines(con = selected_path) |
| 144 |
}, |
|
| 145 | ! |
error = function(cond) FALSE, |
| 146 | ! |
warning = function(cond) {
|
| 147 | ! |
`if`(grepl("^incomplete final line found on", cond[[1]]), suppressWarnings(eval(cond[[2]])), FALSE)
|
| 148 |
} |
|
| 149 |
) |
|
| 150 |
} |
|
| 151 | ||
| 152 | ! |
handle_connection_type <- function(selected_path) {
|
| 153 | ! |
file_extension <- tools::file_ext(selected_path) |
| 154 | ! |
file_class <- suppressWarnings(file(selected_path)) |
| 155 | ! |
close(file_class) |
| 156 | ||
| 157 | ! |
output_text <- test_path_text(selected_path, type = class(file_class)[1]) |
| 158 | ||
| 159 | ! |
if (class(file_class)[1] == "url") {
|
| 160 | ! |
list(selected_path = selected_path, output_text = output_text) |
| 161 |
} else {
|
|
| 162 | ! |
file.copy(normalizePath(selected_path, winslash = "/"), temp_dir) |
| 163 | ! |
selected_path <- file.path(basename(temp_dir), basename(selected_path)) |
| 164 | ! |
list(selected_path = selected_path, output_text = output_text) |
| 165 |
} |
|
| 166 |
} |
|
| 167 | ||
| 168 | ! |
display_file <- function(selected_path) {
|
| 169 | ! |
con_type <- handle_connection_type(selected_path) |
| 170 | ! |
file_extension <- tools::file_ext(selected_path) |
| 171 | ! |
if (file_extension %in% c("png", "apng", "jpg", "jpeg", "svg", "gif", "webp", "bmp")) {
|
| 172 | ! |
tags$img(src = con_type$selected_path, alt = "file does not exist") |
| 173 | ! |
} else if (file_extension == "pdf") {
|
| 174 | ! |
tags$embed( |
| 175 | ! |
style = "height: 600px; width: 100%;", |
| 176 | ! |
src = con_type$selected_path |
| 177 |
) |
|
| 178 | ! |
} else if (!isFALSE(con_type$output_text[1])) {
|
| 179 | ! |
tags$pre(paste0(con_type$output_text, collapse = "\n")) |
| 180 |
} else {
|
|
| 181 | ! |
tags$p("Please select a supported format.")
|
| 182 |
} |
|
| 183 |
} |
|
| 184 | ||
| 185 | ! |
tree_list <- function(file_or_dir) {
|
| 186 | ! |
nested_list <- lapply(file_or_dir, function(path) {
|
| 187 | ! |
file_class <- suppressWarnings(file(path)) |
| 188 | ! |
close(file_class) |
| 189 | ! |
if (class(file_class)[[1]] != "url") {
|
| 190 | ! |
isdir <- file.info(path)$isdir |
| 191 | ! |
if (!isdir) {
|
| 192 | ! |
structure(path, ancestry = path, sticon = "file") |
| 193 |
} else {
|
|
| 194 | ! |
files <- list.files(path, full.names = TRUE, include.dirs = TRUE) |
| 195 | ! |
out <- lapply(files, function(x) tree_list(x)) |
| 196 | ! |
out <- unlist(out, recursive = FALSE) |
| 197 | ! |
if (length(files) > 0) names(out) <- basename(files) |
| 198 | ! |
out |
| 199 |
} |
|
| 200 |
} else {
|
|
| 201 | ! |
structure(path, ancestry = path, sticon = "file") |
| 202 |
} |
|
| 203 |
}) |
|
| 204 | ||
| 205 | ! |
missing_labels <- if (is.null(names(nested_list))) seq_along(nested_list) else which(names(nested_list) == "") |
| 206 | ! |
names(nested_list)[missing_labels] <- file_or_dir[missing_labels] |
| 207 | ! |
nested_list |
| 208 |
} |
|
| 209 | ||
| 210 | ! |
output$tree <- shinyTree::renderTree({
|
| 211 | ! |
if (length(input_path) > 0) {
|
| 212 | ! |
tree_list(input_path) |
| 213 |
} else {
|
|
| 214 | ! |
list("Empty Path" = NULL)
|
| 215 |
} |
|
| 216 |
}) |
|
| 217 | ||
| 218 | ! |
output$output <- renderUI({
|
| 219 | ! |
validate( |
| 220 | ! |
need( |
| 221 | ! |
length(shinyTree::get_selected(input$tree)) > 0, |
| 222 | ! |
"Please select a file." |
| 223 |
) |
|
| 224 |
) |
|
| 225 | ||
| 226 | ! |
obj <- shinyTree::get_selected(input$tree, format = "names")[[1]] |
| 227 | ! |
repo <- attr(obj, "ancestry") |
| 228 | ! |
repo_collapsed <- if (length(repo) > 1) paste0(repo, collapse = "/") else repo |
| 229 | ! |
is_not_named <- file.exists(file.path(c(repo_collapsed, obj[1])))[1] |
| 230 | ||
| 231 | ! |
if (is_not_named) {
|
| 232 | ! |
selected_path <- do.call("file.path", as.list(c(repo, obj[1])))
|
| 233 |
} else {
|
|
| 234 | ! |
if (length(repo) == 0) {
|
| 235 | ! |
selected_path <- do.call("file.path", as.list(attr(input$tree[[obj[1]]], "ancestry")))
|
| 236 |
} else {
|
|
| 237 | ! |
selected_path <- do.call("file.path", as.list(attr(input$tree[[repo]][[obj[1]]], "ancestry")))
|
| 238 |
} |
|
| 239 |
} |
|
| 240 | ||
| 241 | ! |
validate( |
| 242 | ! |
need( |
| 243 | ! |
!isTRUE(file.info(selected_path)$isdir) && length(selected_path) > 0, |
| 244 | ! |
"Please select a single file." |
| 245 |
) |
|
| 246 |
) |
|
| 247 | ! |
display_file(selected_path) |
| 248 |
}) |
|
| 249 | ||
| 250 | ! |
onStop(function() {
|
| 251 | ! |
removeResourcePath(basename(temp_dir)) |
| 252 | ! |
unlink(temp_dir) |
| 253 |
}) |
|
| 254 |
}) |
|
| 255 |
} |
| 1 |
#' `teal` module: Scatterplot and regression analysis |
|
| 2 |
#' |
|
| 3 |
#' Module for visualizing regression analysis, including scatterplots and |
|
| 4 |
#' various regression diagnostics plots. |
|
| 5 |
#' It allows users to explore the relationship between a set of regressors and a response variable, |
|
| 6 |
#' visualize residuals, and identify outliers. |
|
| 7 |
#' |
|
| 8 |
#' @note For more examples, please see the vignette "Using regression plots" via |
|
| 9 |
#' `vignette("using-regression-plots", package = "teal.modules.general")`.
|
|
| 10 |
#' |
|
| 11 |
#' @inheritParams teal::module |
|
| 12 |
#' @inheritParams shared_params |
|
| 13 |
#' @param regressor (`data_extract_spec` or `list` of multiple `data_extract_spec`) |
|
| 14 |
#' Regressor variables from an incoming dataset with filtering and selecting. |
|
| 15 |
#' @param response (`data_extract_spec` or `list` of multiple `data_extract_spec`) |
|
| 16 |
#' Response variables from an incoming dataset with filtering and selecting. |
|
| 17 |
#' @param default_outlier_label (`character`) optional, default column selected to label outliers. |
|
| 18 |
#' @param default_plot_type (`numeric`) optional, defaults to "Response vs Regressor". |
|
| 19 |
#' 1. Response vs Regressor |
|
| 20 |
#' 2. Residuals vs Fitted |
|
| 21 |
#' 3. Normal Q-Q |
|
| 22 |
#' 4. Scale-Location |
|
| 23 |
#' 5. Cook's distance |
|
| 24 |
#' 6. Residuals vs Leverage |
|
| 25 |
#' 7. Cook's dist vs Leverage |
|
| 26 |
#' @param label_segment_threshold (`numeric(1)` or `numeric(3)`) |
|
| 27 |
#' Minimum distance between label and point on the plot that triggers the creation of |
|
| 28 |
#' a line segment between the two. |
|
| 29 |
#' This may happen when the label cannot be placed next to the point as it overlaps another |
|
| 30 |
#' label or point. |
|
| 31 |
#' The value is used as the `min.segment.length` parameter to the [ggrepel::geom_text_repel()] function. |
|
| 32 |
#' |
|
| 33 |
#' It can take the following forms: |
|
| 34 |
#' - `numeric(1)`: Fixed value used for the minimum distance and the slider is not presented in the UI. |
|
| 35 |
#' - `numeric(3)`: A slider is presented in the UI (under "Plot settings") to adjust the minimum distance dynamically. |
|
| 36 |
#' |
|
| 37 |
#' It takes the form of `c(value, min, max)` and it is passed to the `value_min_max` |
|
| 38 |
#' argument in `teal.widgets::optionalSliderInputValMinMax`. |
|
| 39 |
#' |
|
| 40 |
# nolint start: line_length. |
|
| 41 |
#' @param ggplot2_args `r roxygen_ggplot2_args_param("Response vs Regressor", "Residuals vs Fitted", "Scale-Location", "Cook's distance", "Residuals vs Leverage", "Cook's dist vs Leverage")`
|
|
| 42 |
# nolint end: line_length. |
|
| 43 |
#' |
|
| 44 |
#' @inherit shared_params return |
|
| 45 |
#' |
|
| 46 |
#' @section Decorating Module: |
|
| 47 |
#' |
|
| 48 |
#' This module generates the following objects, which can be modified in place using decorators: |
|
| 49 |
#' - `plot` (`ggplot`) |
|
| 50 |
#' |
|
| 51 |
#' A Decorator is applied to the specific output using a named list of `teal_transform_module` objects. |
|
| 52 |
#' The name of this list corresponds to the name of the output to which the decorator is applied. |
|
| 53 |
#' See code snippet below: |
|
| 54 |
#' |
|
| 55 |
#' ``` |
|
| 56 |
#' tm_a_regression( |
|
| 57 |
#' ..., # arguments for module |
|
| 58 |
#' decorators = list( |
|
| 59 |
#' plot = teal_transform_module(...) # applied to the `plot` output |
|
| 60 |
#' ) |
|
| 61 |
#' ) |
|
| 62 |
#' ``` |
|
| 63 |
#' |
|
| 64 |
#' For additional details and examples of decorators, refer to the vignette |
|
| 65 |
#' `vignette("decorate-module-output", package = "teal.modules.general")`.
|
|
| 66 |
#' |
|
| 67 |
#' To learn more please refer to the vignette |
|
| 68 |
#' `vignette("transform-module-output", package = "teal")` or the [`teal::teal_transform_module()`] documentation.
|
|
| 69 |
#' |
|
| 70 |
#' @examplesShinylive |
|
| 71 |
#' library(teal.modules.general) |
|
| 72 |
#' interactive <- function() TRUE |
|
| 73 |
#' {{ next_example }}
|
|
| 74 |
#' @examples |
|
| 75 |
#' |
|
| 76 |
#' # general data example |
|
| 77 |
#' data <- teal_data() |
|
| 78 |
#' data <- within(data, {
|
|
| 79 |
#' require(nestcolor) |
|
| 80 |
#' CO2 <- CO2 |
|
| 81 |
#' }) |
|
| 82 |
#' |
|
| 83 |
#' app <- init( |
|
| 84 |
#' data = data, |
|
| 85 |
#' modules = modules( |
|
| 86 |
#' tm_a_regression( |
|
| 87 |
#' label = "Regression", |
|
| 88 |
#' response = data_extract_spec( |
|
| 89 |
#' dataname = "CO2", |
|
| 90 |
#' select = select_spec( |
|
| 91 |
#' label = "Select variable:", |
|
| 92 |
#' choices = "uptake", |
|
| 93 |
#' selected = "uptake", |
|
| 94 |
#' multiple = FALSE, |
|
| 95 |
#' fixed = TRUE |
|
| 96 |
#' ) |
|
| 97 |
#' ), |
|
| 98 |
#' regressor = data_extract_spec( |
|
| 99 |
#' dataname = "CO2", |
|
| 100 |
#' select = select_spec( |
|
| 101 |
#' label = "Select variables:", |
|
| 102 |
#' choices = variable_choices(data[["CO2"]], c("conc", "Treatment")),
|
|
| 103 |
#' selected = "conc", |
|
| 104 |
#' multiple = TRUE, |
|
| 105 |
#' fixed = FALSE |
|
| 106 |
#' ) |
|
| 107 |
#' ) |
|
| 108 |
#' ) |
|
| 109 |
#' ) |
|
| 110 |
#' ) |
|
| 111 |
#' if (interactive()) {
|
|
| 112 |
#' shinyApp(app$ui, app$server) |
|
| 113 |
#' } |
|
| 114 |
#' |
|
| 115 |
#' @examplesShinylive |
|
| 116 |
#' library(teal.modules.general) |
|
| 117 |
#' interactive <- function() TRUE |
|
| 118 |
#' {{ next_example }}
|
|
| 119 |
#' @examples |
|
| 120 |
#' # CDISC data example |
|
| 121 |
#' data <- teal_data() |
|
| 122 |
#' data <- within(data, {
|
|
| 123 |
#' require(nestcolor) |
|
| 124 |
#' ADSL <- teal.data::rADSL |
|
| 125 |
#' }) |
|
| 126 |
#' join_keys(data) <- default_cdisc_join_keys[names(data)] |
|
| 127 |
#' |
|
| 128 |
#' app <- init( |
|
| 129 |
#' data = data, |
|
| 130 |
#' modules = modules( |
|
| 131 |
#' tm_a_regression( |
|
| 132 |
#' label = "Regression", |
|
| 133 |
#' response = data_extract_spec( |
|
| 134 |
#' dataname = "ADSL", |
|
| 135 |
#' select = select_spec( |
|
| 136 |
#' label = "Select variable:", |
|
| 137 |
#' choices = "BMRKR1", |
|
| 138 |
#' selected = "BMRKR1", |
|
| 139 |
#' multiple = FALSE, |
|
| 140 |
#' fixed = TRUE |
|
| 141 |
#' ) |
|
| 142 |
#' ), |
|
| 143 |
#' regressor = data_extract_spec( |
|
| 144 |
#' dataname = "ADSL", |
|
| 145 |
#' select = select_spec( |
|
| 146 |
#' label = "Select variables:", |
|
| 147 |
#' choices = variable_choices(data[["ADSL"]], c("AGE", "SEX", "RACE")),
|
|
| 148 |
#' selected = "AGE", |
|
| 149 |
#' multiple = TRUE, |
|
| 150 |
#' fixed = FALSE |
|
| 151 |
#' ) |
|
| 152 |
#' ) |
|
| 153 |
#' ) |
|
| 154 |
#' ) |
|
| 155 |
#' ) |
|
| 156 |
#' if (interactive()) {
|
|
| 157 |
#' shinyApp(app$ui, app$server) |
|
| 158 |
#' } |
|
| 159 |
#' |
|
| 160 |
#' @export |
|
| 161 |
#' |
|
| 162 |
tm_a_regression <- function(label = "Regression Analysis", |
|
| 163 |
regressor, |
|
| 164 |
response, |
|
| 165 |
plot_height = c(600, 200, 2000), |
|
| 166 |
plot_width = NULL, |
|
| 167 |
alpha = c(1, 0, 1), |
|
| 168 |
size = c(2, 1, 8), |
|
| 169 |
ggtheme = c("gray", "bw", "linedraw", "light", "dark", "minimal", "classic", "void"),
|
|
| 170 |
ggplot2_args = teal.widgets::ggplot2_args(), |
|
| 171 |
pre_output = NULL, |
|
| 172 |
post_output = NULL, |
|
| 173 |
default_plot_type = 1, |
|
| 174 |
default_outlier_label = "USUBJID", |
|
| 175 |
label_segment_threshold = c(0.5, 0, 10), |
|
| 176 |
transformators = list(), |
|
| 177 |
decorators = list()) {
|
|
| 178 | ! |
message("Initializing tm_a_regression")
|
| 179 | ||
| 180 |
# Normalize the parameters |
|
| 181 | ! |
if (inherits(regressor, "data_extract_spec")) regressor <- list(regressor) |
| 182 | ! |
if (inherits(response, "data_extract_spec")) response <- list(response) |
| 183 | ! |
if (inherits(ggplot2_args, "ggplot2_args")) ggplot2_args <- list(default = ggplot2_args) |
| 184 | ||
| 185 |
# Start of assertions |
|
| 186 | ! |
checkmate::assert_string(label) |
| 187 | ! |
checkmate::assert_list(regressor, types = "data_extract_spec") |
| 188 | ||
| 189 | ! |
checkmate::assert_list(response, types = "data_extract_spec") |
| 190 | ! |
assert_single_selection(response) |
| 191 | ||
| 192 | ! |
checkmate::assert_numeric(plot_height, len = 3, any.missing = FALSE, finite = TRUE) |
| 193 | ! |
checkmate::assert_numeric(plot_height[1], lower = plot_height[2], upper = plot_height[3], .var.name = "plot_height") |
| 194 | ||
| 195 | ! |
checkmate::assert_numeric(plot_width, len = 3, any.missing = FALSE, null.ok = TRUE, finite = TRUE) |
| 196 | ! |
checkmate::assert_numeric( |
| 197 | ! |
plot_width[1], |
| 198 | ! |
lower = plot_width[2], |
| 199 | ! |
upper = plot_width[3], |
| 200 | ! |
null.ok = TRUE, |
| 201 | ! |
.var.name = "plot_width" |
| 202 |
) |
|
| 203 | ||
| 204 | ! |
if (length(alpha) == 1) {
|
| 205 | ! |
checkmate::assert_numeric(alpha, any.missing = FALSE, finite = TRUE) |
| 206 |
} else {
|
|
| 207 | ! |
checkmate::assert_numeric(alpha, len = 3, any.missing = FALSE, finite = TRUE) |
| 208 | ! |
checkmate::assert_numeric(alpha[1], lower = alpha[2], upper = alpha[3], .var.name = "alpha") |
| 209 |
} |
|
| 210 | ||
| 211 | ! |
if (length(size) == 1) {
|
| 212 | ! |
checkmate::assert_numeric(size, any.missing = FALSE, finite = TRUE) |
| 213 |
} else {
|
|
| 214 | ! |
checkmate::assert_numeric(size, len = 3, any.missing = FALSE, finite = TRUE) |
| 215 | ! |
checkmate::assert_numeric(size[1], lower = size[2], upper = size[3], .var.name = "size") |
| 216 |
} |
|
| 217 | ||
| 218 | ! |
ggtheme <- match.arg(ggtheme) |
| 219 | ||
| 220 | ! |
plot_choices <- c( |
| 221 | ! |
"Response vs Regressor", "Residuals vs Fitted", "Normal Q-Q", "Scale-Location", |
| 222 | ! |
"Cook's distance", "Residuals vs Leverage", "Cook's dist vs Leverage" |
| 223 |
) |
|
| 224 | ! |
checkmate::assert_list(ggplot2_args, types = "ggplot2_args") |
| 225 | ! |
checkmate::assert_subset(names(ggplot2_args), c("default", plot_choices))
|
| 226 | ||
| 227 | ! |
checkmate::assert_multi_class(pre_output, c("shiny.tag", "shiny.tag.list", "html"), null.ok = TRUE)
|
| 228 | ! |
checkmate::assert_multi_class(post_output, c("shiny.tag", "shiny.tag.list", "html"), null.ok = TRUE)
|
| 229 | ! |
checkmate::assert_choice(default_plot_type, seq.int(1L, length(plot_choices))) |
| 230 | ! |
checkmate::assert_string(default_outlier_label) |
| 231 | ! |
checkmate::assert_list(decorators, "teal_transform_module") |
| 232 | ||
| 233 | ! |
if (length(label_segment_threshold) == 1) {
|
| 234 | ! |
checkmate::assert_numeric(label_segment_threshold, any.missing = FALSE, finite = TRUE) |
| 235 |
} else {
|
|
| 236 | ! |
checkmate::assert_numeric(label_segment_threshold, len = 3, any.missing = FALSE, finite = TRUE) |
| 237 | ! |
checkmate::assert_numeric( |
| 238 | ! |
label_segment_threshold[1], |
| 239 | ! |
lower = label_segment_threshold[2], |
| 240 | ! |
upper = label_segment_threshold[3], |
| 241 | ! |
.var.name = "label_segment_threshold" |
| 242 |
) |
|
| 243 |
} |
|
| 244 | ! |
assert_decorators(decorators, "plot") |
| 245 |
# End of assertions |
|
| 246 | ||
| 247 |
# Make UI args |
|
| 248 | ! |
args <- as.list(environment()) |
| 249 | ! |
args[["plot_choices"]] <- plot_choices |
| 250 | ! |
data_extract_list <- list( |
| 251 | ! |
regressor = regressor, |
| 252 | ! |
response = response |
| 253 |
) |
|
| 254 | ||
| 255 | ! |
ans <- module( |
| 256 | ! |
label = label, |
| 257 | ! |
server = srv_a_regression, |
| 258 | ! |
ui = ui_a_regression, |
| 259 | ! |
ui_args = args, |
| 260 | ! |
server_args = c( |
| 261 | ! |
data_extract_list, |
| 262 | ! |
list( |
| 263 | ! |
plot_height = plot_height, |
| 264 | ! |
plot_width = plot_width, |
| 265 | ! |
default_outlier_label = default_outlier_label, |
| 266 | ! |
ggplot2_args = ggplot2_args, |
| 267 | ! |
decorators = decorators |
| 268 |
) |
|
| 269 |
), |
|
| 270 | ! |
transformators = transformators, |
| 271 | ! |
datanames = teal.transform::get_extract_datanames(data_extract_list) |
| 272 |
) |
|
| 273 | ! |
attr(ans, "teal_bookmarkable") <- FALSE |
| 274 | ! |
ans |
| 275 |
} |
|
| 276 | ||
| 277 |
# UI function for the regression module |
|
| 278 |
ui_a_regression <- function(id, ...) {
|
|
| 279 | ! |
ns <- NS(id) |
| 280 | ! |
args <- list(...) |
| 281 | ! |
is_single_dataset_value <- teal.transform::is_single_dataset(args$regressor, args$response) |
| 282 | ! |
teal.widgets::standard_layout( |
| 283 | ! |
output = teal.widgets::white_small_well(tags$div( |
| 284 | ! |
teal.widgets::plot_with_settings_ui(id = ns("myplot")),
|
| 285 | ! |
tags$div(verbatimTextOutput(ns("text")))
|
| 286 |
)), |
|
| 287 | ! |
encoding = tags$div( |
| 288 |
### Reporter |
|
| 289 | ! |
teal.reporter::add_card_button_ui(ns("add_reporter"), label = "Add Report Card"),
|
| 290 | ! |
tags$br(), tags$br(), |
| 291 |
### |
|
| 292 | ! |
tags$label("Encodings", class = "text-primary"), tags$br(),
|
| 293 | ! |
teal.transform::datanames_input(args[c("response", "regressor")]),
|
| 294 | ! |
teal.transform::data_extract_ui( |
| 295 | ! |
id = ns("response"),
|
| 296 | ! |
label = "Response variable", |
| 297 | ! |
data_extract_spec = args$response, |
| 298 | ! |
is_single_dataset = is_single_dataset_value |
| 299 |
), |
|
| 300 | ! |
teal.transform::data_extract_ui( |
| 301 | ! |
id = ns("regressor"),
|
| 302 | ! |
label = "Regressor variables", |
| 303 | ! |
data_extract_spec = args$regressor, |
| 304 | ! |
is_single_dataset = is_single_dataset_value |
| 305 |
), |
|
| 306 | ! |
radioButtons( |
| 307 | ! |
ns("plot_type"),
|
| 308 | ! |
label = "Plot type:", |
| 309 | ! |
choices = args$plot_choices, |
| 310 | ! |
selected = args$plot_choices[args$default_plot_type] |
| 311 |
), |
|
| 312 | ! |
checkboxInput(ns("show_outlier"), label = "Display outlier labels", value = TRUE),
|
| 313 | ! |
conditionalPanel( |
| 314 | ! |
condition = "input['show_outlier']", |
| 315 | ! |
ns = ns, |
| 316 | ! |
teal.widgets::optionalSliderInput( |
| 317 | ! |
ns("outlier"),
|
| 318 | ! |
tags$div( |
| 319 | ! |
tagList( |
| 320 | ! |
"Outlier definition:", |
| 321 | ! |
bslib::tooltip( |
| 322 | ! |
icon("fas fa-circle-info"),
|
| 323 | ! |
paste( |
| 324 | ! |
"Use the slider to choose the cut-off value to define outliers.", |
| 325 | ! |
"Points with a Cook's distance greater than", |
| 326 | ! |
"the value on the slider times the mean of the Cook's distance of the dataset will have labels." |
| 327 |
) |
|
| 328 |
) |
|
| 329 |
) |
|
| 330 |
), |
|
| 331 | ! |
min = 1, max = 10, value = 9, ticks = FALSE, step = .1 |
| 332 |
), |
|
| 333 | ! |
teal.widgets::optionalSelectInput( |
| 334 | ! |
ns("label_var"),
|
| 335 | ! |
multiple = FALSE, |
| 336 | ! |
label = "Outlier label" |
| 337 |
) |
|
| 338 |
), |
|
| 339 | ! |
ui_decorate_teal_data(ns("decorator"), decorators = select_decorators(args$decorators, "plot")),
|
| 340 | ! |
bslib::accordion( |
| 341 | ! |
open = TRUE, |
| 342 | ! |
bslib::accordion_panel( |
| 343 | ! |
title = "Plot settings", |
| 344 | ! |
teal.widgets::optionalSliderInputValMinMax(ns("alpha"), "Opacity:", args$alpha, ticks = FALSE),
|
| 345 | ! |
teal.widgets::optionalSliderInputValMinMax(ns("size"), "Points size:", args$size, ticks = FALSE),
|
| 346 | ! |
teal.widgets::optionalSliderInputValMinMax( |
| 347 | ! |
inputId = ns("label_min_segment"),
|
| 348 | ! |
label = tags$div( |
| 349 | ! |
tagList( |
| 350 | ! |
"Label min. segment:", |
| 351 | ! |
bslib::tooltip( |
| 352 | ! |
icon("circle-info"),
|
| 353 | ! |
tags$span( |
| 354 | ! |
paste( |
| 355 | ! |
"Use the slider to choose the cut-off value to define minimum distance between label and point", |
| 356 | ! |
"that generates a line segment.", |
| 357 | ! |
"It's only valid when 'Display outlier labels' is checked." |
| 358 |
) |
|
| 359 |
) |
|
| 360 |
) |
|
| 361 |
) |
|
| 362 |
), |
|
| 363 | ! |
value_min_max = args$label_segment_threshold, |
| 364 |
# Extra parameters to sliderInput |
|
| 365 | ! |
ticks = FALSE, |
| 366 | ! |
step = .1, |
| 367 | ! |
round = FALSE |
| 368 |
), |
|
| 369 | ! |
selectInput( |
| 370 | ! |
inputId = ns("ggtheme"),
|
| 371 | ! |
label = "Theme (by ggplot):", |
| 372 | ! |
choices = ggplot_themes, |
| 373 | ! |
selected = args$ggtheme, |
| 374 | ! |
multiple = FALSE |
| 375 |
) |
|
| 376 |
) |
|
| 377 |
) |
|
| 378 |
), |
|
| 379 | ! |
forms = tagList( |
| 380 | ! |
teal.widgets::verbatim_popup_ui(ns("rcode"), "Show R code")
|
| 381 |
), |
|
| 382 | ! |
pre_output = args$pre_output, |
| 383 | ! |
post_output = args$post_output |
| 384 |
) |
|
| 385 |
} |
|
| 386 | ||
| 387 |
# Server function for the regression module |
|
| 388 |
srv_a_regression <- function(id, |
|
| 389 |
data, |
|
| 390 |
reporter, |
|
| 391 |
filter_panel_api, |
|
| 392 |
response, |
|
| 393 |
regressor, |
|
| 394 |
plot_height, |
|
| 395 |
plot_width, |
|
| 396 |
ggplot2_args, |
|
| 397 |
default_outlier_label, |
|
| 398 |
decorators) {
|
|
| 399 | ! |
with_reporter <- !missing(reporter) && inherits(reporter, "Reporter") |
| 400 | ! |
with_filter <- !missing(filter_panel_api) && inherits(filter_panel_api, "FilterPanelAPI") |
| 401 | ! |
checkmate::assert_class(data, "reactive") |
| 402 | ! |
checkmate::assert_class(isolate(data()), "teal_data") |
| 403 | ! |
moduleServer(id, function(input, output, session) {
|
| 404 | ! |
teal.logger::log_shiny_input_changes(input, namespace = "teal.modules.general") |
| 405 | ||
| 406 | ! |
ns <- session$ns |
| 407 | ||
| 408 | ! |
rule_rvr1 <- function(value) {
|
| 409 | ! |
if (isTRUE(input$plot_type == "Response vs Regressor")) {
|
| 410 | ! |
if (length(value) > 1L) {
|
| 411 | ! |
"This plot can only have one regressor." |
| 412 |
} |
|
| 413 |
} |
|
| 414 |
} |
|
| 415 | ! |
rule_rvr2 <- function(other) {
|
| 416 | ! |
function(value) {
|
| 417 | ! |
if (isTRUE(input$plot_type == "Response vs Regressor")) {
|
| 418 | ! |
otherval <- selector_list()[[other]]()$select |
| 419 | ! |
if (isTRUE(value == otherval)) {
|
| 420 | ! |
"Response and Regressor must be different." |
| 421 |
} |
|
| 422 |
} |
|
| 423 |
} |
|
| 424 |
} |
|
| 425 | ||
| 426 | ! |
selector_list <- teal.transform::data_extract_multiple_srv( |
| 427 | ! |
data_extract = list(response = response, regressor = regressor), |
| 428 | ! |
datasets = data, |
| 429 | ! |
select_validation_rule = list( |
| 430 | ! |
regressor = shinyvalidate::compose_rules( |
| 431 | ! |
shinyvalidate::sv_required("At least one regressor should be selected."),
|
| 432 | ! |
rule_rvr1, |
| 433 | ! |
rule_rvr2("response")
|
| 434 |
), |
|
| 435 | ! |
response = shinyvalidate::compose_rules( |
| 436 | ! |
shinyvalidate::sv_required("At least one response should be selected."),
|
| 437 | ! |
rule_rvr2("regressor")
|
| 438 |
) |
|
| 439 |
) |
|
| 440 |
) |
|
| 441 | ||
| 442 | ! |
iv_r <- reactive({
|
| 443 | ! |
iv <- shinyvalidate::InputValidator$new() |
| 444 | ! |
teal.transform::compose_and_enable_validators(iv, selector_list) |
| 445 |
}) |
|
| 446 | ||
| 447 | ! |
iv_out <- shinyvalidate::InputValidator$new() |
| 448 | ! |
iv_out$condition(~ isTRUE(input$show_outlier)) |
| 449 | ! |
iv_out$add_rule("label_var", shinyvalidate::sv_required("Please provide an `Outlier label` variable"))
|
| 450 | ! |
iv_out$enable() |
| 451 | ||
| 452 | ! |
anl_merged_input <- teal.transform::merge_expression_srv( |
| 453 | ! |
selector_list = selector_list, |
| 454 | ! |
datasets = data |
| 455 |
) |
|
| 456 | ||
| 457 | ! |
regression_var <- reactive({
|
| 458 | ! |
teal::validate_inputs(iv_r()) |
| 459 | ||
| 460 | ! |
list( |
| 461 | ! |
response = as.vector(anl_merged_input()$columns_source$response), |
| 462 | ! |
regressor = as.vector(anl_merged_input()$columns_source$regressor) |
| 463 |
) |
|
| 464 |
}) |
|
| 465 | ||
| 466 | ! |
qenv <- reactive( |
| 467 | ! |
teal.code::eval_code(data(), 'library("ggplot2");library("dplyr")') # nolint quotes
|
| 468 |
) |
|
| 469 | ||
| 470 | ! |
anl_merged_q <- reactive({
|
| 471 | ! |
req(anl_merged_input()) |
| 472 | ! |
qenv() %>% |
| 473 | ! |
teal.code::eval_code(as.expression(anl_merged_input()$expr)) |
| 474 |
}) |
|
| 475 | ||
| 476 |
# sets qenv object and populates it with data merge call and fit expression |
|
| 477 | ! |
fit_r <- reactive({
|
| 478 | ! |
ANL <- anl_merged_q()[["ANL"]] |
| 479 | ! |
teal::validate_has_data(ANL, 10) |
| 480 | ||
| 481 | ! |
validate(need(is.numeric(ANL[regression_var()$response][[1]]), "Response variable should be numeric.")) |
| 482 | ||
| 483 | ! |
teal::validate_has_data( |
| 484 | ! |
ANL[, c(regression_var()$response, regression_var()$regressor)], 10, |
| 485 | ! |
complete = TRUE, allow_inf = FALSE |
| 486 |
) |
|
| 487 | ||
| 488 | ! |
form <- stats::as.formula( |
| 489 | ! |
paste( |
| 490 | ! |
regression_var()$response, |
| 491 | ! |
paste( |
| 492 | ! |
regression_var()$regressor, |
| 493 | ! |
collapse = " + " |
| 494 |
), |
|
| 495 | ! |
sep = " ~ " |
| 496 |
) |
|
| 497 |
) |
|
| 498 | ||
| 499 | ! |
if (input$show_outlier) {
|
| 500 | ! |
opts <- teal.transform::variable_choices(ANL) |
| 501 | ! |
selected <- if (!is.null(isolate(input$label_var)) && isolate(input$label_var) %in% as.character(opts)) {
|
| 502 | ! |
isolate(input$label_var) |
| 503 |
} else {
|
|
| 504 | ! |
if (length(opts[as.character(opts) == default_outlier_label]) == 0) {
|
| 505 | ! |
opts[[1]] |
| 506 |
} else {
|
|
| 507 | ! |
opts[as.character(opts) == default_outlier_label] |
| 508 |
} |
|
| 509 |
} |
|
| 510 | ! |
teal.widgets::updateOptionalSelectInput( |
| 511 | ! |
session = session, |
| 512 | ! |
inputId = "label_var", |
| 513 | ! |
choices = opts, |
| 514 | ! |
selected = restoreInput(ns("label_var"), selected)
|
| 515 |
) |
|
| 516 | ||
| 517 | ! |
data <- ggplot2::fortify(stats::lm(form, data = ANL)) |
| 518 | ! |
cooksd <- data$.cooksd[!is.nan(data$.cooksd)] |
| 519 | ! |
max_outlier <- max(ceiling(max(cooksd) / mean(cooksd)), 2) |
| 520 | ! |
cur_outlier <- isolate(input$outlier) |
| 521 | ! |
updateSliderInput( |
| 522 | ! |
session = session, |
| 523 | ! |
inputId = "outlier", |
| 524 | ! |
min = 1, |
| 525 | ! |
max = max_outlier, |
| 526 | ! |
value = restoreInput(ns("outlier"), if (cur_outlier < max_outlier) cur_outlier else max_outlier * .9)
|
| 527 |
) |
|
| 528 |
} |
|
| 529 | ||
| 530 | ! |
anl_merged_q() %>% |
| 531 | ! |
teal.code::eval_code(substitute(fit <- stats::lm(form, data = ANL), env = list(form = form))) %>% |
| 532 | ! |
teal.code::eval_code(quote({
|
| 533 | ! |
for (regressor in names(fit$contrasts)) {
|
| 534 | ! |
alts <- paste0(levels(ANL[[regressor]]), collapse = "|") |
| 535 | ! |
names(fit$coefficients) <- gsub( |
| 536 | ! |
paste0("^(", regressor, ")(", alts, ")$"), paste0("\\1", ": ", "\\2"), names(fit$coefficients)
|
| 537 |
) |
|
| 538 |
} |
|
| 539 |
})) %>% |
|
| 540 | ! |
teal.code::eval_code(quote(summary(fit))) |
| 541 |
}) |
|
| 542 | ||
| 543 | ! |
label_col <- reactive({
|
| 544 | ! |
teal::validate_inputs(iv_out) |
| 545 | ||
| 546 | ! |
substitute( |
| 547 | ! |
expr = dplyr::if_else( |
| 548 | ! |
data$.cooksd > outliers * mean(data$.cooksd, na.rm = TRUE), |
| 549 | ! |
as.character(stats::na.omit(ANL)[[label_var]]), |
| 550 |
"" |
|
| 551 |
) %>% |
|
| 552 | ! |
dplyr::if_else(is.na(.), "cooksd == NaN", .), |
| 553 | ! |
env = list(outliers = input$outlier, label_var = input$label_var) |
| 554 |
) |
|
| 555 |
}) |
|
| 556 | ||
| 557 | ! |
label_min_segment <- reactive({
|
| 558 | ! |
input$label_min_segment |
| 559 |
}) |
|
| 560 | ||
| 561 | ! |
outlier_label <- reactive({
|
| 562 | ! |
substitute( |
| 563 | ! |
expr = ggrepel::geom_text_repel( |
| 564 | ! |
label = label_col, |
| 565 | ! |
color = "red", |
| 566 | ! |
hjust = 0, |
| 567 | ! |
vjust = 1, |
| 568 | ! |
max.overlaps = Inf, |
| 569 | ! |
min.segment.length = label_min_segment, |
| 570 | ! |
segment.alpha = 0.5, |
| 571 | ! |
seed = 123 |
| 572 |
), |
|
| 573 | ! |
env = list(label_col = label_col(), label_min_segment = label_min_segment()) |
| 574 |
) |
|
| 575 |
}) |
|
| 576 | ||
| 577 | ! |
output_plot_base <- reactive({
|
| 578 | ! |
base_fit <- fit_r() |
| 579 | ! |
teal.code::eval_code( |
| 580 | ! |
base_fit, |
| 581 | ! |
quote({
|
| 582 | ! |
class(fit$residuals) <- NULL |
| 583 | ||
| 584 | ! |
data <- ggplot2::fortify(fit) |
| 585 | ||
| 586 | ! |
smooth <- function(x, y) {
|
| 587 | ! |
as.data.frame(stats::lowess(x, y, f = 2 / 3, iter = 3)) |
| 588 |
} |
|
| 589 | ||
| 590 | ! |
smoothy_aes <- ggplot2::aes_string(x = "x", y = "y") |
| 591 | ||
| 592 | ! |
reg_form <- deparse(fit$call[[2]]) |
| 593 |
}) |
|
| 594 |
) |
|
| 595 |
}) |
|
| 596 | ||
| 597 | ! |
output_plot_0 <- reactive({
|
| 598 | ! |
fit <- fit_r()[["fit"]] |
| 599 | ! |
ANL <- anl_merged_q()[["ANL"]] |
| 600 | ||
| 601 | ! |
stopifnot(ncol(fit$model) == 2) |
| 602 | ||
| 603 | ! |
if (!is.factor(ANL[[regression_var()$regressor]])) {
|
| 604 | ! |
shinyjs::show("size")
|
| 605 | ! |
shinyjs::show("alpha")
|
| 606 | ! |
plot <- substitute( |
| 607 | ! |
expr = ggplot2::ggplot(fit$model[, 2:1], ggplot2::aes_string(regressor, response)) + |
| 608 | ! |
ggplot2::geom_point(size = size, alpha = alpha) + |
| 609 | ! |
ggplot2::stat_smooth(method = "lm", formula = y ~ x, se = FALSE), |
| 610 | ! |
env = list( |
| 611 | ! |
regressor = regression_var()$regressor, |
| 612 | ! |
response = regression_var()$response, |
| 613 | ! |
size = input$size, |
| 614 | ! |
alpha = input$alpha |
| 615 |
) |
|
| 616 |
) |
|
| 617 | ! |
if (input$show_outlier) {
|
| 618 | ! |
plot <- substitute( |
| 619 | ! |
expr = plot + outlier_label, |
| 620 | ! |
env = list(plot = plot, outlier_label = outlier_label()) |
| 621 |
) |
|
| 622 |
} |
|
| 623 |
} else {
|
|
| 624 | ! |
shinyjs::hide("size")
|
| 625 | ! |
shinyjs::hide("alpha")
|
| 626 | ! |
plot <- substitute( |
| 627 | ! |
expr = ggplot2::ggplot(fit$model[, 2:1], ggplot2::aes_string(regressor, response)) + |
| 628 | ! |
ggplot2::geom_boxplot(), |
| 629 | ! |
env = list(regressor = regression_var()$regressor, response = regression_var()$response) |
| 630 |
) |
|
| 631 | ! |
if (input$show_outlier) {
|
| 632 | ! |
plot <- substitute(expr = plot + outlier_label, env = list(plot = plot, outlier_label = outlier_label())) |
| 633 |
} |
|
| 634 |
} |
|
| 635 | ||
| 636 | ! |
parsed_ggplot2_args <- teal.widgets::parse_ggplot2_args( |
| 637 | ! |
teal.widgets::resolve_ggplot2_args( |
| 638 | ! |
user_plot = ggplot2_args[["Response vs Regressor"]], |
| 639 | ! |
user_default = ggplot2_args$default, |
| 640 | ! |
module_plot = teal.widgets::ggplot2_args( |
| 641 | ! |
labs = list( |
| 642 | ! |
title = "Response vs Regressor", |
| 643 | ! |
x = varname_w_label(regression_var()$regressor, ANL), |
| 644 | ! |
y = varname_w_label(regression_var()$response, ANL) |
| 645 |
), |
|
| 646 | ! |
theme = list() |
| 647 |
) |
|
| 648 |
), |
|
| 649 | ! |
ggtheme = input$ggtheme |
| 650 |
) |
|
| 651 | ||
| 652 | ! |
teal.code::eval_code( |
| 653 | ! |
fit_r(), |
| 654 | ! |
substitute( |
| 655 | ! |
expr = {
|
| 656 | ! |
class(fit$residuals) <- NULL |
| 657 | ! |
data <- ggplot2::fortify(fit) |
| 658 | ! |
plot <- graph |
| 659 |
}, |
|
| 660 | ! |
env = list( |
| 661 | ! |
graph = Reduce(function(x, y) call("+", x, y), c(plot, parsed_ggplot2_args))
|
| 662 |
) |
|
| 663 |
) |
|
| 664 |
) |
|
| 665 |
}) |
|
| 666 | ||
| 667 | ! |
output_plot_1 <- reactive({
|
| 668 | ! |
plot_base <- output_plot_base() |
| 669 | ! |
shinyjs::show("size")
|
| 670 | ! |
shinyjs::show("alpha")
|
| 671 | ! |
plot <- substitute( |
| 672 | ! |
expr = ggplot2::ggplot(data = data, ggplot2::aes(.fitted, .resid)) + |
| 673 | ! |
ggplot2::geom_point(size = size, alpha = alpha) + |
| 674 | ! |
ggplot2::geom_hline(yintercept = 0, linetype = "dashed", size = 1) + |
| 675 | ! |
ggplot2::geom_line(data = smoothy, mapping = smoothy_aes), |
| 676 | ! |
env = list(size = input$size, alpha = input$alpha) |
| 677 |
) |
|
| 678 | ! |
if (input$show_outlier) {
|
| 679 | ! |
plot <- substitute(expr = plot + outlier_label, env = list(plot = plot, outlier_label = outlier_label())) |
| 680 |
} |
|
| 681 | ||
| 682 | ! |
parsed_ggplot2_args <- teal.widgets::parse_ggplot2_args( |
| 683 | ! |
teal.widgets::resolve_ggplot2_args( |
| 684 | ! |
user_plot = ggplot2_args[["Residuals vs Fitted"]], |
| 685 | ! |
user_default = ggplot2_args$default, |
| 686 | ! |
module_plot = teal.widgets::ggplot2_args( |
| 687 | ! |
labs = list( |
| 688 | ! |
x = quote(paste0("Fitted values\nlm(", reg_form, ")")),
|
| 689 | ! |
y = "Residuals", |
| 690 | ! |
title = "Residuals vs Fitted" |
| 691 |
) |
|
| 692 |
) |
|
| 693 |
), |
|
| 694 | ! |
ggtheme = input$ggtheme |
| 695 |
) |
|
| 696 | ||
| 697 | ! |
teal.code::eval_code( |
| 698 | ! |
plot_base, |
| 699 | ! |
substitute( |
| 700 | ! |
expr = {
|
| 701 | ! |
smoothy <- smooth(data$.fitted, data$.resid) |
| 702 | ! |
plot <- graph |
| 703 |
}, |
|
| 704 | ! |
env = list( |
| 705 | ! |
graph = Reduce(function(x, y) call("+", x, y), c(plot, parsed_ggplot2_args))
|
| 706 |
) |
|
| 707 |
) |
|
| 708 |
) |
|
| 709 |
}) |
|
| 710 | ||
| 711 | ! |
output_plot_2 <- reactive({
|
| 712 | ! |
shinyjs::show("size")
|
| 713 | ! |
shinyjs::show("alpha")
|
| 714 | ! |
plot_base <- output_plot_base() |
| 715 | ! |
plot <- substitute( |
| 716 | ! |
expr = ggplot2::ggplot(data = data, ggplot2::aes(sample = .stdresid)) + |
| 717 | ! |
ggplot2::stat_qq(size = size, alpha = alpha) + |
| 718 | ! |
ggplot2::geom_abline(linetype = "dashed"), |
| 719 | ! |
env = list(size = input$size, alpha = input$alpha) |
| 720 |
) |
|
| 721 | ! |
if (input$show_outlier) {
|
| 722 | ! |
plot <- substitute( |
| 723 | ! |
expr = plot + |
| 724 | ! |
ggplot2::stat_qq( |
| 725 | ! |
geom = ggrepel::GeomTextRepel, |
| 726 | ! |
label = label_col %>% |
| 727 | ! |
data.frame(label = .) %>% |
| 728 | ! |
dplyr::filter(label != "cooksd == NaN") %>% |
| 729 | ! |
unlist(), |
| 730 | ! |
color = "red", |
| 731 | ! |
hjust = 0, |
| 732 | ! |
vjust = 0, |
| 733 | ! |
max.overlaps = Inf, |
| 734 | ! |
min.segment.length = label_min_segment, |
| 735 | ! |
segment.alpha = .5, |
| 736 | ! |
seed = 123 |
| 737 |
), |
|
| 738 | ! |
env = list(plot = plot, label_col = label_col(), label_min_segment = label_min_segment()) |
| 739 |
) |
|
| 740 |
} |
|
| 741 | ||
| 742 | ! |
parsed_ggplot2_args <- teal.widgets::parse_ggplot2_args( |
| 743 | ! |
teal.widgets::resolve_ggplot2_args( |
| 744 | ! |
user_plot = ggplot2_args[["Normal Q-Q"]], |
| 745 | ! |
user_default = ggplot2_args$default, |
| 746 | ! |
module_plot = teal.widgets::ggplot2_args( |
| 747 | ! |
labs = list( |
| 748 | ! |
x = quote(paste0("Theoretical Quantiles\nlm(", reg_form, ")")),
|
| 749 | ! |
y = "Standardized residuals", |
| 750 | ! |
title = "Normal Q-Q" |
| 751 |
) |
|
| 752 |
) |
|
| 753 |
), |
|
| 754 | ! |
ggtheme = input$ggtheme |
| 755 |
) |
|
| 756 | ||
| 757 | ! |
teal.code::eval_code( |
| 758 | ! |
plot_base, |
| 759 | ! |
substitute( |
| 760 | ! |
expr = {
|
| 761 | ! |
plot <- graph |
| 762 |
}, |
|
| 763 | ! |
env = list( |
| 764 | ! |
graph = Reduce(function(x, y) call("+", x, y), c(plot, parsed_ggplot2_args))
|
| 765 |
) |
|
| 766 |
) |
|
| 767 |
) |
|
| 768 |
}) |
|
| 769 | ||
| 770 | ! |
output_plot_3 <- reactive({
|
| 771 | ! |
shinyjs::show("size")
|
| 772 | ! |
shinyjs::show("alpha")
|
| 773 | ! |
plot_base <- output_plot_base() |
| 774 | ! |
plot <- substitute( |
| 775 | ! |
expr = ggplot2::ggplot(data = data, ggplot2::aes(.fitted, sqrt(abs(.stdresid)))) + |
| 776 | ! |
ggplot2::geom_point(size = size, alpha = alpha) + |
| 777 | ! |
ggplot2::geom_line(data = smoothy, mapping = smoothy_aes), |
| 778 | ! |
env = list(size = input$size, alpha = input$alpha) |
| 779 |
) |
|
| 780 | ! |
if (input$show_outlier) {
|
| 781 | ! |
plot <- substitute(expr = plot + outlier_label, env = list(plot = plot, outlier_label = outlier_label())) |
| 782 |
} |
|
| 783 | ||
| 784 | ! |
parsed_ggplot2_args <- teal.widgets::parse_ggplot2_args( |
| 785 | ! |
teal.widgets::resolve_ggplot2_args( |
| 786 | ! |
user_plot = ggplot2_args[["Scale-Location"]], |
| 787 | ! |
user_default = ggplot2_args$default, |
| 788 | ! |
module_plot = teal.widgets::ggplot2_args( |
| 789 | ! |
labs = list( |
| 790 | ! |
x = quote(paste0("Fitted values\nlm(", reg_form, ")")),
|
| 791 | ! |
y = quote(expression(sqrt(abs(`Standardized residuals`)))), |
| 792 | ! |
title = "Scale-Location" |
| 793 |
) |
|
| 794 |
) |
|
| 795 |
), |
|
| 796 | ! |
ggtheme = input$ggtheme |
| 797 |
) |
|
| 798 | ||
| 799 | ! |
teal.code::eval_code( |
| 800 | ! |
plot_base, |
| 801 | ! |
substitute( |
| 802 | ! |
expr = {
|
| 803 | ! |
smoothy <- smooth(data$.fitted, sqrt(abs(data$.stdresid))) |
| 804 | ! |
plot <- graph |
| 805 |
}, |
|
| 806 | ! |
env = list( |
| 807 | ! |
graph = Reduce(function(x, y) call("+", x, y), c(plot, parsed_ggplot2_args))
|
| 808 |
) |
|
| 809 |
) |
|
| 810 |
) |
|
| 811 |
}) |
|
| 812 | ||
| 813 | ! |
output_plot_4 <- reactive({
|
| 814 | ! |
shinyjs::hide("size")
|
| 815 | ! |
shinyjs::show("alpha")
|
| 816 | ! |
plot_base <- output_plot_base() |
| 817 | ! |
plot <- substitute( |
| 818 | ! |
expr = ggplot2::ggplot(data = data, ggplot2::aes(seq_along(.cooksd), .cooksd)) + |
| 819 | ! |
ggplot2::geom_col(alpha = alpha), |
| 820 | ! |
env = list(alpha = input$alpha) |
| 821 |
) |
|
| 822 | ! |
if (input$show_outlier) {
|
| 823 | ! |
plot <- substitute( |
| 824 | ! |
expr = plot + |
| 825 | ! |
ggplot2::geom_hline( |
| 826 | ! |
yintercept = c( |
| 827 | ! |
outlier * mean(data$.cooksd, na.rm = TRUE), |
| 828 | ! |
mean(data$.cooksd, na.rm = TRUE) |
| 829 |
), |
|
| 830 | ! |
color = "red", |
| 831 | ! |
linetype = "dashed" |
| 832 |
) + |
|
| 833 | ! |
ggplot2::annotate( |
| 834 | ! |
geom = "text", |
| 835 | ! |
x = 0, |
| 836 | ! |
y = mean(data$.cooksd, na.rm = TRUE), |
| 837 | ! |
label = paste("mu", "=", round(mean(data$.cooksd, na.rm = TRUE), 4)),
|
| 838 | ! |
vjust = -1, |
| 839 | ! |
hjust = 0, |
| 840 | ! |
color = "red", |
| 841 | ! |
angle = 90 |
| 842 |
) + |
|
| 843 | ! |
outlier_label, |
| 844 | ! |
env = list(plot = plot, outlier = input$outlier, outlier_label = outlier_label()) |
| 845 |
) |
|
| 846 |
} |
|
| 847 | ||
| 848 | ! |
parsed_ggplot2_args <- teal.widgets::parse_ggplot2_args( |
| 849 | ! |
teal.widgets::resolve_ggplot2_args( |
| 850 | ! |
user_plot = ggplot2_args[["Cook's distance"]], |
| 851 | ! |
user_default = ggplot2_args$default, |
| 852 | ! |
module_plot = teal.widgets::ggplot2_args( |
| 853 | ! |
labs = list( |
| 854 | ! |
x = quote(paste0("Obs. number\nlm(", reg_form, ")")),
|
| 855 | ! |
y = "Cook's distance", |
| 856 | ! |
title = "Cook's distance" |
| 857 |
) |
|
| 858 |
) |
|
| 859 |
), |
|
| 860 | ! |
ggtheme = input$ggtheme |
| 861 |
) |
|
| 862 | ||
| 863 | ! |
teal.code::eval_code( |
| 864 | ! |
plot_base, |
| 865 | ! |
substitute( |
| 866 | ! |
expr = {
|
| 867 | ! |
plot <- graph |
| 868 |
}, |
|
| 869 | ! |
env = list( |
| 870 | ! |
graph = Reduce(function(x, y) call("+", x, y), c(plot, parsed_ggplot2_args))
|
| 871 |
) |
|
| 872 |
) |
|
| 873 |
) |
|
| 874 |
}) |
|
| 875 | ||
| 876 | ! |
output_plot_5 <- reactive({
|
| 877 | ! |
shinyjs::show("size")
|
| 878 | ! |
shinyjs::show("alpha")
|
| 879 | ! |
plot_base <- output_plot_base() |
| 880 | ! |
plot <- substitute( |
| 881 | ! |
expr = ggplot2::ggplot(data = data, ggplot2::aes(.hat, .stdresid)) + |
| 882 | ! |
ggplot2::geom_vline( |
| 883 | ! |
size = 1, |
| 884 | ! |
colour = "black", |
| 885 | ! |
linetype = "dashed", |
| 886 | ! |
xintercept = 0 |
| 887 |
) + |
|
| 888 | ! |
ggplot2::geom_hline( |
| 889 | ! |
size = 1, |
| 890 | ! |
colour = "black", |
| 891 | ! |
linetype = "dashed", |
| 892 | ! |
yintercept = 0 |
| 893 |
) + |
|
| 894 | ! |
ggplot2::geom_point(size = size, alpha = alpha) + |
| 895 | ! |
ggplot2::geom_line(data = smoothy, mapping = smoothy_aes), |
| 896 | ! |
env = list(size = input$size, alpha = input$alpha) |
| 897 |
) |
|
| 898 | ! |
if (input$show_outlier) {
|
| 899 | ! |
plot <- substitute(expr = plot + outlier_label, env = list(plot = plot, outlier_label = outlier_label())) |
| 900 |
} |
|
| 901 | ||
| 902 | ! |
parsed_ggplot2_args <- teal.widgets::parse_ggplot2_args( |
| 903 | ! |
teal.widgets::resolve_ggplot2_args( |
| 904 | ! |
user_plot = ggplot2_args[["Residuals vs Leverage"]], |
| 905 | ! |
user_default = ggplot2_args$default, |
| 906 | ! |
module_plot = teal.widgets::ggplot2_args( |
| 907 | ! |
labs = list( |
| 908 | ! |
x = quote(paste0("Standardized residuals\nlm(", reg_form, ")")),
|
| 909 | ! |
y = "Leverage", |
| 910 | ! |
title = "Residuals vs Leverage" |
| 911 |
) |
|
| 912 |
) |
|
| 913 |
), |
|
| 914 | ! |
ggtheme = input$ggtheme |
| 915 |
) |
|
| 916 | ||
| 917 | ! |
teal.code::eval_code( |
| 918 | ! |
plot_base, |
| 919 | ! |
substitute( |
| 920 | ! |
expr = {
|
| 921 | ! |
smoothy <- smooth(data$.hat, data$.stdresid) |
| 922 | ! |
plot <- graph |
| 923 |
}, |
|
| 924 | ! |
env = list( |
| 925 | ! |
graph = Reduce(function(x, y) call("+", x, y), c(plot, parsed_ggplot2_args))
|
| 926 |
) |
|
| 927 |
) |
|
| 928 |
) |
|
| 929 |
}) |
|
| 930 | ||
| 931 | ! |
output_plot_6 <- reactive({
|
| 932 | ! |
shinyjs::show("size")
|
| 933 | ! |
shinyjs::show("alpha")
|
| 934 | ! |
plot_base <- output_plot_base() |
| 935 | ! |
plot <- substitute( |
| 936 | ! |
expr = ggplot2::ggplot(data = data, ggplot2::aes(.hat, .cooksd)) + |
| 937 | ! |
ggplot2::geom_vline(xintercept = 0, colour = NA) + |
| 938 | ! |
ggplot2::geom_abline( |
| 939 | ! |
slope = seq(0, 3, by = 0.5), |
| 940 | ! |
colour = "black", |
| 941 | ! |
linetype = "dashed", |
| 942 | ! |
size = 1 |
| 943 |
) + |
|
| 944 | ! |
ggplot2::geom_line(data = smoothy, mapping = smoothy_aes) + |
| 945 | ! |
ggplot2::geom_point(size = size, alpha = alpha), |
| 946 | ! |
env = list(size = input$size, alpha = input$alpha) |
| 947 |
) |
|
| 948 | ! |
if (input$show_outlier) {
|
| 949 | ! |
plot <- substitute(expr = plot + outlier_label, env = list(plot = plot, outlier_label = outlier_label())) |
| 950 |
} |
|
| 951 | ||
| 952 | ! |
parsed_ggplot2_args <- teal.widgets::parse_ggplot2_args( |
| 953 | ! |
teal.widgets::resolve_ggplot2_args( |
| 954 | ! |
user_plot = ggplot2_args[["Cook's dist vs Leverage"]], |
| 955 | ! |
user_default = ggplot2_args$default, |
| 956 | ! |
module_plot = teal.widgets::ggplot2_args( |
| 957 | ! |
labs = list( |
| 958 | ! |
x = quote(paste0("Leverage\nlm(", reg_form, ")")),
|
| 959 | ! |
y = "Cooks's distance", |
| 960 | ! |
title = "Cook's dist vs Leverage" |
| 961 |
) |
|
| 962 |
) |
|
| 963 |
), |
|
| 964 | ! |
ggtheme = input$ggtheme |
| 965 |
) |
|
| 966 | ||
| 967 | ! |
teal.code::eval_code( |
| 968 | ! |
plot_base, |
| 969 | ! |
substitute( |
| 970 | ! |
expr = {
|
| 971 | ! |
smoothy <- smooth(data$.hat, data$.cooksd) |
| 972 | ! |
plot <- graph |
| 973 |
}, |
|
| 974 | ! |
env = list( |
| 975 | ! |
graph = Reduce(function(x, y) call("+", x, y), c(plot, parsed_ggplot2_args))
|
| 976 |
) |
|
| 977 |
) |
|
| 978 |
) |
|
| 979 |
}) |
|
| 980 | ||
| 981 | ! |
output_q <- reactive({
|
| 982 | ! |
teal::validate_inputs(iv_r()) |
| 983 | ! |
switch(input$plot_type, |
| 984 | ! |
"Response vs Regressor" = output_plot_0(), |
| 985 | ! |
"Residuals vs Fitted" = output_plot_1(), |
| 986 | ! |
"Normal Q-Q" = output_plot_2(), |
| 987 | ! |
"Scale-Location" = output_plot_3(), |
| 988 | ! |
"Cook's distance" = output_plot_4(), |
| 989 | ! |
"Residuals vs Leverage" = output_plot_5(), |
| 990 | ! |
"Cook's dist vs Leverage" = output_plot_6() |
| 991 |
) |
|
| 992 |
}) |
|
| 993 | ||
| 994 | ! |
decorated_output_q <- srv_decorate_teal_data( |
| 995 | ! |
"decorator", |
| 996 | ! |
data = output_q, |
| 997 | ! |
decorators = select_decorators(decorators, "plot"), |
| 998 | ! |
expr = plot |
| 999 |
) |
|
| 1000 | ||
| 1001 | ! |
fitted <- reactive({
|
| 1002 | ! |
req(output_q()) |
| 1003 | ! |
decorated_output_q()[["fit"]] |
| 1004 |
}) |
|
| 1005 | ! |
plot_r <- reactive({
|
| 1006 | ! |
req(output_q()) |
| 1007 | ! |
decorated_output_q()[["plot"]] |
| 1008 |
}) |
|
| 1009 | ||
| 1010 |
# Insert the plot into a plot_with_settings module from teal.widgets |
|
| 1011 | ! |
pws <- teal.widgets::plot_with_settings_srv( |
| 1012 | ! |
id = "myplot", |
| 1013 | ! |
plot_r = plot_r, |
| 1014 | ! |
height = plot_height, |
| 1015 | ! |
width = plot_width |
| 1016 |
) |
|
| 1017 | ||
| 1018 | ! |
output$text <- renderText({
|
| 1019 | ! |
req(iv_r()$is_valid()) |
| 1020 | ! |
req(iv_out$is_valid()) |
| 1021 | ! |
paste(utils::capture.output(summary(fitted()))[-1], collapse = "\n") |
| 1022 |
}) |
|
| 1023 | ||
| 1024 |
# Render R code. |
|
| 1025 | ! |
source_code_r <- reactive(teal.code::get_code(req(decorated_output_q()))) |
| 1026 | ||
| 1027 | ! |
teal.widgets::verbatim_popup_srv( |
| 1028 | ! |
id = "rcode", |
| 1029 | ! |
verbatim_content = source_code_r, |
| 1030 | ! |
title = "R code for the regression plot", |
| 1031 |
) |
|
| 1032 | ||
| 1033 |
### REPORTER |
|
| 1034 | ! |
if (with_reporter) {
|
| 1035 | ! |
card_fun <- function(comment, label) {
|
| 1036 | ! |
card <- teal::report_card_template( |
| 1037 | ! |
title = "Linear Regression Plot", |
| 1038 | ! |
label = label, |
| 1039 | ! |
with_filter = with_filter, |
| 1040 | ! |
filter_panel_api = filter_panel_api |
| 1041 |
) |
|
| 1042 | ! |
card$append_text("Plot", "header3")
|
| 1043 | ! |
card$append_plot(plot_r(), dim = pws$dim()) |
| 1044 | ! |
if (!comment == "") {
|
| 1045 | ! |
card$append_text("Comment", "header3")
|
| 1046 | ! |
card$append_text(comment) |
| 1047 |
} |
|
| 1048 | ! |
card$append_src(source_code_r()) |
| 1049 | ! |
card |
| 1050 |
} |
|
| 1051 | ! |
teal.reporter::add_card_button_srv("add_reporter", reporter = reporter, card_fun = card_fun)
|
| 1052 |
} |
|
| 1053 |
### |
|
| 1054 |
}) |
|
| 1055 |
} |
| 1 |
#' `teal` module: Cross-table |
|
| 2 |
#' |
|
| 3 |
#' Generates a simple cross-table of two variables from a dataset with custom |
|
| 4 |
#' options for showing percentages and sub-totals. |
|
| 5 |
#' |
|
| 6 |
#' @inheritParams teal::module |
|
| 7 |
#' @inheritParams shared_params |
|
| 8 |
#' @param x (`data_extract_spec` or `list` of multiple `data_extract_spec`) |
|
| 9 |
#' Object with all available choices with pre-selected option for variable X - row values. |
|
| 10 |
#' In case of `data_extract_spec` use `select_spec(..., ordered = TRUE)` if table elements should be |
|
| 11 |
#' rendered according to selection order. |
|
| 12 |
#' @param y (`data_extract_spec` or `list` of multiple `data_extract_spec`) |
|
| 13 |
#' Object with all available choices with pre-selected option for variable Y - column values. |
|
| 14 |
#' |
|
| 15 |
#' `data_extract_spec` must not allow multiple selection in this case. |
|
| 16 |
#' @param show_percentage (`logical(1)`) |
|
| 17 |
#' Indicates whether to show percentages (relevant only when `x` is a `factor`). |
|
| 18 |
#' Defaults to `TRUE`. |
|
| 19 |
#' @param show_total (`logical(1)`) |
|
| 20 |
#' Indicates whether to show total column. |
|
| 21 |
#' Defaults to `TRUE`. |
|
| 22 |
#' @param remove_zero_columns (`logical(1)`) |
|
| 23 |
#' Indicates whether to remove columns that contain only zeros from the output table. |
|
| 24 |
#' Defaults to `FALSE`. |
|
| 25 |
#' |
|
| 26 |
#' @note For more examples, please see the vignette "Using cross table" via |
|
| 27 |
#' `vignette("using-cross-table", package = "teal.modules.general")`.
|
|
| 28 |
#' |
|
| 29 |
#' @inherit shared_params return |
|
| 30 |
#' |
|
| 31 |
#' @section Table Settings: |
|
| 32 |
#' The module provides several table settings that can be adjusted: |
|
| 33 |
#' \itemize{
|
|
| 34 |
#' \item \code{Show column percentage}: Shows column percentages when enabled
|
|
| 35 |
#' \item \code{Show total column}: Shows a total column when enabled
|
|
| 36 |
#' \item \code{Remove zero-only columns}: Removes columns that contain only zeros from the output table
|
|
| 37 |
#' } |
|
| 38 |
#' |
|
| 39 |
#' @section Decorating Module: |
|
| 40 |
#' |
|
| 41 |
#' This module generates the following objects, which can be modified in place using decorators: |
|
| 42 |
#' - `table` (`ElementaryTable` - output of `rtables::build_table`) |
|
| 43 |
#' |
|
| 44 |
#' A Decorator is applied to the specific output using a named list of `teal_transform_module` objects. |
|
| 45 |
#' The name of this list corresponds to the name of the output to which the decorator is applied. |
|
| 46 |
#' See code snippet below: |
|
| 47 |
#' |
|
| 48 |
#' ``` |
|
| 49 |
#' tm_t_crosstable( |
|
| 50 |
#' ..., # arguments for module |
|
| 51 |
#' decorators = list( |
|
| 52 |
#' table = teal_transform_module(...) # applied to the `table` output |
|
| 53 |
#' ) |
|
| 54 |
#' ) |
|
| 55 |
#' ``` |
|
| 56 |
#' For additional details and examples of decorators, refer to the vignette |
|
| 57 |
#' `vignette("decorate-module-output", package = "teal.modules.general")`.
|
|
| 58 |
#' |
|
| 59 |
#' To learn more please refer to the vignette |
|
| 60 |
#' `vignette("transform-module-output", package = "teal")` or the [`teal::teal_transform_module()`] documentation.
|
|
| 61 |
#' |
|
| 62 |
#' @examplesShinylive |
|
| 63 |
#' library(teal.modules.general) |
|
| 64 |
#' interactive <- function() TRUE |
|
| 65 |
#' {{ next_example }}
|
|
| 66 |
#' @examples |
|
| 67 |
#' # general data example |
|
| 68 |
#' data <- teal_data() |
|
| 69 |
#' data <- within(data, {
|
|
| 70 |
#' mtcars <- mtcars |
|
| 71 |
#' for (v in c("cyl", "vs", "am", "gear")) {
|
|
| 72 |
#' mtcars[[v]] <- as.factor(mtcars[[v]]) |
|
| 73 |
#' } |
|
| 74 |
#' mtcars[["primary_key"]] <- seq_len(nrow(mtcars)) |
|
| 75 |
#' }) |
|
| 76 |
#' join_keys(data) <- join_keys(join_key("mtcars", "mtcars", "primary_key"))
|
|
| 77 |
#' |
|
| 78 |
#' app <- init( |
|
| 79 |
#' data = data, |
|
| 80 |
#' modules = modules( |
|
| 81 |
#' tm_t_crosstable( |
|
| 82 |
#' label = "Cross Table", |
|
| 83 |
#' x = data_extract_spec( |
|
| 84 |
#' dataname = "mtcars", |
|
| 85 |
#' select = select_spec( |
|
| 86 |
#' label = "Select variable:", |
|
| 87 |
#' choices = variable_choices(data[["mtcars"]], c("cyl", "vs", "am", "gear")),
|
|
| 88 |
#' selected = c("cyl", "gear"),
|
|
| 89 |
#' multiple = TRUE, |
|
| 90 |
#' ordered = TRUE, |
|
| 91 |
#' fixed = FALSE |
|
| 92 |
#' ) |
|
| 93 |
#' ), |
|
| 94 |
#' y = data_extract_spec( |
|
| 95 |
#' dataname = "mtcars", |
|
| 96 |
#' select = select_spec( |
|
| 97 |
#' label = "Select variable:", |
|
| 98 |
#' choices = variable_choices(data[["mtcars"]], c("cyl", "vs", "am", "gear")),
|
|
| 99 |
#' selected = "vs", |
|
| 100 |
#' multiple = FALSE, |
|
| 101 |
#' fixed = FALSE |
|
| 102 |
#' ) |
|
| 103 |
#' ) |
|
| 104 |
#' ) |
|
| 105 |
#' ) |
|
| 106 |
#' ) |
|
| 107 |
#' if (interactive()) {
|
|
| 108 |
#' shinyApp(app$ui, app$server) |
|
| 109 |
#' } |
|
| 110 |
#' |
|
| 111 |
#' @examplesShinylive |
|
| 112 |
#' library(teal.modules.general) |
|
| 113 |
#' interactive <- function() TRUE |
|
| 114 |
#' {{ next_example }}
|
|
| 115 |
#' @examples |
|
| 116 |
#' # CDISC data example |
|
| 117 |
#' data <- teal_data() |
|
| 118 |
#' data <- within(data, {
|
|
| 119 |
#' ADSL <- teal.data::rADSL |
|
| 120 |
#' }) |
|
| 121 |
#' join_keys(data) <- default_cdisc_join_keys[names(data)] |
|
| 122 |
#' |
|
| 123 |
#' app <- init( |
|
| 124 |
#' data = data, |
|
| 125 |
#' modules = modules( |
|
| 126 |
#' tm_t_crosstable( |
|
| 127 |
#' label = "Cross Table", |
|
| 128 |
#' x = data_extract_spec( |
|
| 129 |
#' dataname = "ADSL", |
|
| 130 |
#' select = select_spec( |
|
| 131 |
#' label = "Select variable:", |
|
| 132 |
#' choices = variable_choices(data[["ADSL"]], subset = function(data) {
|
|
| 133 |
#' idx <- !vapply(data, inherits, logical(1), c("Date", "POSIXct", "POSIXlt"))
|
|
| 134 |
#' return(names(data)[idx]) |
|
| 135 |
#' }), |
|
| 136 |
#' selected = "COUNTRY", |
|
| 137 |
#' multiple = TRUE, |
|
| 138 |
#' ordered = TRUE, |
|
| 139 |
#' fixed = FALSE |
|
| 140 |
#' ) |
|
| 141 |
#' ), |
|
| 142 |
#' y = data_extract_spec( |
|
| 143 |
#' dataname = "ADSL", |
|
| 144 |
#' select = select_spec( |
|
| 145 |
#' label = "Select variable:", |
|
| 146 |
#' choices = variable_choices(data[["ADSL"]], subset = function(data) {
|
|
| 147 |
#' idx <- vapply(data, is.factor, logical(1)) |
|
| 148 |
#' return(names(data)[idx]) |
|
| 149 |
#' }), |
|
| 150 |
#' selected = "SEX", |
|
| 151 |
#' multiple = FALSE, |
|
| 152 |
#' fixed = FALSE |
|
| 153 |
#' ) |
|
| 154 |
#' ) |
|
| 155 |
#' ) |
|
| 156 |
#' ) |
|
| 157 |
#' ) |
|
| 158 |
#' if (interactive()) {
|
|
| 159 |
#' shinyApp(app$ui, app$server) |
|
| 160 |
#' } |
|
| 161 |
#' |
|
| 162 |
#' @export |
|
| 163 |
#' |
|
| 164 |
tm_t_crosstable <- function(label = "Cross Table", |
|
| 165 |
x, |
|
| 166 |
y, |
|
| 167 |
show_percentage = TRUE, |
|
| 168 |
show_total = TRUE, |
|
| 169 |
remove_zero_columns = FALSE, |
|
| 170 |
pre_output = NULL, |
|
| 171 |
post_output = NULL, |
|
| 172 |
basic_table_args = teal.widgets::basic_table_args(), |
|
| 173 |
transformators = list(), |
|
| 174 |
decorators = list()) {
|
|
| 175 | ! |
message("Initializing tm_t_crosstable")
|
| 176 | ||
| 177 |
# Normalize the parameters |
|
| 178 | ! |
if (inherits(x, "data_extract_spec")) x <- list(x) |
| 179 | ! |
if (inherits(y, "data_extract_spec")) y <- list(y) |
| 180 | ||
| 181 |
# Start of assertions |
|
| 182 | ! |
checkmate::assert_string(label) |
| 183 | ! |
checkmate::assert_list(x, types = "data_extract_spec") |
| 184 | ||
| 185 | ! |
checkmate::assert_list(y, types = "data_extract_spec") |
| 186 | ! |
assert_single_selection(y) |
| 187 | ||
| 188 | ! |
checkmate::assert_flag(show_percentage) |
| 189 | ! |
checkmate::assert_flag(show_total) |
| 190 | ! |
checkmate::assert_flag(remove_zero_columns) |
| 191 | ! |
checkmate::assert_multi_class(pre_output, c("shiny.tag", "shiny.tag.list", "html"), null.ok = TRUE)
|
| 192 | ! |
checkmate::assert_multi_class(post_output, c("shiny.tag", "shiny.tag.list", "html"), null.ok = TRUE)
|
| 193 | ! |
checkmate::assert_class(basic_table_args, classes = "basic_table_args") |
| 194 | ||
| 195 | ! |
assert_decorators(decorators, "table") |
| 196 |
# End of assertions |
|
| 197 | ||
| 198 |
# Make UI args |
|
| 199 | ! |
ui_args <- as.list(environment()) |
| 200 | ||
| 201 | ! |
server_args <- list( |
| 202 | ! |
label = label, |
| 203 | ! |
x = x, |
| 204 | ! |
y = y, |
| 205 | ! |
remove_zero_columns = remove_zero_columns, |
| 206 | ! |
basic_table_args = basic_table_args, |
| 207 | ! |
decorators = decorators |
| 208 |
) |
|
| 209 | ||
| 210 | ! |
ans <- module( |
| 211 | ! |
label = label, |
| 212 | ! |
server = srv_t_crosstable, |
| 213 | ! |
ui = ui_t_crosstable, |
| 214 | ! |
ui_args = ui_args, |
| 215 | ! |
server_args = server_args, |
| 216 | ! |
transformators = transformators, |
| 217 | ! |
datanames = teal.transform::get_extract_datanames(list(x = x, y = y)) |
| 218 |
) |
|
| 219 | ! |
attr(ans, "teal_bookmarkable") <- TRUE |
| 220 | ! |
ans |
| 221 |
} |
|
| 222 | ||
| 223 |
# UI function for the cross-table module |
|
| 224 |
ui_t_crosstable <- function(id, x, y, show_percentage, show_total, remove_zero_columns, pre_output, post_output, ...) {
|
|
| 225 | ! |
args <- list(...) |
| 226 | ! |
ns <- NS(id) |
| 227 | ! |
is_single_dataset <- teal.transform::is_single_dataset(x, y) |
| 228 | ||
| 229 | ! |
join_default_options <- c( |
| 230 | ! |
"Full Join" = "dplyr::full_join", |
| 231 | ! |
"Inner Join" = "dplyr::inner_join", |
| 232 | ! |
"Left Join" = "dplyr::left_join", |
| 233 | ! |
"Right Join" = "dplyr::right_join" |
| 234 |
) |
|
| 235 | ||
| 236 | ! |
teal.widgets::standard_layout( |
| 237 | ! |
output = teal.widgets::white_small_well( |
| 238 | ! |
textOutput(ns("title")),
|
| 239 | ! |
teal.widgets::table_with_settings_ui(ns("table"))
|
| 240 |
), |
|
| 241 | ! |
encoding = tags$div( |
| 242 |
### Reporter |
|
| 243 | ! |
teal.reporter::add_card_button_ui(ns("add_reporter"), label = "Add Report Card"),
|
| 244 | ! |
tags$br(), tags$br(), |
| 245 |
### |
|
| 246 | ! |
tags$label("Encodings", class = "text-primary"),
|
| 247 | ! |
teal.transform::datanames_input(list(x, y)), |
| 248 | ! |
teal.transform::data_extract_ui(ns("x"), label = "Row values", x, is_single_dataset = is_single_dataset),
|
| 249 | ! |
teal.transform::data_extract_ui(ns("y"), label = "Column values", y, is_single_dataset = is_single_dataset),
|
| 250 | ! |
teal.widgets::optionalSelectInput( |
| 251 | ! |
ns("join_fun"),
|
| 252 | ! |
label = "Row to Column type of join", |
| 253 | ! |
choices = join_default_options, |
| 254 | ! |
selected = join_default_options[1], |
| 255 | ! |
multiple = FALSE |
| 256 |
), |
|
| 257 | ! |
tags$hr(), |
| 258 | ! |
bslib::accordion( |
| 259 | ! |
open = TRUE, |
| 260 | ! |
bslib::accordion_panel( |
| 261 | ! |
title = "Table settings", |
| 262 | ! |
checkboxInput(ns("show_percentage"), "Show column percentage", value = show_percentage),
|
| 263 | ! |
checkboxInput(ns("show_total"), "Show total column", value = show_total),
|
| 264 | ! |
checkboxInput(ns("remove_zero_columns"), "Remove zero-only columns", value = remove_zero_columns)
|
| 265 |
) |
|
| 266 |
), |
|
| 267 | ! |
ui_decorate_teal_data(ns("decorator"), decorators = select_decorators(args$decorators, "table"))
|
| 268 |
), |
|
| 269 | ! |
forms = tagList( |
| 270 | ! |
teal.widgets::verbatim_popup_ui(ns("rcode"), "Show R code")
|
| 271 |
), |
|
| 272 | ! |
pre_output = pre_output, |
| 273 | ! |
post_output = post_output |
| 274 |
) |
|
| 275 |
} |
|
| 276 | ||
| 277 |
# Server function for the cross-table module |
|
| 278 |
srv_t_crosstable <- function(id, |
|
| 279 |
data, |
|
| 280 |
reporter, |
|
| 281 |
filter_panel_api, |
|
| 282 |
label, |
|
| 283 |
x, |
|
| 284 |
y, |
|
| 285 |
remove_zero_columns, |
|
| 286 |
basic_table_args, |
|
| 287 |
decorators) {
|
|
| 288 | ! |
with_reporter <- !missing(reporter) && inherits(reporter, "Reporter") |
| 289 | ! |
with_filter <- !missing(filter_panel_api) && inherits(filter_panel_api, "FilterPanelAPI") |
| 290 | ! |
checkmate::assert_class(data, "reactive") |
| 291 | ! |
checkmate::assert_class(isolate(data()), "teal_data") |
| 292 | ! |
moduleServer(id, function(input, output, session) {
|
| 293 | ! |
teal.logger::log_shiny_input_changes(input, namespace = "teal.modules.general") |
| 294 | ||
| 295 | ! |
selector_list <- teal.transform::data_extract_multiple_srv( |
| 296 | ! |
data_extract = list(x = x, y = y), |
| 297 | ! |
datasets = data, |
| 298 | ! |
select_validation_rule = list( |
| 299 | ! |
x = shinyvalidate::sv_required("Please define column for row variable."),
|
| 300 | ! |
y = shinyvalidate::sv_required("Please define column for column variable.")
|
| 301 |
) |
|
| 302 |
) |
|
| 303 | ||
| 304 | ! |
iv_r <- reactive({
|
| 305 | ! |
iv <- shinyvalidate::InputValidator$new() |
| 306 | ! |
iv$add_rule("join_fun", function(value) {
|
| 307 | ! |
if (!identical(selector_list()$x()$dataname, selector_list()$y()$dataname)) {
|
| 308 | ! |
if (!shinyvalidate::input_provided(value)) {
|
| 309 | ! |
"Please select a joining function." |
| 310 |
} |
|
| 311 |
} |
|
| 312 |
}) |
|
| 313 | ! |
teal.transform::compose_and_enable_validators(iv, selector_list) |
| 314 |
}) |
|
| 315 | ||
| 316 | ! |
observeEvent( |
| 317 | ! |
eventExpr = {
|
| 318 | ! |
req(!is.null(selector_list()$x()) && !is.null(selector_list()$y())) |
| 319 | ! |
list(selector_list()$x(), selector_list()$y()) |
| 320 |
}, |
|
| 321 | ! |
handlerExpr = {
|
| 322 | ! |
if (identical(selector_list()$x()$dataname, selector_list()$y()$dataname)) {
|
| 323 | ! |
shinyjs::hide("join_fun")
|
| 324 |
} else {
|
|
| 325 | ! |
shinyjs::show("join_fun")
|
| 326 |
} |
|
| 327 |
} |
|
| 328 |
) |
|
| 329 | ||
| 330 | ! |
merge_function <- reactive({
|
| 331 | ! |
if (is.null(input$join_fun)) {
|
| 332 | ! |
"dplyr::full_join" |
| 333 |
} else {
|
|
| 334 | ! |
input$join_fun |
| 335 |
} |
|
| 336 |
}) |
|
| 337 | ||
| 338 | ! |
anl_merged_input <- teal.transform::merge_expression_srv( |
| 339 | ! |
datasets = data, |
| 340 | ! |
selector_list = selector_list, |
| 341 | ! |
merge_function = merge_function |
| 342 |
) |
|
| 343 | ! |
qenv <- reactive( |
| 344 | ! |
teal.code::eval_code(data(), 'library("rtables");library("tern");library("dplyr")') # nolint quotes
|
| 345 |
) |
|
| 346 | ! |
anl_merged_q <- reactive({
|
| 347 | ! |
req(anl_merged_input()) |
| 348 | ! |
qenv() %>% |
| 349 | ! |
teal.code::eval_code(as.expression(anl_merged_input()$expr)) |
| 350 |
}) |
|
| 351 | ||
| 352 | ! |
merged <- list( |
| 353 | ! |
anl_input_r = anl_merged_input, |
| 354 | ! |
anl_q_r = anl_merged_q |
| 355 |
) |
|
| 356 | ||
| 357 | ! |
output_q <- reactive({
|
| 358 | ! |
teal::validate_inputs(iv_r()) |
| 359 | ! |
ANL <- merged$anl_q_r()[["ANL"]] |
| 360 | ||
| 361 |
# As this is a summary |
|
| 362 | ! |
x_name <- as.vector(merged$anl_input_r()$columns_source$x) |
| 363 | ! |
y_name <- as.vector(merged$anl_input_r()$columns_source$y) |
| 364 | ||
| 365 | ! |
teal::validate_has_data(ANL, 3) |
| 366 | ! |
teal::validate_has_data(ANL[, c(x_name, y_name)], 3, complete = TRUE, allow_inf = FALSE) |
| 367 | ||
| 368 | ! |
is_allowed_class <- function(x) is.numeric(x) || is.factor(x) || is.character(x) || is.logical(x) |
| 369 | ! |
validate(need( |
| 370 | ! |
all(vapply(ANL[x_name], is_allowed_class, logical(1))), |
| 371 | ! |
"Selected row variable has an unsupported data type." |
| 372 |
)) |
|
| 373 | ! |
validate(need( |
| 374 | ! |
is_allowed_class(ANL[[y_name]]), |
| 375 | ! |
"Selected column variable has an unsupported data type." |
| 376 |
)) |
|
| 377 | ||
| 378 | ! |
show_percentage <- input$show_percentage |
| 379 | ! |
show_total <- input$show_total |
| 380 | ! |
remove_zero_columns <- input$remove_zero_columns |
| 381 | ||
| 382 | ! |
plot_title <- paste( |
| 383 | ! |
"Cross-Table of", |
| 384 | ! |
paste0(varname_w_label(x_name, ANL), collapse = ", "), |
| 385 | ! |
"(rows)", "vs.", |
| 386 | ! |
varname_w_label(y_name, ANL), |
| 387 | ! |
"(columns)" |
| 388 |
) |
|
| 389 | ||
| 390 | ! |
labels_vec <- vapply( |
| 391 | ! |
x_name, |
| 392 | ! |
varname_w_label, |
| 393 | ! |
character(1), |
| 394 | ! |
ANL |
| 395 |
) |
|
| 396 | ||
| 397 | ! |
obj <- teal.code::eval_code( |
| 398 | ! |
merged$anl_q_r(), |
| 399 | ! |
substitute( |
| 400 | ! |
expr = {
|
| 401 | ! |
title <- plot_title |
| 402 |
}, |
|
| 403 | ! |
env = list(plot_title = plot_title) |
| 404 |
) |
|
| 405 |
) %>% |
|
| 406 | ! |
teal.code::eval_code( |
| 407 | ! |
substitute( |
| 408 | ! |
expr = {
|
| 409 | ! |
table <- basic_tables %>% |
| 410 | ! |
split_call %>% # styler: off |
| 411 | ! |
rtables::add_colcounts() %>% |
| 412 | ! |
tern::analyze_vars( |
| 413 | ! |
vars = x_name, |
| 414 | ! |
var_labels = labels_vec, |
| 415 | ! |
na.rm = FALSE, |
| 416 | ! |
denom = "N_col", |
| 417 | ! |
.stats = c("mean_sd", "median", "range", count_value)
|
| 418 |
) |
|
| 419 |
}, |
|
| 420 | ! |
env = list( |
| 421 | ! |
basic_tables = teal.widgets::parse_basic_table_args( |
| 422 | ! |
basic_table_args = teal.widgets::resolve_basic_table_args(basic_table_args) |
| 423 |
), |
|
| 424 | ! |
split_call = if (show_total) {
|
| 425 | ! |
substitute( |
| 426 | ! |
expr = rtables::split_cols_by( |
| 427 | ! |
y_name, |
| 428 | ! |
split_fun = rtables::add_overall_level(label = "Total", first = FALSE) |
| 429 |
), |
|
| 430 | ! |
env = list(y_name = y_name) |
| 431 |
) |
|
| 432 |
} else {
|
|
| 433 | ! |
substitute(rtables::split_cols_by(y_name), env = list(y_name = y_name)) |
| 434 |
}, |
|
| 435 | ! |
x_name = x_name, |
| 436 | ! |
labels_vec = labels_vec, |
| 437 | ! |
count_value = ifelse(show_percentage, "count_fraction", "count") |
| 438 |
) |
|
| 439 |
) |
|
| 440 |
) %>% |
|
| 441 | ! |
teal.code::eval_code( |
| 442 | ! |
expression(ANL <- tern::df_explicit_na(ANL)) |
| 443 |
) |
|
| 444 | ||
| 445 | ! |
if (remove_zero_columns) {
|
| 446 | ! |
obj <- obj %>% |
| 447 | ! |
teal.code::eval_code( |
| 448 | ! |
substitute( |
| 449 | ! |
expr = {
|
| 450 | ! |
ANL[[y_name]] <- droplevels(ANL[[y_name]]) |
| 451 | ! |
table <- rtables::build_table(lyt = table, df = ANL[order(ANL[[y_name]]), ]) |
| 452 |
}, |
|
| 453 | ! |
env = list(y_name = y_name) |
| 454 |
) |
|
| 455 |
) |
|
| 456 |
} else {
|
|
| 457 | ! |
obj <- obj %>% |
| 458 | ! |
teal.code::eval_code( |
| 459 | ! |
substitute( |
| 460 | ! |
expr = {
|
| 461 | ! |
table <- rtables::build_table(lyt = table, df = ANL[order(ANL[[y_name]]), ]) |
| 462 |
}, |
|
| 463 | ! |
env = list(y_name = y_name) |
| 464 |
) |
|
| 465 |
) |
|
| 466 |
} |
|
| 467 | ! |
obj |
| 468 |
}) |
|
| 469 | ||
| 470 | ! |
decorated_output_q <- srv_decorate_teal_data( |
| 471 | ! |
id = "decorator", |
| 472 | ! |
data = output_q, |
| 473 | ! |
decorators = select_decorators(decorators, "table"), |
| 474 | ! |
expr = table |
| 475 |
) |
|
| 476 | ||
| 477 | ! |
output$title <- renderText(req(decorated_output_q())[["title"]]) |
| 478 | ||
| 479 | ! |
table_r <- reactive({
|
| 480 | ! |
req(iv_r()$is_valid()) |
| 481 | ! |
req(decorated_output_q())[["table"]] |
| 482 |
}) |
|
| 483 | ||
| 484 | ! |
teal.widgets::table_with_settings_srv( |
| 485 | ! |
id = "table", |
| 486 | ! |
table_r = table_r |
| 487 |
) |
|
| 488 | ||
| 489 |
# Render R code. |
|
| 490 | ! |
source_code_r <- reactive(teal.code::get_code(req(decorated_output_q()))) |
| 491 | ||
| 492 | ! |
teal.widgets::verbatim_popup_srv( |
| 493 | ! |
id = "rcode", |
| 494 | ! |
verbatim_content = source_code_r, |
| 495 | ! |
title = "Show R Code for Cross-Table" |
| 496 |
) |
|
| 497 | ||
| 498 |
### REPORTER |
|
| 499 | ! |
if (with_reporter) {
|
| 500 | ! |
card_fun <- function(comment, label) {
|
| 501 | ! |
card <- teal::report_card_template( |
| 502 | ! |
title = "Cross Table", |
| 503 | ! |
label = label, |
| 504 | ! |
with_filter = with_filter, |
| 505 | ! |
filter_panel_api = filter_panel_api |
| 506 |
) |
|
| 507 | ! |
card$append_text("Table", "header3")
|
| 508 | ! |
card$append_table(table_r()) |
| 509 | ! |
if (!comment == "") {
|
| 510 | ! |
card$append_text("Comment", "header3")
|
| 511 | ! |
card$append_text(comment) |
| 512 |
} |
|
| 513 | ! |
card$append_src(source_code_r()) |
| 514 | ! |
card |
| 515 |
} |
|
| 516 | ! |
teal.reporter::add_card_button_srv("add_reporter", reporter = reporter, card_fun = card_fun)
|
| 517 |
} |
|
| 518 |
### |
|
| 519 |
}) |
|
| 520 |
} |
| 1 |
#' `teal` module: Scatterplot matrix |
|
| 2 |
#' |
|
| 3 |
#' Generates a scatterplot matrix from selected `variables` from datasets. |
|
| 4 |
#' Each plot within the matrix represents the relationship between two variables, |
|
| 5 |
#' providing the overview of correlations and distributions across selected data. |
|
| 6 |
#' |
|
| 7 |
#' @note For more examples, please see the vignette "Using scatterplot matrix" via |
|
| 8 |
#' `vignette("using-scatterplot-matrix", package = "teal.modules.general")`.
|
|
| 9 |
#' |
|
| 10 |
#' @inheritParams teal::module |
|
| 11 |
#' @inheritParams tm_g_scatterplot |
|
| 12 |
#' @inheritParams shared_params |
|
| 13 |
#' |
|
| 14 |
#' @param variables (`data_extract_spec` or `list` of multiple `data_extract_spec`) |
|
| 15 |
#' Specifies plotting variables from an incoming dataset with filtering and selecting. In case of |
|
| 16 |
#' `data_extract_spec` use `select_spec(..., ordered = TRUE)` if plot elements should be |
|
| 17 |
#' rendered according to selection order. |
|
| 18 |
#' |
|
| 19 |
#' @inherit shared_params return |
|
| 20 |
#' |
|
| 21 |
#' @section Decorating Module: |
|
| 22 |
#' |
|
| 23 |
#' This module generates the following objects, which can be modified in place using decorators: |
|
| 24 |
#' - `plot` (`trellis` - output of `lattice::splom`) |
|
| 25 |
#' |
|
| 26 |
#' A Decorator is applied to the specific output using a named list of `teal_transform_module` objects. |
|
| 27 |
#' The name of this list corresponds to the name of the output to which the decorator is applied. |
|
| 28 |
#' See code snippet below: |
|
| 29 |
#' |
|
| 30 |
#' ``` |
|
| 31 |
#' tm_g_scatterplotmatrix( |
|
| 32 |
#' ..., # arguments for module |
|
| 33 |
#' decorators = list( |
|
| 34 |
#' plot = teal_transform_module(...) # applied to the `plot` output |
|
| 35 |
#' ) |
|
| 36 |
#' ) |
|
| 37 |
#' ``` |
|
| 38 |
#' |
|
| 39 |
#' For additional details and examples of decorators, refer to the vignette |
|
| 40 |
#' `vignette("decorate-module-output", package = "teal.modules.general")`.
|
|
| 41 |
#' |
|
| 42 |
#' To learn more please refer to the vignette |
|
| 43 |
#' `vignette("transform-module-output", package = "teal")` or the [`teal::teal_transform_module()`] documentation.
|
|
| 44 |
#' |
|
| 45 |
#' @examplesShinylive |
|
| 46 |
#' library(teal.modules.general) |
|
| 47 |
#' interactive <- function() TRUE |
|
| 48 |
#' {{ next_example }}
|
|
| 49 |
#' @examples |
|
| 50 |
#' # general data example |
|
| 51 |
#' data <- teal_data() |
|
| 52 |
#' data <- within(data, {
|
|
| 53 |
#' countries <- data.frame( |
|
| 54 |
#' id = c("DE", "FR", "IT", "ES", "PT", "GR", "NL", "BE", "LU", "AT"),
|
|
| 55 |
#' government = factor( |
|
| 56 |
#' c(2, 2, 2, 1, 2, 2, 1, 1, 1, 2), |
|
| 57 |
#' labels = c("Monarchy", "Republic")
|
|
| 58 |
#' ), |
|
| 59 |
#' language_family = factor( |
|
| 60 |
#' c(1, 3, 3, 3, 3, 2, 1, 1, 3, 1), |
|
| 61 |
#' labels = c("Germanic", "Hellenic", "Romance")
|
|
| 62 |
#' ), |
|
| 63 |
#' population = c(83, 67, 60, 47, 10, 11, 17, 11, 0.6, 9), |
|
| 64 |
#' area = c(357, 551, 301, 505, 92, 132, 41, 30, 2.6, 83), |
|
| 65 |
#' gdp = c(3.4, 2.7, 2.1, 1.4, 0.3, 0.2, 0.7, 0.5, 0.1, 0.4), |
|
| 66 |
#' debt = c(2.1, 2.3, 2.4, 2.6, 2.3, 2.4, 2.3, 2.4, 2.3, 2.4) |
|
| 67 |
#' ) |
|
| 68 |
#' sales <- data.frame( |
|
| 69 |
#' id = 1:50, |
|
| 70 |
#' country_id = sample( |
|
| 71 |
#' c("DE", "FR", "IT", "ES", "PT", "GR", "NL", "BE", "LU", "AT"),
|
|
| 72 |
#' size = 50, |
|
| 73 |
#' replace = TRUE |
|
| 74 |
#' ), |
|
| 75 |
#' year = sort(sample(2010:2020, 50, replace = TRUE)), |
|
| 76 |
#' venue = sample(c("small", "medium", "large", "online"), 50, replace = TRUE),
|
|
| 77 |
#' cancelled = sample(c(TRUE, FALSE), 50, replace = TRUE), |
|
| 78 |
#' quantity = rnorm(50, 100, 20), |
|
| 79 |
#' costs = rnorm(50, 80, 20), |
|
| 80 |
#' profit = rnorm(50, 20, 10) |
|
| 81 |
#' ) |
|
| 82 |
#' }) |
|
| 83 |
#' join_keys(data) <- join_keys( |
|
| 84 |
#' join_key("countries", "countries", "id"),
|
|
| 85 |
#' join_key("sales", "sales", "id"),
|
|
| 86 |
#' join_key("countries", "sales", c("id" = "country_id"))
|
|
| 87 |
#' ) |
|
| 88 |
#' |
|
| 89 |
#' app <- init( |
|
| 90 |
#' data = data, |
|
| 91 |
#' modules = modules( |
|
| 92 |
#' tm_g_scatterplotmatrix( |
|
| 93 |
#' label = "Scatterplot matrix", |
|
| 94 |
#' variables = list( |
|
| 95 |
#' data_extract_spec( |
|
| 96 |
#' dataname = "countries", |
|
| 97 |
#' select = select_spec( |
|
| 98 |
#' label = "Select variables:", |
|
| 99 |
#' choices = variable_choices(data[["countries"]]), |
|
| 100 |
#' selected = c("area", "gdp", "debt"),
|
|
| 101 |
#' multiple = TRUE, |
|
| 102 |
#' ordered = TRUE, |
|
| 103 |
#' fixed = FALSE |
|
| 104 |
#' ) |
|
| 105 |
#' ), |
|
| 106 |
#' data_extract_spec( |
|
| 107 |
#' dataname = "sales", |
|
| 108 |
#' filter = filter_spec( |
|
| 109 |
#' label = "Select variable:", |
|
| 110 |
#' vars = "country_id", |
|
| 111 |
#' choices = value_choices(data[["sales"]], "country_id"), |
|
| 112 |
#' selected = c("DE", "FR", "IT", "PT", "GR", "NL", "BE", "LU", "AT"),
|
|
| 113 |
#' multiple = TRUE |
|
| 114 |
#' ), |
|
| 115 |
#' select = select_spec( |
|
| 116 |
#' label = "Select variables:", |
|
| 117 |
#' choices = variable_choices(data[["sales"]], c("quantity", "costs", "profit")),
|
|
| 118 |
#' selected = c("quantity", "costs", "profit"),
|
|
| 119 |
#' multiple = TRUE, |
|
| 120 |
#' ordered = TRUE, |
|
| 121 |
#' fixed = FALSE |
|
| 122 |
#' ) |
|
| 123 |
#' ) |
|
| 124 |
#' ) |
|
| 125 |
#' ) |
|
| 126 |
#' ) |
|
| 127 |
#' ) |
|
| 128 |
#' if (interactive()) {
|
|
| 129 |
#' shinyApp(app$ui, app$server) |
|
| 130 |
#' } |
|
| 131 |
#' |
|
| 132 |
#' @examplesShinylive |
|
| 133 |
#' library(teal.modules.general) |
|
| 134 |
#' interactive <- function() TRUE |
|
| 135 |
#' {{ next_example }}
|
|
| 136 |
#' @examples |
|
| 137 |
#' # CDISC data example |
|
| 138 |
#' data <- teal_data() |
|
| 139 |
#' data <- within(data, {
|
|
| 140 |
#' ADSL <- teal.data::rADSL |
|
| 141 |
#' ADRS <- teal.data::rADRS |
|
| 142 |
#' }) |
|
| 143 |
#' join_keys(data) <- default_cdisc_join_keys[names(data)] |
|
| 144 |
#' |
|
| 145 |
#' app <- init( |
|
| 146 |
#' data = data, |
|
| 147 |
#' modules = modules( |
|
| 148 |
#' tm_g_scatterplotmatrix( |
|
| 149 |
#' label = "Scatterplot matrix", |
|
| 150 |
#' variables = list( |
|
| 151 |
#' data_extract_spec( |
|
| 152 |
#' dataname = "ADSL", |
|
| 153 |
#' select = select_spec( |
|
| 154 |
#' label = "Select variables:", |
|
| 155 |
#' choices = variable_choices(data[["ADSL"]]), |
|
| 156 |
#' selected = c("AGE", "RACE", "SEX"),
|
|
| 157 |
#' multiple = TRUE, |
|
| 158 |
#' ordered = TRUE, |
|
| 159 |
#' fixed = FALSE |
|
| 160 |
#' ) |
|
| 161 |
#' ), |
|
| 162 |
#' data_extract_spec( |
|
| 163 |
#' dataname = "ADRS", |
|
| 164 |
#' filter = filter_spec( |
|
| 165 |
#' label = "Select endpoints:", |
|
| 166 |
#' vars = c("PARAMCD", "AVISIT"),
|
|
| 167 |
#' choices = value_choices(data[["ADRS"]], c("PARAMCD", "AVISIT"), c("PARAM", "AVISIT")),
|
|
| 168 |
#' selected = "INVET - END OF INDUCTION", |
|
| 169 |
#' multiple = TRUE |
|
| 170 |
#' ), |
|
| 171 |
#' select = select_spec( |
|
| 172 |
#' label = "Select variables:", |
|
| 173 |
#' choices = variable_choices(data[["ADRS"]]), |
|
| 174 |
#' selected = c("AGE", "AVAL", "ADY"),
|
|
| 175 |
#' multiple = TRUE, |
|
| 176 |
#' ordered = TRUE, |
|
| 177 |
#' fixed = FALSE |
|
| 178 |
#' ) |
|
| 179 |
#' ) |
|
| 180 |
#' ) |
|
| 181 |
#' ) |
|
| 182 |
#' ) |
|
| 183 |
#' ) |
|
| 184 |
#' if (interactive()) {
|
|
| 185 |
#' shinyApp(app$ui, app$server) |
|
| 186 |
#' } |
|
| 187 |
#' |
|
| 188 |
#' @export |
|
| 189 |
#' |
|
| 190 |
tm_g_scatterplotmatrix <- function(label = "Scatterplot Matrix", |
|
| 191 |
variables, |
|
| 192 |
plot_height = c(600, 200, 2000), |
|
| 193 |
plot_width = NULL, |
|
| 194 |
pre_output = NULL, |
|
| 195 |
post_output = NULL, |
|
| 196 |
transformators = list(), |
|
| 197 |
decorators = list()) {
|
|
| 198 | ! |
message("Initializing tm_g_scatterplotmatrix")
|
| 199 | ||
| 200 |
# Normalize the parameters |
|
| 201 | ! |
if (inherits(variables, "data_extract_spec")) variables <- list(variables) |
| 202 | ||
| 203 |
# Start of assertions |
|
| 204 | ! |
checkmate::assert_string(label) |
| 205 | ! |
checkmate::assert_list(variables, types = "data_extract_spec") |
| 206 | ||
| 207 | ! |
checkmate::assert_numeric(plot_height, len = 3, any.missing = FALSE, finite = TRUE) |
| 208 | ! |
checkmate::assert_numeric(plot_height[1], lower = plot_height[2], upper = plot_height[3], .var.name = "plot_height") |
| 209 | ! |
checkmate::assert_numeric(plot_width, len = 3, any.missing = FALSE, null.ok = TRUE, finite = TRUE) |
| 210 | ! |
checkmate::assert_numeric( |
| 211 | ! |
plot_width[1], |
| 212 | ! |
lower = plot_width[2], upper = plot_width[3], null.ok = TRUE, .var.name = "plot_width" |
| 213 |
) |
|
| 214 | ||
| 215 | ! |
checkmate::assert_multi_class(pre_output, c("shiny.tag", "shiny.tag.list", "html"), null.ok = TRUE)
|
| 216 | ! |
checkmate::assert_multi_class(post_output, c("shiny.tag", "shiny.tag.list", "html"), null.ok = TRUE)
|
| 217 | ||
| 218 | ! |
assert_decorators(decorators, "plot") |
| 219 |
# End of assertions |
|
| 220 | ||
| 221 |
# Make UI args |
|
| 222 | ! |
args <- as.list(environment()) |
| 223 | ||
| 224 | ! |
ans <- module( |
| 225 | ! |
label = label, |
| 226 | ! |
server = srv_g_scatterplotmatrix, |
| 227 | ! |
ui = ui_g_scatterplotmatrix, |
| 228 | ! |
ui_args = args, |
| 229 | ! |
server_args = list( |
| 230 | ! |
variables = variables, |
| 231 | ! |
plot_height = plot_height, |
| 232 | ! |
plot_width = plot_width, |
| 233 | ! |
decorators = decorators |
| 234 |
), |
|
| 235 | ! |
transformators = transformators, |
| 236 | ! |
datanames = teal.transform::get_extract_datanames(variables) |
| 237 |
) |
|
| 238 | ! |
attr(ans, "teal_bookmarkable") <- TRUE |
| 239 | ! |
ans |
| 240 |
} |
|
| 241 | ||
| 242 |
# UI function for the scatterplot matrix module |
|
| 243 |
ui_g_scatterplotmatrix <- function(id, ...) {
|
|
| 244 | ! |
args <- list(...) |
| 245 | ! |
is_single_dataset_value <- teal.transform::is_single_dataset(args$variables) |
| 246 | ! |
ns <- NS(id) |
| 247 | ! |
teal.widgets::standard_layout( |
| 248 | ! |
output = teal.widgets::white_small_well( |
| 249 | ! |
textOutput(ns("message")),
|
| 250 | ! |
tags$br(), |
| 251 | ! |
teal.widgets::plot_with_settings_ui(id = ns("myplot"))
|
| 252 |
), |
|
| 253 | ! |
encoding = tags$div( |
| 254 |
### Reporter |
|
| 255 | ! |
teal.reporter::add_card_button_ui(ns("add_reporter"), label = "Add Report Card"),
|
| 256 | ! |
tags$br(), tags$br(), |
| 257 |
### |
|
| 258 | ! |
tags$label("Encodings", class = "text-primary"),
|
| 259 | ! |
teal.transform::datanames_input(args$variables), |
| 260 | ! |
teal.transform::data_extract_ui( |
| 261 | ! |
id = ns("variables"),
|
| 262 | ! |
label = "Variables", |
| 263 | ! |
data_extract_spec = args$variables, |
| 264 | ! |
is_single_dataset = is_single_dataset_value |
| 265 |
), |
|
| 266 | ! |
tags$hr(), |
| 267 | ! |
ui_decorate_teal_data(ns("decorator"), decorators = select_decorators(args$decorators, "plot")),
|
| 268 | ! |
bslib::accordion( |
| 269 | ! |
open = TRUE, |
| 270 | ! |
bslib::accordion_panel( |
| 271 | ! |
title = "Plot settings", |
| 272 | ! |
sliderInput( |
| 273 | ! |
ns("alpha"), "Opacity:",
|
| 274 | ! |
min = 0, max = 1, |
| 275 | ! |
step = .05, value = .5, ticks = FALSE |
| 276 |
), |
|
| 277 | ! |
sliderInput( |
| 278 | ! |
ns("cex"), "Points size:",
|
| 279 | ! |
min = 0.2, max = 3, |
| 280 | ! |
step = .05, value = .65, ticks = FALSE |
| 281 |
), |
|
| 282 | ! |
checkboxInput(ns("cor"), "Add Correlation", value = FALSE),
|
| 283 | ! |
radioButtons( |
| 284 | ! |
ns("cor_method"), "Select Correlation Method",
|
| 285 | ! |
choiceNames = c("Pearson", "Kendall", "Spearman"),
|
| 286 | ! |
choiceValues = c("pearson", "kendall", "spearman"),
|
| 287 | ! |
inline = TRUE |
| 288 |
), |
|
| 289 | ! |
checkboxInput(ns("cor_na_omit"), "Omit Missing Values", value = TRUE)
|
| 290 |
) |
|
| 291 |
) |
|
| 292 |
), |
|
| 293 | ! |
forms = tagList( |
| 294 | ! |
teal.widgets::verbatim_popup_ui(ns("rcode"), "Show R code")
|
| 295 |
), |
|
| 296 | ! |
pre_output = args$pre_output, |
| 297 | ! |
post_output = args$post_output |
| 298 |
) |
|
| 299 |
} |
|
| 300 | ||
| 301 |
# Server function for the scatterplot matrix module |
|
| 302 |
srv_g_scatterplotmatrix <- function(id, |
|
| 303 |
data, |
|
| 304 |
reporter, |
|
| 305 |
filter_panel_api, |
|
| 306 |
variables, |
|
| 307 |
plot_height, |
|
| 308 |
plot_width, |
|
| 309 |
decorators) {
|
|
| 310 | ! |
with_reporter <- !missing(reporter) && inherits(reporter, "Reporter") |
| 311 | ! |
with_filter <- !missing(filter_panel_api) && inherits(filter_panel_api, "FilterPanelAPI") |
| 312 | ! |
checkmate::assert_class(data, "reactive") |
| 313 | ! |
checkmate::assert_class(isolate(data()), "teal_data") |
| 314 | ! |
moduleServer(id, function(input, output, session) {
|
| 315 | ! |
teal.logger::log_shiny_input_changes(input, namespace = "teal.modules.general") |
| 316 | ||
| 317 | ! |
selector_list <- teal.transform::data_extract_multiple_srv( |
| 318 | ! |
data_extract = list(variables = variables), |
| 319 | ! |
datasets = data, |
| 320 | ! |
select_validation_rule = list( |
| 321 | ! |
variables = ~ if (length(.) <= 1) "Please select at least 2 columns." |
| 322 |
) |
|
| 323 |
) |
|
| 324 | ||
| 325 | ! |
iv_r <- reactive({
|
| 326 | ! |
iv <- shinyvalidate::InputValidator$new() |
| 327 | ! |
teal.transform::compose_and_enable_validators(iv, selector_list) |
| 328 |
}) |
|
| 329 | ||
| 330 | ! |
anl_merged_input <- teal.transform::merge_expression_srv( |
| 331 | ! |
datasets = data, |
| 332 | ! |
selector_list = selector_list |
| 333 |
) |
|
| 334 | ||
| 335 | ! |
anl_merged_q <- reactive({
|
| 336 | ! |
req(anl_merged_input()) |
| 337 | ! |
qenv <- teal.code::eval_code(data(), 'library("dplyr");library("lattice")') # nolint quotes
|
| 338 | ! |
teal.code::eval_code(qenv, as.expression(anl_merged_input()$expr)) |
| 339 |
}) |
|
| 340 | ||
| 341 | ! |
merged <- list( |
| 342 | ! |
anl_input_r = anl_merged_input, |
| 343 | ! |
anl_q_r = anl_merged_q |
| 344 |
) |
|
| 345 | ||
| 346 |
# plot |
|
| 347 | ! |
output_q <- reactive({
|
| 348 | ! |
teal::validate_inputs(iv_r()) |
| 349 | ||
| 350 | ! |
qenv <- merged$anl_q_r() |
| 351 | ! |
ANL <- qenv[["ANL"]] |
| 352 | ||
| 353 | ! |
cols_names <- merged$anl_input_r()$columns_source$variables |
| 354 | ! |
alpha <- input$alpha |
| 355 | ! |
cex <- input$cex |
| 356 | ! |
add_cor <- input$cor |
| 357 | ! |
cor_method <- input$cor_method |
| 358 | ! |
cor_na_omit <- input$cor_na_omit |
| 359 | ||
| 360 | ! |
cor_na_action <- if (isTruthy(cor_na_omit)) {
|
| 361 | ! |
"na.omit" |
| 362 |
} else {
|
|
| 363 | ! |
"na.fail" |
| 364 |
} |
|
| 365 | ||
| 366 | ! |
teal::validate_has_data(ANL, 10) |
| 367 | ! |
teal::validate_has_data(ANL[, cols_names, drop = FALSE], 10, complete = TRUE, allow_inf = FALSE) |
| 368 | ||
| 369 |
# get labels and proper variable names |
|
| 370 | ! |
varnames <- varname_w_label(cols_names, ANL, wrap_width = 20) |
| 371 | ||
| 372 |
# check character columns. If any, then those are converted to factors |
|
| 373 | ! |
check_char <- vapply(ANL[, cols_names], is.character, logical(1)) |
| 374 | ! |
qenv <- teal.code::eval_code(qenv, 'library("dplyr")') # nolint quotes
|
| 375 | ! |
if (any(check_char)) {
|
| 376 | ! |
qenv <- teal.code::eval_code( |
| 377 | ! |
qenv, |
| 378 | ! |
substitute( |
| 379 | ! |
expr = ANL <- ANL[, cols_names] %>% |
| 380 | ! |
dplyr::mutate_if(is.character, as.factor) %>% |
| 381 | ! |
droplevels(), |
| 382 | ! |
env = list(cols_names = cols_names) |
| 383 |
) |
|
| 384 |
) |
|
| 385 |
} else {
|
|
| 386 | ! |
qenv <- teal.code::eval_code( |
| 387 | ! |
qenv, |
| 388 | ! |
substitute( |
| 389 | ! |
expr = ANL <- ANL[, cols_names] %>% |
| 390 | ! |
droplevels(), |
| 391 | ! |
env = list(cols_names = cols_names) |
| 392 |
) |
|
| 393 |
) |
|
| 394 |
} |
|
| 395 | ||
| 396 | ||
| 397 |
# create plot |
|
| 398 | ! |
if (add_cor) {
|
| 399 | ! |
shinyjs::show("cor_method")
|
| 400 | ! |
shinyjs::show("cor_use")
|
| 401 | ! |
shinyjs::show("cor_na_omit")
|
| 402 | ||
| 403 | ! |
qenv <- teal.code::eval_code( |
| 404 | ! |
qenv, |
| 405 | ! |
substitute( |
| 406 | ! |
expr = {
|
| 407 | ! |
plot <- lattice::splom( |
| 408 | ! |
ANL, |
| 409 | ! |
varnames = varnames_value, |
| 410 | ! |
panel = function(x, y, ...) {
|
| 411 | ! |
lattice::panel.splom(x = x, y = y, ...) |
| 412 | ! |
cpl <- lattice::current.panel.limits() |
| 413 | ! |
lattice::panel.text( |
| 414 | ! |
mean(cpl$xlim), |
| 415 | ! |
mean(cpl$ylim), |
| 416 | ! |
get_scatterplotmatrix_stats( |
| 417 | ! |
x, |
| 418 | ! |
y, |
| 419 | ! |
.f = stats::cor.test, |
| 420 | ! |
.f_args = list(method = cor_method, na.action = cor_na_action) |
| 421 |
), |
|
| 422 | ! |
alpha = 0.6, |
| 423 | ! |
fontsize = 18, |
| 424 | ! |
fontface = "bold" |
| 425 |
) |
|
| 426 |
}, |
|
| 427 | ! |
pch = 16, |
| 428 | ! |
alpha = alpha_value, |
| 429 | ! |
cex = cex_value |
| 430 |
) |
|
| 431 |
}, |
|
| 432 | ! |
env = list( |
| 433 | ! |
varnames_value = varnames, |
| 434 | ! |
cor_method = cor_method, |
| 435 | ! |
cor_na_action = cor_na_action, |
| 436 | ! |
alpha_value = alpha, |
| 437 | ! |
cex_value = cex |
| 438 |
) |
|
| 439 |
) |
|
| 440 |
) |
|
| 441 |
} else {
|
|
| 442 | ! |
shinyjs::hide("cor_method")
|
| 443 | ! |
shinyjs::hide("cor_use")
|
| 444 | ! |
shinyjs::hide("cor_na_omit")
|
| 445 | ! |
qenv <- teal.code::eval_code( |
| 446 | ! |
qenv, |
| 447 | ! |
substitute( |
| 448 | ! |
expr = {
|
| 449 | ! |
plot <- lattice::splom( |
| 450 | ! |
ANL, |
| 451 | ! |
varnames = varnames_value, |
| 452 | ! |
pch = 16, |
| 453 | ! |
alpha = alpha_value, |
| 454 | ! |
cex = cex_value |
| 455 |
) |
|
| 456 |
}, |
|
| 457 | ! |
env = list(varnames_value = varnames, alpha_value = alpha, cex_value = cex) |
| 458 |
) |
|
| 459 |
) |
|
| 460 |
} |
|
| 461 | ! |
qenv |
| 462 |
}) |
|
| 463 | ||
| 464 | ! |
decorated_output_q <- srv_decorate_teal_data( |
| 465 | ! |
id = "decorator", |
| 466 | ! |
data = output_q, |
| 467 | ! |
decorators = select_decorators(decorators, "plot"), |
| 468 | ! |
expr = plot |
| 469 |
) |
|
| 470 | ||
| 471 | ! |
plot_r <- reactive(req(decorated_output_q())[["plot"]]) |
| 472 | ||
| 473 |
# Insert the plot into a plot_with_settings module |
|
| 474 | ! |
pws <- teal.widgets::plot_with_settings_srv( |
| 475 | ! |
id = "myplot", |
| 476 | ! |
plot_r = plot_r, |
| 477 | ! |
height = plot_height, |
| 478 | ! |
width = plot_width |
| 479 |
) |
|
| 480 | ||
| 481 |
# show a message if conversion to factors took place |
|
| 482 | ! |
output$message <- renderText({
|
| 483 | ! |
req(iv_r()$is_valid()) |
| 484 | ! |
req(selector_list()$variables()) |
| 485 | ! |
ANL <- merged$anl_q_r()[["ANL"]] |
| 486 | ! |
cols_names <- unique(unname(do.call(c, merged$anl_input_r()$columns_source))) |
| 487 | ! |
check_char <- vapply(ANL[, cols_names], is.character, logical(1)) |
| 488 | ! |
if (any(check_char)) {
|
| 489 | ! |
is_single <- sum(check_char) == 1 |
| 490 | ! |
paste( |
| 491 | ! |
"Character", |
| 492 | ! |
ifelse(is_single, "variable", "variables"), |
| 493 | ! |
paste0("(", paste(cols_names[check_char], collapse = ", "), ")"),
|
| 494 | ! |
ifelse(is_single, "was", "were"), |
| 495 | ! |
"converted to", |
| 496 | ! |
ifelse(is_single, "factor.", "factors.") |
| 497 |
) |
|
| 498 |
} else {
|
|
| 499 |
"" |
|
| 500 |
} |
|
| 501 |
}) |
|
| 502 | ||
| 503 |
# Render R code. |
|
| 504 | ! |
source_code_r <- reactive(teal.code::get_code(req(decorated_output_q()))) |
| 505 | ||
| 506 | ! |
teal.widgets::verbatim_popup_srv( |
| 507 | ! |
id = "rcode", |
| 508 | ! |
verbatim_content = source_code_r, |
| 509 | ! |
title = "Show R Code for Scatterplotmatrix" |
| 510 |
) |
|
| 511 | ||
| 512 |
### REPORTER |
|
| 513 | ! |
if (with_reporter) {
|
| 514 | ! |
card_fun <- function(comment, label) {
|
| 515 | ! |
card <- teal::report_card_template( |
| 516 | ! |
title = "Scatter Plot Matrix", |
| 517 | ! |
label = label, |
| 518 | ! |
with_filter = with_filter, |
| 519 | ! |
filter_panel_api = filter_panel_api |
| 520 |
) |
|
| 521 | ! |
card$append_text("Plot", "header3")
|
| 522 | ! |
card$append_plot(plot_r(), dim = pws$dim()) |
| 523 | ! |
if (!comment == "") {
|
| 524 | ! |
card$append_text("Comment", "header3")
|
| 525 | ! |
card$append_text(comment) |
| 526 |
} |
|
| 527 | ! |
card$append_src(source_code_r()) |
| 528 | ! |
card |
| 529 |
} |
|
| 530 | ! |
teal.reporter::add_card_button_srv("add_reporter", reporter = reporter, card_fun = card_fun)
|
| 531 |
} |
|
| 532 |
### |
|
| 533 |
}) |
|
| 534 |
} |
|
| 535 | ||
| 536 |
#' Get stats for x-y pairs in scatterplot matrix |
|
| 537 |
#' |
|
| 538 |
#' Uses [stats::cor.test()] per default for all numerical input variables and converts results |
|
| 539 |
#' to character vector. |
|
| 540 |
#' Could be extended if different stats for different variable types are needed. |
|
| 541 |
#' Meant to be called from [lattice::panel.text()]. |
|
| 542 |
#' |
|
| 543 |
#' Presently we need to use a formula input for `stats::cor.test` because |
|
| 544 |
#' `na.fail` only gets evaluated when a formula is passed (see below). |
|
| 545 |
#' ``` |
|
| 546 |
#' x = c(1,3,5,7,NA) |
|
| 547 |
#' y = c(3,6,7,8,1) |
|
| 548 |
#' stats::cor.test(x, y, na.action = "na.fail") |
|
| 549 |
#' stats::cor.test(~ x + y, na.action = "na.fail") |
|
| 550 |
#' ``` |
|
| 551 |
#' |
|
| 552 |
#' @param x,y (`numeric`) vectors of data values. `x` and `y` must have the same length. |
|
| 553 |
#' @param .f (`function`) function that accepts x and y as formula input `~ x + y`. |
|
| 554 |
#' Default `stats::cor.test`. |
|
| 555 |
#' @param .f_args (`list`) of arguments to be passed to `.f`. |
|
| 556 |
#' @param round_stat (`integer(1)`) optional, number of decimal places to use when rounding the estimate. |
|
| 557 |
#' @param round_pval (`integer(1)`) optional, number of decimal places to use when rounding the p-value. |
|
| 558 |
#' |
|
| 559 |
#' @return Character with stats. For [stats::cor.test()] correlation coefficient and p-value. |
|
| 560 |
#' |
|
| 561 |
#' @examples |
|
| 562 |
#' set.seed(1) |
|
| 563 |
#' x <- runif(25, 0, 1) |
|
| 564 |
#' y <- runif(25, 0, 1) |
|
| 565 |
#' x[c(3, 10, 18)] <- NA |
|
| 566 |
#' |
|
| 567 |
#' get_scatterplotmatrix_stats(x, y, .f = stats::cor.test, .f_args = list(method = "pearson")) |
|
| 568 |
#' get_scatterplotmatrix_stats(x, y, .f = stats::cor.test, .f_args = list( |
|
| 569 |
#' method = "pearson", |
|
| 570 |
#' na.action = na.fail |
|
| 571 |
#' )) |
|
| 572 |
#' |
|
| 573 |
#' @export |
|
| 574 |
#' |
|
| 575 |
get_scatterplotmatrix_stats <- function(x, y, |
|
| 576 |
.f = stats::cor.test, |
|
| 577 |
.f_args = list(), |
|
| 578 |
round_stat = 2, |
|
| 579 |
round_pval = 4) {
|
|
| 580 | 6x |
if (is.numeric(x) && is.numeric(y)) {
|
| 581 | 3x |
stat <- tryCatch(do.call(.f, c(list(~ x + y), .f_args)), error = function(e) NA) |
| 582 | ||
| 583 | 3x |
if (anyNA(stat)) {
|
| 584 | 1x |
return("NA")
|
| 585 | 2x |
} else if (all(c("estimate", "p.value") %in% names(stat))) {
|
| 586 | 2x |
return(paste( |
| 587 | 2x |
c( |
| 588 | 2x |
paste0(names(stat$estimate), ":", round(stat$estimate, round_stat)), |
| 589 | 2x |
paste0("P:", round(stat$p.value, round_pval))
|
| 590 |
), |
|
| 591 | 2x |
collapse = "\n" |
| 592 |
)) |
|
| 593 |
} else {
|
|
| 594 | ! |
stop("function not supported")
|
| 595 |
} |
|
| 596 |
} else {
|
|
| 597 | 3x |
if ("method" %in% names(.f_args)) {
|
| 598 | 3x |
if (.f_args$method == "pearson") {
|
| 599 | 1x |
return("cor:-")
|
| 600 |
} |
|
| 601 | 2x |
if (.f_args$method == "kendall") {
|
| 602 | 1x |
return("tau:-")
|
| 603 |
} |
|
| 604 | 1x |
if (.f_args$method == "spearman") {
|
| 605 | 1x |
return("rho:-")
|
| 606 |
} |
|
| 607 |
} |
|
| 608 | ! |
return("-")
|
| 609 |
} |
|
| 610 |
} |
| 1 |
#' `teal` module: Front page |
|
| 2 |
#' |
|
| 3 |
#' Creates a simple front page for `teal` applications, displaying |
|
| 4 |
#' introductory text, tables, additional `html` or `shiny` tags, and footnotes. |
|
| 5 |
#' |
|
| 6 |
#' @inheritParams teal::module |
|
| 7 |
#' @param header_text (`character` vector) text to be shown at the top of the module, for each |
|
| 8 |
#' element, if named the name is shown first in bold as a header followed by the value. The first |
|
| 9 |
#' element's header is displayed larger than the others. |
|
| 10 |
#' @param tables (`named list` of `data.frame`s) tables to be shown in the module. |
|
| 11 |
#' @param additional_tags (`shiny.tag.list` or `html`) additional `shiny` tags or `html` to be included after the table, |
|
| 12 |
#' for example to include an image, `tagList(tags$img(src = "image.png"))` or to include further `html`, |
|
| 13 |
#' `HTML("html text here")`.
|
|
| 14 |
#' @param footnotes (`character` vector) of text to be shown at the bottom of the module, for each |
|
| 15 |
#' element, if named the name is shown first in bold, followed by the value. |
|
| 16 |
#' @param show_metadata (`logical`) `r lifecycle::badge("deprecated")` indicating
|
|
| 17 |
#' whether the metadata of the datasets be available on the module. |
|
| 18 |
#' Metadata shown automatically when `datanames` set. |
|
| 19 |
#' @inheritParams tm_variable_browser |
|
| 20 |
#' |
|
| 21 |
#' @inherit shared_params return |
|
| 22 |
#' |
|
| 23 |
#' @examplesShinylive |
|
| 24 |
#' library(teal.modules.general) |
|
| 25 |
#' interactive <- function() TRUE |
|
| 26 |
#' {{ next_example }}
|
|
| 27 |
#' @examples |
|
| 28 |
#' data <- teal_data() |
|
| 29 |
#' data <- within(data, {
|
|
| 30 |
#' require(nestcolor) |
|
| 31 |
#' ADSL <- teal.data::rADSL |
|
| 32 |
#' attr(ADSL, "metadata") <- list("Author" = "NEST team", "data_source" = "synthetic data")
|
|
| 33 |
#' }) |
|
| 34 |
#' join_keys(data) <- default_cdisc_join_keys[names(data)] |
|
| 35 |
#' |
|
| 36 |
#' table_1 <- data.frame(Info = c("A", "B"), Text = c("A", "B"))
|
|
| 37 |
#' table_2 <- data.frame(`Column 1` = c("C", "D"), `Column 2` = c(5.5, 6.6), `Column 3` = c("A", "B"))
|
|
| 38 |
#' table_3 <- data.frame(Info = c("E", "F"), Text = c("G", "H"))
|
|
| 39 |
#' |
|
| 40 |
#' table_input <- list( |
|
| 41 |
#' "Table 1" = table_1, |
|
| 42 |
#' "Table 2" = table_2, |
|
| 43 |
#' "Table 3" = table_3 |
|
| 44 |
#' ) |
|
| 45 |
#' |
|
| 46 |
#' app <- init( |
|
| 47 |
#' data = data, |
|
| 48 |
#' modules = modules( |
|
| 49 |
#' tm_front_page( |
|
| 50 |
#' header_text = c( |
|
| 51 |
#' "Important information" = "It can go here.", |
|
| 52 |
#' "Other information" = "Can go here." |
|
| 53 |
#' ), |
|
| 54 |
#' tables = table_input, |
|
| 55 |
#' additional_tags = HTML("Additional HTML or shiny tags go here <br>"),
|
|
| 56 |
#' footnotes = c("X" = "is the first footnote", "Y is the second footnote")
|
|
| 57 |
#' ) |
|
| 58 |
#' ) |
|
| 59 |
#' ) |> |
|
| 60 |
#' modify_header(tags$h1("Sample Application")) |>
|
|
| 61 |
#' modify_footer(tags$p("Application footer"))
|
|
| 62 |
#' |
|
| 63 |
#' if (interactive()) {
|
|
| 64 |
#' shinyApp(app$ui, app$server) |
|
| 65 |
#' } |
|
| 66 |
#' |
|
| 67 |
#' @export |
|
| 68 |
#' |
|
| 69 |
tm_front_page <- function(label = "Front page", |
|
| 70 |
header_text = character(0), |
|
| 71 |
tables = list(), |
|
| 72 |
additional_tags = tagList(), |
|
| 73 |
footnotes = character(0), |
|
| 74 |
show_metadata = deprecated(), |
|
| 75 |
datanames = if (missing(show_metadata)) NULL else "all", |
|
| 76 |
transformators = list()) {
|
|
| 77 | ! |
message("Initializing tm_front_page")
|
| 78 | ||
| 79 |
# Start of assertions |
|
| 80 | ! |
checkmate::assert_string(label) |
| 81 | ! |
checkmate::assert_character(header_text, min.len = 0, any.missing = FALSE) |
| 82 | ! |
checkmate::assert_list(tables, types = "data.frame", names = "named", any.missing = FALSE) |
| 83 | ! |
checkmate::assert_multi_class(additional_tags, classes = c("shiny.tag.list", "html"))
|
| 84 | ! |
checkmate::assert_character(footnotes, min.len = 0, any.missing = FALSE) |
| 85 | ! |
if (!missing(show_metadata)) {
|
| 86 | ! |
lifecycle::deprecate_stop( |
| 87 | ! |
when = "0.4.0", |
| 88 | ! |
what = "tm_front_page(show_metadata)", |
| 89 | ! |
with = "tm_front_page(datanames)", |
| 90 | ! |
details = c( |
| 91 | ! |
"With `datanames` you can select which datasets are displayed.", |
| 92 | ! |
i = "Use `tm_front_page(datanames = 'all')` to keep the previous behavior and avoid this warning." |
| 93 |
) |
|
| 94 |
) |
|
| 95 |
} |
|
| 96 | ! |
checkmate::assert_character(datanames, min.len = 0, min.chars = 1, null.ok = TRUE) |
| 97 | ||
| 98 |
# End of assertions |
|
| 99 | ||
| 100 |
# Make UI args |
|
| 101 | ! |
args <- as.list(environment()) |
| 102 | ||
| 103 | ! |
ans <- module( |
| 104 | ! |
label = label, |
| 105 | ! |
server = srv_front_page, |
| 106 | ! |
ui = ui_front_page, |
| 107 | ! |
ui_args = args, |
| 108 | ! |
server_args = list(tables = tables), |
| 109 | ! |
datanames = datanames, , |
| 110 | ! |
transformators = transformators |
| 111 |
) |
|
| 112 | ! |
attr(ans, "teal_bookmarkable") <- TRUE |
| 113 | ! |
ans |
| 114 |
} |
|
| 115 | ||
| 116 |
# UI function for the front page module |
|
| 117 |
ui_front_page <- function(id, ...) {
|
|
| 118 | ! |
args <- list(...) |
| 119 | ! |
ns <- NS(id) |
| 120 | ||
| 121 | ! |
tagList( |
| 122 | ! |
tags$div( |
| 123 | ! |
id = "front_page_content", |
| 124 | ! |
style = "margin-left: 2rem;", |
| 125 | ! |
tags$div( |
| 126 | ! |
id = "front_page_headers", |
| 127 | ! |
get_header_tags(args$header_text) |
| 128 |
), |
|
| 129 | ! |
tags$div( |
| 130 | ! |
id = "front_page_tables", |
| 131 | ! |
style = "margin-left: 2rem;", |
| 132 | ! |
get_table_tags(args$tables, ns) |
| 133 |
), |
|
| 134 | ! |
tags$div( |
| 135 | ! |
id = "front_page_custom_html", |
| 136 | ! |
style = "margin-left: 2rem;", |
| 137 | ! |
args$additional_tags |
| 138 |
), |
|
| 139 | ! |
if (length(args$datanames) > 0L) {
|
| 140 | ! |
tags$div( |
| 141 | ! |
id = "front_page_metabutton", |
| 142 | ! |
style = "margin: 1rem;", |
| 143 | ! |
actionButton(ns("metadata_button"), "Show metadata")
|
| 144 |
) |
|
| 145 |
}, |
|
| 146 | ! |
tags$footer( |
| 147 | ! |
class = "small", |
| 148 | ! |
get_footer_tags(args$footnotes) |
| 149 |
) |
|
| 150 |
) |
|
| 151 |
) |
|
| 152 |
} |
|
| 153 | ||
| 154 |
# Server function for the front page module |
|
| 155 |
srv_front_page <- function(id, data, tables) {
|
|
| 156 | ! |
checkmate::assert_class(data, "reactive") |
| 157 | ! |
checkmate::assert_class(isolate(data()), "teal_data") |
| 158 | ! |
moduleServer(id, function(input, output, session) {
|
| 159 | ! |
teal.logger::log_shiny_input_changes(input, namespace = "teal.modules.general") |
| 160 | ||
| 161 | ! |
ns <- session$ns |
| 162 | ||
| 163 | ! |
setBookmarkExclude("metadata_button")
|
| 164 | ||
| 165 | ! |
lapply(seq_along(tables), function(idx) {
|
| 166 | ! |
output[[paste0("table_", idx)]] <- renderTable(
|
| 167 | ! |
tables[[idx]], |
| 168 | ! |
bordered = TRUE, |
| 169 | ! |
caption = names(tables)[idx], |
| 170 | ! |
caption.placement = "top" |
| 171 |
) |
|
| 172 |
}) |
|
| 173 | ! |
if (length(isolate(names(data()))) > 0L) {
|
| 174 | ! |
observeEvent( |
| 175 | ! |
input$metadata_button, showModal( |
| 176 | ! |
modalDialog( |
| 177 | ! |
title = "Metadata", |
| 178 | ! |
dataTableOutput(ns("metadata_table")),
|
| 179 | ! |
size = "l", |
| 180 | ! |
easyClose = TRUE |
| 181 |
) |
|
| 182 |
) |
|
| 183 |
) |
|
| 184 | ||
| 185 | ! |
metadata_data_frame <- reactive({
|
| 186 | ! |
datanames <- names(data()) |
| 187 | ! |
convert_metadata_to_dataframe( |
| 188 | ! |
lapply(datanames, function(dataname) attr(data()[[dataname]], "metadata")), |
| 189 | ! |
datanames |
| 190 |
) |
|
| 191 |
}) |
|
| 192 | ||
| 193 | ! |
output$metadata_table <- renderDataTable({
|
| 194 | ! |
validate(need(nrow(metadata_data_frame()) > 0, "The data has no associated metadata")) |
| 195 | ! |
metadata_data_frame() |
| 196 |
}) |
|
| 197 |
} |
|
| 198 |
}) |
|
| 199 |
} |
|
| 200 | ||
| 201 |
## utils functions |
|
| 202 | ||
| 203 |
get_header_tags <- function(header_text) {
|
|
| 204 | ! |
if (length(header_text) == 0) {
|
| 205 | ! |
return(list()) |
| 206 |
} |
|
| 207 | ||
| 208 | ! |
get_single_header_tags <- function(header_text, p_text, header_tag = tags$h4) {
|
| 209 | ! |
tagList( |
| 210 | ! |
tags$div( |
| 211 | ! |
if (!is.null(header_text) && nchar(header_text) > 0) header_tag(header_text), |
| 212 | ! |
tags$p(p_text) |
| 213 |
) |
|
| 214 |
) |
|
| 215 |
} |
|
| 216 | ||
| 217 | ! |
header_tags <- get_single_header_tags(names(header_text[1]), header_text[1], header_tag = tags$h3) |
| 218 | ! |
c(header_tags, mapply(get_single_header_tags, utils::tail(names(header_text), -1), utils::tail(header_text, -1))) |
| 219 |
} |
|
| 220 | ||
| 221 |
get_table_tags <- function(tables, ns) {
|
|
| 222 | ! |
if (length(tables) == 0) {
|
| 223 | ! |
return(list()) |
| 224 |
} |
|
| 225 | ! |
table_tags <- c(lapply(seq_along(tables), function(idx) {
|
| 226 | ! |
list( |
| 227 | ! |
tableOutput(ns(paste0("table_", idx)))
|
| 228 |
) |
|
| 229 |
})) |
|
| 230 | ! |
table_tags |
| 231 |
} |
|
| 232 | ||
| 233 |
get_footer_tags <- function(footnotes) {
|
|
| 234 | ! |
if (length(footnotes) == 0) {
|
| 235 | ! |
return(list()) |
| 236 |
} |
|
| 237 | ! |
bold_texts <- if (is.null(names(footnotes))) rep("", length(footnotes)) else names(footnotes)
|
| 238 | ! |
footnote_tags <- mapply(function(bold_text, value) {
|
| 239 | ! |
list( |
| 240 | ! |
tags$div( |
| 241 | ! |
tags$b(bold_text), |
| 242 | ! |
value, |
| 243 | ! |
tags$br() |
| 244 |
) |
|
| 245 |
) |
|
| 246 | ! |
}, bold_text = bold_texts, value = footnotes) |
| 247 |
} |
|
| 248 | ||
| 249 |
# take a list of metadata, one item per dataset (raw_metadata each element from datasets$get_metadata()) |
|
| 250 |
# and the corresponding datanames and output a data.frame with columns {Dataset, Name, Value}.
|
|
| 251 |
# which are, the Dataset the metadata came from, the metadata's name and value |
|
| 252 |
convert_metadata_to_dataframe <- function(raw_metadata, datanames) {
|
|
| 253 | 4x |
output <- mapply(function(metadata, dataname) {
|
| 254 | 6x |
if (is.null(metadata)) {
|
| 255 | 2x |
return(data.frame(Dataset = character(0), Name = character(0), Value = character(0))) |
| 256 |
} |
|
| 257 | 4x |
data.frame( |
| 258 | 4x |
Dataset = dataname, |
| 259 | 4x |
Name = names(metadata), |
| 260 | 4x |
Value = unname(unlist(lapply(metadata, as.character))) |
| 261 |
) |
|
| 262 | 4x |
}, raw_metadata, datanames, SIMPLIFY = FALSE) |
| 263 | 4x |
do.call(rbind, output) |
| 264 |
} |
| 1 |
.onLoad <- function(libname, pkgname) {
|
|
| 2 | ! |
teal.logger::register_logger(namespace = "teal.modules.general") |
| 3 | ! |
teal.logger::register_handlers("teal.modules.general")
|
| 4 |
} |
|
| 5 | ||
| 6 |
### global variables |
|
| 7 |
ggplot_themes <- c("gray", "bw", "linedraw", "light", "dark", "minimal", "classic", "void")
|
|
| 8 | ||
| 9 |
#' @importFrom lifecycle deprecated |
|
| 10 |
interactive <- NULL |