Skip to content

Commit ba0e71e

Browse files
authored
rewrite modules to teal_reportable (#887)
Merging this to a feature branch. The rest can be dona as normal issues. Thanks!
1 parent c283343 commit ba0e71e

22 files changed

+400
-484
lines changed

R/tm_a_pca.R

Lines changed: 21 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -44,8 +44,7 @@
4444
#' For additional details and examples of decorators, refer to the vignette
4545
#' `vignette("decorate-module-output", package = "teal.modules.general")`.
4646
#'
47-
#' To learn more please refer to the vignette
48-
#' `vignette("transform-module-output", package = "teal")` or the [`teal::teal_transform_module()`] documentation.
47+
#' @inheritSection teal::example_module Reporting
4948
#'
5049
#' @examplesShinylive
5150
#' library(teal.modules.general)
@@ -235,9 +234,6 @@ ui_a_pca <- function(id, ...) {
235234
uiOutput(ns("all_plots"))
236235
),
237236
encoding = tags$div(
238-
### Reporter
239-
teal.reporter::simple_reporter_ui(ns("simple_reporter")),
240-
###
241237
tags$label("Encodings", class = "text-primary"),
242238
teal.transform::datanames_input(args["dat"]),
243239
teal.transform::data_extract_ui(
@@ -353,9 +349,7 @@ ui_a_pca <- function(id, ...) {
353349
}
354350

355351
# 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")
352+
srv_a_pca <- function(id, data, dat, plot_height, plot_width, ggplot2_args, decorators) {
359353
checkmate::assert_class(data, "reactive")
360354
checkmate::assert_class(isolate(data()), "teal_data")
361355
moduleServer(id, function(input, output, session) {
@@ -436,9 +430,11 @@ srv_a_pca <- function(id, data, reporter, filter_panel_api, dat, plot_height, pl
436430
selector_list = selector_list,
437431
datasets = data
438432
)
439-
qenv <- reactive(
440-
teal.code::eval_code(data(), 'library("ggplot2");library("dplyr");library("tidyr")') # nolint quotes
441-
)
433+
qenv <- reactive({
434+
obj <- data()
435+
teal.reporter::teal_card(obj) <- append(teal.reporter::teal_card(obj), "# Principal Component Analysis", after = 0)
436+
teal.code::eval_code(obj, 'library("ggplot2");library("dplyr");library("tidyr")') # nolint quotes
437+
})
442438
anl_merged_q <- reactive({
443439
req(anl_merged_input())
444440
qenv() %>%
@@ -514,20 +510,26 @@ srv_a_pca <- function(id, data, reporter, filter_panel_api, dat, plot_height, pl
514510
)
515511
)
516512

513+
teal.reporter::teal_card(qenv) <- append(teal.reporter::teal_card(qenv), "## Principal Components Table")
514+
517515
qenv <- teal.code::eval_code(
518516
qenv,
519517
quote({
520518
tbl_importance <- dplyr::as_tibble(pca$importance, rownames = "Metric")
521519
tbl_importance
522-
})
520+
}),
521+
keep_output = "tbl_importance"
523522
)
524523

524+
teal.reporter::teal_card(qenv) <- append(teal.reporter::teal_card(qenv), "## Eigenvectors Table")
525+
525526
teal.code::eval_code(
526527
qenv,
527528
quote({
528529
tbl_eigenvector <- dplyr::as_tibble(pca$rotation, rownames = "Variable")
529530
tbl_eigenvector
530-
})
531+
}),
532+
keep_output = "tbl_eigenvector"
531533
)
532534
})
533535

@@ -602,7 +604,7 @@ srv_a_pca <- function(id, data, reporter, filter_panel_api, dat, plot_height, pl
602604
),
603605
ggtheme = ggtheme
604606
)
605-
607+
teal.reporter::teal_card(base_q) <- append(teal.reporter::teal_card(base_q), "## Elbow plot")
606608
teal.code::eval_code(
607609
base_q,
608610
substitute(
@@ -679,6 +681,7 @@ srv_a_pca <- function(id, data, reporter, filter_panel_api, dat, plot_height, pl
679681
ggtheme = ggtheme
680682
)
681683

684+
teal.reporter::teal_card(base_q) <- append(teal.reporter::teal_card(base_q), "## Circle plot")
682685
teal.code::eval_code(
683686
base_q,
684687
substitute(
@@ -737,6 +740,7 @@ srv_a_pca <- function(id, data, reporter, filter_panel_api, dat, plot_height, pl
737740
size <- input$size
738741
font_size <- input$font_size
739742

743+
teal_card(base_q) <- append(teal_card(base_q), "## Biplot")
740744
qenv <- teal.code::eval_code(
741745
qenv,
742746
substitute(
@@ -997,6 +1001,7 @@ srv_a_pca <- function(id, data, reporter, filter_panel_api, dat, plot_height, pl
9971001
parsed_ggplot2_args$theme
9981002
)
9991003

1004+
teal_card(base_q) <- append(teal_card(base_q), "## Eigenvector plot")
10001005
teal.code::eval_code(
10011006
base_q,
10021007
substitute(
@@ -1039,7 +1044,8 @@ srv_a_pca <- function(id, data, reporter, filter_panel_api, dat, plot_height, pl
10391044
decorators = select_decorators(decorators, obj_name),
10401045
expr = reactive({
10411046
substitute(.plot, env = list(.plot = as.name(obj_name)))
1042-
})
1047+
}),
1048+
keep_output = obj_name
10431049
)
10441050
},
10451051
names(output_q),
@@ -1131,33 +1137,6 @@ srv_a_pca <- function(id, data, reporter, filter_panel_api, dat, plot_height, pl
11311137
verbatim_content = source_code_r,
11321138
title = "R Code for PCA"
11331139
)
1134-
1135-
### REPORTER
1136-
if (with_reporter) {
1137-
card_fun <- function(comment, label) {
1138-
card <- teal::report_card_template(
1139-
title = "Principal Component Analysis Plot",
1140-
label = label,
1141-
with_filter = with_filter,
1142-
filter_panel_api = filter_panel_api
1143-
)
1144-
card$append_text("Principal Components Table", "header3")
1145-
card$append_table(computation()[["tbl_importance"]])
1146-
card$append_text("Eigenvectors Table", "header3")
1147-
card$append_table(computation()[["tbl_eigenvector"]])
1148-
card$append_text("Plot", "header3")
1149-
card$append_plot(plot_r(), dim = pws$dim())
1150-
if (!comment == "") {
1151-
card$append_text("Comment", "header3")
1152-
card$append_text(comment)
1153-
}
1154-
card$append_src(source_code_r())
1155-
card
1156-
}
1157-
teal.reporter::simple_reporter_srv("simple_reporter", reporter = reporter, card_fun = card_fun)
1158-
}
1159-
###
1160-
11611140
decorated_output_q
11621141
})
11631142
}

R/tm_a_regression.R

Lines changed: 12 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,8 @@
6767
#' To learn more please refer to the vignette
6868
#' `vignette("transform-module-output", package = "teal")` or the [`teal::teal_transform_module()`] documentation.
6969
#'
70+
#' @inheritSection teal::example_module Reporting
71+
#'
7072
#' @examplesShinylive
7173
#' library(teal.modules.general)
7274
#' interactive <- function() TRUE
@@ -285,9 +287,6 @@ ui_a_regression <- function(id, ...) {
285287
tags$div(verbatimTextOutput(ns("text")))
286288
)),
287289
encoding = tags$div(
288-
### Reporter
289-
teal.reporter::simple_reporter_ui(ns("simple_reporter")),
290-
###
291290
tags$label("Encodings", class = "text-primary"), tags$br(),
292291
teal.transform::datanames_input(args[c("response", "regressor")]),
293292
teal.transform::data_extract_ui(
@@ -386,17 +385,13 @@ ui_a_regression <- function(id, ...) {
386385
# Server function for the regression module
387386
srv_a_regression <- function(id,
388387
data,
389-
reporter,
390-
filter_panel_api,
391388
response,
392389
regressor,
393390
plot_height,
394391
plot_width,
395392
ggplot2_args,
396393
default_outlier_label,
397394
decorators) {
398-
with_reporter <- !missing(reporter) && inherits(reporter, "Reporter")
399-
with_filter <- !missing(filter_panel_api) && inherits(filter_panel_api, "FilterPanelAPI")
400395
checkmate::assert_class(data, "reactive")
401396
checkmate::assert_class(isolate(data()), "teal_data")
402397
moduleServer(id, function(input, output, session) {
@@ -464,7 +459,7 @@ srv_a_regression <- function(id,
464459

465460
qenv <- reactive({
466461
obj <- data()
467-
teal.reporter::teal_card(obj) <- c(teal.reporter::teal_card(obj), "# Module's computation")
462+
teal.reporter::teal_card(obj) <- append(teal.reporter::teal_card(obj), "# Linear Regression Plot", after = 0)
468463
teal.code::eval_code(obj, 'library("ggplot2");library("dplyr")') # nolint quotes
469464
})
470465

@@ -528,7 +523,7 @@ srv_a_regression <- function(id,
528523
)
529524
}
530525

531-
anl_merged_q() %>%
526+
anl_fit <- anl_merged_q() %>%
532527
teal.code::eval_code(substitute(fit <- stats::lm(form, data = ANL), env = list(form = form))) %>%
533528
teal.code::eval_code(quote({
534529
for (regressor in names(fit$contrasts)) {
@@ -538,7 +533,12 @@ srv_a_regression <- function(id,
538533
)
539534
}
540535
})) %>%
541-
teal.code::eval_code(quote(summary(fit)))
536+
teal.code::eval_code(quote({
537+
fit_summary <- summary(fit)
538+
fit_summary
539+
}), keep_output = "fit_summary")
540+
teal.reporter::teal_card(anl_fit) <- append(teal.reporter::teal_card(anl_fit), "## Plot")
541+
anl_fit
542542
})
543543

544544
label_col <- reactive({
@@ -1000,7 +1000,8 @@ srv_a_regression <- function(id,
10001000
"decorator",
10011001
data = output_q,
10021002
decorators = select_decorators(decorators, "plot"),
1003-
expr = quote(plot)
1003+
expr = quote(plot),
1004+
keep_output = "plot"
10041005
)
10051006

10061007
fitted <- reactive({
@@ -1037,28 +1038,6 @@ srv_a_regression <- function(id,
10371038
title = "R code for the regression plot",
10381039
)
10391040

1040-
### REPORTER
1041-
if (with_reporter) {
1042-
card_fun <- function(comment, label) {
1043-
card <- teal::report_card_template(
1044-
title = "Linear Regression Plot",
1045-
label = label,
1046-
with_filter = with_filter,
1047-
filter_panel_api = filter_panel_api
1048-
)
1049-
card$append_text("Plot", "header3")
1050-
card$append_plot(plot_r(), dim = pws$dim())
1051-
if (!comment == "") {
1052-
card$append_text("Comment", "header3")
1053-
card$append_text(comment)
1054-
}
1055-
card$append_src(source_code_r())
1056-
card
1057-
}
1058-
teal.reporter::simple_reporter_srv("simple_reporter", reporter = reporter, card_fun = card_fun)
1059-
}
1060-
###
1061-
10621041
decorated_output_q
10631042
})
10641043
}

R/tm_g_association.R

Lines changed: 13 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,8 @@
4848
#' To learn more please refer to the vignette
4949
#' `vignette("transform-module-output", package = "teal")` or the [`teal::teal_transform_module()`] documentation.
5050
#'
51+
#' @inheritSection teal::example_module Reporting
52+
#'
5153
#' @examplesShinylive
5254
#' library(teal.modules.general)
5355
#' interactive <- function() TRUE
@@ -232,9 +234,6 @@ ui_tm_g_association <- function(id, ...) {
232234
teal.widgets::plot_with_settings_ui(id = ns("myplot"))
233235
),
234236
encoding = tags$div(
235-
### Reporter
236-
teal.reporter::simple_reporter_ui(ns("simple_reporter")),
237-
###
238237
tags$label("Encodings", class = "text-primary"),
239238
teal.transform::datanames_input(args[c("ref", "vars")]),
240239
teal.transform::data_extract_ui(
@@ -301,16 +300,12 @@ ui_tm_g_association <- function(id, ...) {
301300
# Server function for the association module
302301
srv_tm_g_association <- function(id,
303302
data,
304-
reporter,
305-
filter_panel_api,
306303
ref,
307304
vars,
308305
plot_height,
309306
plot_width,
310307
ggplot2_args,
311308
decorators) {
312-
with_reporter <- !missing(reporter) && inherits(reporter, "Reporter")
313-
with_filter <- !missing(filter_panel_api) && inherits(filter_panel_api, "FilterPanelAPI")
314309
checkmate::assert_class(data, "reactive")
315310
checkmate::assert_class(isolate(data()), "teal_data")
316311

@@ -346,9 +341,11 @@ srv_tm_g_association <- function(id,
346341
selector_list = selector_list
347342
)
348343

349-
qenv <- reactive(
350-
teal.code::eval_code(data(), 'library("ggplot2");library("dplyr");library("ggmosaic")') # nolint quotes
351-
)
344+
qenv <- reactive({
345+
obj <- data()
346+
teal.reporter::teal_card(obj) <- append(teal.reporter::teal_card(obj), "# Association Plot", after = 0)
347+
teal.code::eval_code(obj, 'library("ggplot2");library("dplyr");library("ggmosaic")') # nolint quotes
348+
})
352349
anl_merged_q <- reactive({
353350
req(anl_merged_input())
354351
qenv() %>% teal.code::eval_code(as.expression(anl_merged_input()$expr))
@@ -495,8 +492,10 @@ srv_tm_g_association <- function(id,
495492
)
496493
)
497494
}
495+
obj <- merged$anl_q_r()
496+
teal.reporter::teal_card(obj) <- append(teal.reporter::teal_card(obj), "## Plot")
498497
teal.code::eval_code(
499-
merged$anl_q_r(),
498+
obj,
500499
substitute(
501500
expr = title <- new_title,
502501
env = list(new_title = new_title)
@@ -506,7 +505,7 @@ srv_tm_g_association <- function(id,
506505
substitute(
507506
expr = {
508507
plots <- plot_calls
509-
plot <- grid.arrange(plots[[1]], plots[[2]], ncol = 1)
508+
plot <- gridExtra::grid.arrange(plots[[1]], plots[[2]], ncol = 1)
510509
},
511510
env = list(
512511
plot_calls = do.call(
@@ -523,7 +522,8 @@ srv_tm_g_association <- function(id,
523522
id = "decorator",
524523
data = output_q,
525524
decorators = select_decorators(decorators, "plot"),
526-
expr = quote(plot)
525+
expr = quote(plot),
526+
keep_output = "plot"
527527
)
528528

529529
plot_r <- reactive({
@@ -551,28 +551,6 @@ srv_tm_g_association <- function(id,
551551
title = "Association Plot"
552552
)
553553

554-
### REPORTER
555-
if (with_reporter) {
556-
card_fun <- function(comment, label) {
557-
card <- teal::report_card_template(
558-
title = "Association Plot",
559-
label = label,
560-
with_filter = with_filter,
561-
filter_panel_api = filter_panel_api
562-
)
563-
card$append_text("Plot", "header3")
564-
card$append_plot(plot_r(), dim = pws$dim())
565-
if (!comment == "") {
566-
card$append_text("Comment", "header3")
567-
card$append_text(comment)
568-
}
569-
card$append_src(source_code_r())
570-
card
571-
}
572-
teal.reporter::simple_reporter_srv("simple_reporter", reporter = reporter, card_fun = card_fun)
573-
}
574-
575554
decorated_output_grob_q
576-
###
577555
})
578556
}

0 commit comments

Comments
 (0)