Skip to content

Commit 9ec4381

Browse files
committed
feat: tm_g_distribution
1 parent f9c1d7b commit 9ec4381

File tree

1 file changed

+86
-52
lines changed

1 file changed

+86
-52
lines changed

R/tm_g_distribution.R

Lines changed: 86 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -30,9 +30,10 @@
3030
#' @section Decorating `tm_outliers`:
3131
#'
3232
#' This module generates the following objects, which can be modified in place using decorators::
33-
#' - `plot` (`ggplot2`)
34-
#' - `test_table` (`data.frame`)
33+
#' - `histogram_plot` (`ggplot2`)
34+
#' - `qq_plot` (`data.frame`)
3535
#' - `summary_table` (`data.frame`)
36+
#' - `test_table` (`data.frame`)
3637
#'
3738
#' Decorators can be applied to all outputs or only to specific objects using a
3839
#' named list of `teal_transform_module` objects.
@@ -44,9 +45,10 @@
4445
#' ..., # arguments for module
4546
#' decorators = list(
4647
#' default = list(teal_transform_module(...)), # applied to all outputs
47-
#' plot = list(teal_transform_module(...)), # applied only to `plot` output (histogram plot)
48-
#' test_table = list(teal_transform_module(...)) # applied only to `test_table` output
48+
#' histogram_plot = list(teal_transform_module(...)), # applied only to `histogram_plot` output (histogram plot)
49+
#' qq_plot = list(teal_transform_module(...)) # applied only to `qq_plot` output
4950
#' summary_table = list(teal_transform_module(...)) # applied only to `summary_table` output
51+
#' test_table = list(teal_transform_module(...)) # applied only to `test_table` output
5052
#' )
5153
#' )
5254
#' ```
@@ -201,7 +203,7 @@ tm_g_distribution <- function(label = "Distribution Module",
201203
checkmate::assert_multi_class(pre_output, c("shiny.tag", "shiny.tag.list", "html"), null.ok = TRUE)
202204
checkmate::assert_multi_class(post_output, c("shiny.tag", "shiny.tag.list", "html"), null.ok = TRUE)
203205

204-
available_decorators <- c("plot", "test_table", "summary_table")
206+
available_decorators <- c("histogram_plot", "qq_plot", "test_table", "summary_table")
205207
decorators <- normalize_decorators(decorators, available_decorators)
206208
assert_decorators(decorators, null.ok = TRUE, names = available_decorators)
207209

@@ -300,7 +302,10 @@ ui_distribution <- function(id, ...) {
300302
inline = TRUE
301303
),
302304
checkboxInput(ns("add_dens"), label = "Overlay Density", value = TRUE),
303-
ui_transform_teal_data(ns("d_dist"), transformators = args$decorators),
305+
ui_decorate_teal_data(
306+
ns("d_density"),
307+
decorators = subset_decorators("histogram_plot", args$decorators)
308+
),
304309
collapsed = FALSE
305310
)
306311
),
@@ -309,10 +314,21 @@ ui_distribution <- function(id, ...) {
309314
teal.widgets::panel_item(
310315
"QQ Plot",
311316
checkboxInput(ns("qq_line"), label = "Add diagonal line(s)", TRUE),
312-
ui_transform_teal_data(ns("d_qq"), transformators = args$decorators),
317+
ui_decorate_teal_data(
318+
ns("d_qq"),
319+
decorators = subset_decorators("qq_plot", args$decorators)
320+
),
313321
collapsed = FALSE
314322
)
315323
),
324+
ui_decorate_teal_data(
325+
ns("d_summary"),
326+
decorators = subset_decorators("summary_table", args$decorators)
327+
),
328+
ui_decorate_teal_data(
329+
ns("d_test"),
330+
decorators = subset_decorators("test_table", args$decorators)
331+
),
316332
conditionalPanel(
317333
condition = paste0("input['", ns("main_type"), "'] == 'Density'"),
318334
teal.widgets::panel_item(
@@ -697,12 +713,12 @@ srv_distribution <- function(id,
697713
)
698714
}
699715

700-
if (length(s_var) == 0 && length(g_var) == 0) {
701-
qenv <- teal.code::eval_code(
716+
qenv <- if (length(s_var) == 0 && length(g_var) == 0) {
717+
teal.code::eval_code(
702718
qenv,
703719
substitute(
704720
expr = {
705-
summary_table <- ANL %>%
721+
summary_table_data <- ANL %>%
706722
dplyr::summarise(
707723
min = round(min(dist_var_name, na.rm = TRUE), roundn),
708724
median = round(stats::median(dist_var_name, na.rm = TRUE), roundn),
@@ -719,12 +735,12 @@ srv_distribution <- function(id,
719735
)
720736
)
721737
} else {
722-
qenv <- teal.code::eval_code(
738+
teal.code::eval_code(
723739
qenv,
724740
substitute(
725741
expr = {
726742
strata_vars <- strata_vars_raw
727-
summary_table <- ANL %>%
743+
summary_table_data <- ANL %>%
728744
dplyr::group_by_at(dplyr::vars(dplyr::any_of(strata_vars))) %>%
729745
dplyr::summarise(
730746
min = round(min(dist_var_name, na.rm = TRUE), roundn),
@@ -734,7 +750,6 @@ srv_distribution <- function(id,
734750
sd = round(stats::sd(dist_var_name, na.rm = TRUE), roundn),
735751
count = dplyr::n()
736752
)
737-
summary_table # used to display table when running show-r-code code
738753
},
739754
env = list(
740755
dist_var_name = dist_var_name,
@@ -744,6 +759,20 @@ srv_distribution <- function(id,
744759
)
745760
)
746761
}
762+
if (iv_r()$is_valid()) {
763+
within(qenv, {
764+
summary_table <- DT::datatable(
765+
summary_table_data,
766+
options = list(
767+
autoWidth = TRUE,
768+
columnDefs = list(list(width = "200px", targets = "_all"))
769+
),
770+
rownames = FALSE
771+
)
772+
})
773+
} else {
774+
within(qenv, summary_table <- NULL)
775+
}
747776
})
748777

749778
# distplot qenv ----
@@ -933,7 +962,7 @@ srv_distribution <- function(id,
933962
teal.code::eval_code(
934963
qenv,
935964
substitute(
936-
expr = plot <- plot_call,
965+
expr = histogram_plot <- plot_call,
937966
env = list(plot_call = Reduce(function(x, y) call("+", x, y), c(plot_call, parsed_ggplot2_args)))
938967
)
939968
)
@@ -1062,7 +1091,7 @@ srv_distribution <- function(id,
10621091
teal.code::eval_code(
10631092
qenv,
10641093
substitute(
1065-
expr = plot <- plot_call,
1094+
expr = qq_plot <- plot_call,
10661095
env = list(plot_call = Reduce(function(x, y) call("+", x, y), c(plot_call, parsed_ggplot2_args)))
10671096
)
10681097
)
@@ -1211,7 +1240,7 @@ srv_distribution <- function(id,
12111240
qenv,
12121241
substitute(
12131242
expr = {
1214-
test_table <- ANL %>%
1243+
test_table_data <- ANL %>%
12151244
dplyr::select(dist_var) %>%
12161245
with(., broom::glance(do.call(test, args))) %>%
12171246
dplyr::mutate_if(is.numeric, round, 3)
@@ -1224,7 +1253,7 @@ srv_distribution <- function(id,
12241253
qenv,
12251254
substitute(
12261255
expr = {
1227-
test_table <- ANL %>%
1256+
test_table_data <- ANL %>%
12281257
dplyr::select(dist_var, s_var, g_var) %>%
12291258
dplyr::group_by_at(dplyr::vars(dplyr::any_of(groups))) %>%
12301259
dplyr::do(tests = broom::glance(do.call(test, args))) %>%
@@ -1235,9 +1264,6 @@ srv_distribution <- function(id,
12351264
)
12361265
)
12371266
}
1238-
qenv %>%
1239-
# used to display table when running show-r-code code
1240-
teal.code::eval_code(quote(test_table))
12411267
}
12421268
)
12431269

@@ -1247,32 +1273,39 @@ srv_distribution <- function(id,
12471273
# wrapped in if since could lead into validate error - we do want to continue
12481274
test_q_out <- try(test_q(), silent = TRUE)
12491275
if (!inherits(test_q_out, c("try-error", "error"))) {
1250-
c(common_q(), test_q_out)
1276+
c(
1277+
common_q(),
1278+
within(test_q_out, {
1279+
test_table <- DT::datatable(
1280+
test_table_data,
1281+
options = list(scrollX = TRUE),
1282+
rownames = FALSE
1283+
)
1284+
})
1285+
)
12511286
} else {
1252-
common_q()
1287+
within(common_q(), test_table <- NULL)
12531288
}
12541289
})
12551290

12561291
output_dist_q <- reactive(c(output_common_q(), req(dist_q())))
12571292
output_qq_q <- reactive(c(output_common_q(), req(qq_q())))
12581293

1259-
decorated_output_dist_q_no_print <- srv_transform_teal_data(
1260-
"d_dist",
1294+
decorated_output_dist_q <- srv_decorate_teal_data(
1295+
"d_density",
12611296
data = output_dist_q,
1262-
transformators = decorators
1297+
decorators = subset_decorators("histogram_plot", decorators),
1298+
expr = print(histogram_plot)
12631299
)
12641300

1265-
decorated_output_dist_q <- reactive(within(req(decorated_output_dist_q_no_print()), expr = print(plot)))
1266-
1267-
decorated_output_qq_q_no_print <- srv_transform_teal_data(
1301+
decorated_output_qq_q <- srv_decorate_teal_data(
12681302
"d_qq",
12691303
data = output_qq_q,
1270-
transformators = decorators
1304+
decorators = subset_decorators("qq_plot", decorators),
1305+
expr = print(qq_plot)
12711306
)
12721307

1273-
decorated_output_qq_q <- reactive(within(req(decorated_output_qq_q_no_print()), expr = print(plot)))
1274-
1275-
decorated_output_q <- reactive({
1308+
decorated_output_q_base <- reactive({
12761309
tab <- req(input$tabs) # tab is NULL upon app launch, hence will crash without this statement
12771310
if (tab == "Histogram") {
12781311
decorated_output_dist_q()
@@ -1281,30 +1314,33 @@ srv_distribution <- function(id,
12811314
}
12821315
})
12831316

1284-
dist_r <- reactive({
1285-
req(output_dist_q()) # Ensure original errors are displayed
1286-
decorated_output_dist_q()[["plot"]]
1287-
})
1288-
1289-
qq_r <- reactive({
1290-
req(output_qq_q()) # Ensure original errors are displayed
1291-
decorated_output_qq_q()[["plot"]]
1292-
})
1317+
decorated_output_q_summary <- srv_decorate_teal_data(
1318+
"d_summary",
1319+
data = decorated_output_q_base,
1320+
decorators = subset_decorators("summary_table", decorators),
1321+
expr = summary_table
1322+
)
12931323

1294-
output$summary_table <- DT::renderDataTable(
1295-
expr = if (iv_r()$is_valid()) decorated_output_dist_q()[["summary_table"]] else NULL,
1296-
options = list(
1297-
autoWidth = TRUE,
1298-
columnDefs = list(list(width = "200px", targets = "_all"))
1299-
),
1300-
rownames = FALSE
1324+
decorated_output_q_test <- srv_decorate_teal_data(
1325+
"d_test",
1326+
data = decorated_output_q_summary,
1327+
decorators = subset_decorators("test_table", decorators),
1328+
expr = test_table
13011329
)
13021330

1331+
decorated_output_q <- decorated_output_q_test
1332+
1333+
dist_r <- reactive(req(decorated_output_dist_q())[["histogram_plot"]])
1334+
1335+
qq_r <- reactive(req(decorated_output_qq_q())[["qq_plot"]])
1336+
1337+
output$summary_table <- DT::renderDataTable(expr = decorated_output_q()[["summary_table"]])
1338+
13031339
tests_r <- reactive({
13041340
req(iv_r()$is_valid())
13051341
teal::validate_inputs(iv_r_dist())
13061342
req(test_q()) # Ensure original errors are displayed
1307-
decorated_output_dist_q()[["test_table"]]
1343+
decorated_output_q()[["test_table"]]
13081344
})
13091345

13101346
pws1 <- teal.widgets::plot_with_settings_srv(
@@ -1324,9 +1360,7 @@ srv_distribution <- function(id,
13241360
)
13251361

13261362
output$t_stats <- DT::renderDataTable(
1327-
expr = tests_r(),
1328-
options = list(scrollX = TRUE),
1329-
rownames = FALSE
1363+
expr = tests_r()
13301364
)
13311365

13321366
teal.widgets::verbatim_popup_srv(

0 commit comments

Comments
 (0)