Skip to content

Commit 320e808

Browse files
averissimogogonzogithub-actions[bot]m7pr
authored
📑 Reporter Refactor: Adds support for new teal_report and teal_card class (#884)
# Pull Request Fixes: - insightsengineering/teal#1526 Built on top of: - insightsengineering/teal#1499 _(will be closed once this PR is stable)_ ### Companion PRs: - insightsengineering/teal#1541 - insightsengineering/teal.code#255 - insightsengineering/teal.data#370 - insightsengineering/teal.reporter#331 - #884 ### Changes description - [x] Return a `teal_report` object on every module - [x] Remove old "Add to reporter" implementation in modules --------- Signed-off-by: Marcin <[email protected]> Signed-off-by: André Veríssimo <[email protected]> Co-authored-by: Dawid Kaledkowski <[email protected]> Co-authored-by: github-actions <41898282+github-actions[bot]@users.noreply.github.com> Co-authored-by: Marcin <[email protected]> Co-authored-by: m7pr <[email protected]>
1 parent 9ab175a commit 320e808

21 files changed

+709
-707
lines changed

‎.pre-commit-config.yaml‎

Lines changed: 19 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -18,33 +18,45 @@ repos:
1818
- ggmosaic
1919
- ggplot2
2020
- shiny
21+
- insightsengineering/teal
22+
- insightsengineering/teal.transform
23+
- bslib
2124
- checkmate
25+
- colourpicker
2226
- dplyr
2327
- DT
2428
- forcats
29+
- ggExtra
30+
- ggpmisc
31+
- ggpp
32+
- ggrepel
33+
- goftest
2534
- grid
26-
- logger
35+
- gridExtra
36+
- htmlwidgets
37+
- jsonlite
38+
- lattice
39+
- lifecycle
40+
- MASS
41+
- rtables
2742
- scales
2843
- shinyjs
2944
- shinyTree
3045
- shinyvalidate
3146
- shinyWidgets
47+
- sparkline
3248
- stats
3349
- stringr
34-
- insightsengineering/roxy.shinylive
35-
- insightsengineering/teal
36-
- insightsengineering/teal.transform
3750
- insightsengineering/teal.code
3851
- insightsengineering/teal.data
3952
- insightsengineering/teal.logger
4053
- insightsengineering/teal.reporter
41-
- insightsengineering/teal.slice
4254
- insightsengineering/teal.widgets
4355
- tern
4456
- tibble
4557
- tidyr
46-
- utils
47-
58+
- insightsengineering/teal.slice
59+
- insightsengineering/roxy.shinylive
4860
- id: spell-check
4961
name: Check spelling with `spelling`
5062
exclude: >

‎DESCRIPTION‎

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ Depends:
2727
ggplot2 (>= 3.4.0),
2828
R (>= 4.1),
2929
shiny (>= 1.8.1),
30-
teal (>= 1.0.0),
30+
teal (>= 1.0.0.9003),
3131
teal.transform (>= 0.7.0)
3232
Imports:
3333
bslib (>= 0.8.0),
@@ -61,7 +61,7 @@ Imports:
6161
teal.code (>= 0.7.0),
6262
teal.data (>= 0.8.0),
6363
teal.logger (>= 0.4.0),
64-
teal.reporter (>= 0.5.0),
64+
teal.reporter (>= 0.5.0.9001),
6565
teal.widgets (>= 0.5.0),
6666
tern (>= 0.9.7),
6767
tibble (>= 2.0.0),
@@ -83,6 +83,9 @@ Suggests:
8383
VignetteBuilder:
8484
knitr,
8585
rmarkdown
86+
Remotes:
87+
insightsengineering/teal.reporter,
88+
insightsengineering/teal
8689
Config/Needs/verdepcheck: haleyjeppson/ggmosaic, tidyverse/ggplot2,
8790
rstudio/shiny, insightsengineering/teal,
8891
insightsengineering/teal.transform, mllg/checkmate, tidyverse/dplyr,
@@ -105,4 +108,4 @@ Encoding: UTF-8
105108
Language: en-US
106109
LazyData: true
107110
Roxygen: list(markdown = TRUE, packages = c("roxy.shinylive"))
108-
RoxygenNote: 7.3.2
111+
RoxygenNote: 7.3.3

‎R/tm_a_pca.R‎

Lines changed: 26 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,8 @@
4747
#' To learn more please refer to the vignette
4848
#' `vignette("transform-module-output", package = "teal")` or the [`teal::teal_transform_module()`] documentation.
4949
#'
50+
#' @inheritSection teal::example_module Reporting
51+
#'
5052
#' @examplesShinylive
5153
#' library(teal.modules.general)
5254
#' interactive <- function() TRUE
@@ -234,10 +236,6 @@ ui_a_pca <- function(id, ...) {
234236
uiOutput(ns("all_plots"))
235237
),
236238
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-
###
241239
tags$label("Encodings", class = "text-primary"),
242240
teal.transform::datanames_input(args["dat"]),
243241
teal.transform::data_extract_ui(
@@ -353,9 +351,7 @@ ui_a_pca <- function(id, ...) {
353351
}
354352

355353
# 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")
354+
srv_a_pca <- function(id, data, dat, plot_height, plot_width, ggplot2_args, decorators) {
359355
checkmate::assert_class(data, "reactive")
360356
checkmate::assert_class(isolate(data()), "teal_data")
361357
moduleServer(id, function(input, output, session) {
@@ -436,9 +432,16 @@ srv_a_pca <- function(id, data, reporter, filter_panel_api, dat, plot_height, pl
436432
selector_list = selector_list,
437433
datasets = data
438434
)
439-
qenv <- reactive(
440-
teal.code::eval_code(data(), 'library("ggplot2");library("dplyr");library("tidyr")') # nolint quotes
441-
)
435+
qenv <- reactive({
436+
obj <- data()
437+
teal.reporter::teal_card(obj) <-
438+
c(
439+
teal.reporter::teal_card("# Principal Component Analysis"),
440+
teal.reporter::teal_card(obj),
441+
teal.reporter::teal_card("## Module's code")
442+
)
443+
teal.code::eval_code(obj, 'library("ggplot2");library("dplyr");library("tidyr")') # nolint: quotes
444+
})
442445
anl_merged_q <- reactive({
443446
req(anl_merged_input())
444447
qenv() %>%
@@ -514,6 +517,8 @@ srv_a_pca <- function(id, data, reporter, filter_panel_api, dat, plot_height, pl
514517
)
515518
)
516519

520+
teal.reporter::teal_card(qenv) <- c(teal.reporter::teal_card(qenv), "## Principal Components Table")
521+
517522
qenv <- teal.code::eval_code(
518523
qenv,
519524
quote({
@@ -522,6 +527,8 @@ srv_a_pca <- function(id, data, reporter, filter_panel_api, dat, plot_height, pl
522527
})
523528
)
524529

530+
teal.reporter::teal_card(qenv) <- c(teal.reporter::teal_card(qenv), "## Eigenvectors Table")
531+
525532
teal.code::eval_code(
526533
qenv,
527534
quote({
@@ -602,7 +609,7 @@ srv_a_pca <- function(id, data, reporter, filter_panel_api, dat, plot_height, pl
602609
),
603610
ggtheme = ggtheme
604611
)
605-
612+
teal.reporter::teal_card(base_q) <- c(teal.reporter::teal_card(base_q), "## Elbow plot")
606613
teal.code::eval_code(
607614
base_q,
608615
substitute(
@@ -679,6 +686,7 @@ srv_a_pca <- function(id, data, reporter, filter_panel_api, dat, plot_height, pl
679686
ggtheme = ggtheme
680687
)
681688

689+
teal.reporter::teal_card(base_q) <- c(teal.reporter::teal_card(base_q), "## Circle plot")
682690
teal.code::eval_code(
683691
base_q,
684692
substitute(
@@ -737,6 +745,7 @@ srv_a_pca <- function(id, data, reporter, filter_panel_api, dat, plot_height, pl
737745
size <- input$size
738746
font_size <- input$font_size
739747

748+
teal.reporter::teal_card(base_q) <- c(teal.reporter::teal_card(base_q), "## Biplot")
740749
qenv <- teal.code::eval_code(
741750
qenv,
742751
substitute(
@@ -997,6 +1006,7 @@ srv_a_pca <- function(id, data, reporter, filter_panel_api, dat, plot_height, pl
9971006
parsed_ggplot2_args$theme
9981007
)
9991008

1009+
teal.reporter::teal_card(base_q) <- c(teal.reporter::teal_card(base_q), "## Eigenvector plot")
10001010
teal.code::eval_code(
10011011
base_q,
10021012
substitute(
@@ -1038,9 +1048,8 @@ srv_a_pca <- function(id, data, reporter, filter_panel_api, dat, plot_height, pl
10381048
data = q,
10391049
decorators = select_decorators(decorators, obj_name),
10401050
expr = reactive({
1041-
substitute(print(.plot), env = list(.plot = as.name(obj_name)))
1042-
}),
1043-
expr_is_reactive = TRUE
1051+
substitute(.plot, env = list(.plot = as.name(obj_name)))
1052+
})
10441053
)
10451054
},
10461055
names(output_q),
@@ -1071,6 +1080,8 @@ srv_a_pca <- function(id, data, reporter, filter_panel_api, dat, plot_height, pl
10711080
graph_align = "center"
10721081
)
10731082

1083+
decorated_output_dims_q <- set_chunk_dims(pws, decorated_output_q)
1084+
10741085
# tables ----
10751086
output$tbl_importance <- renderTable(
10761087
expr = {
@@ -1132,31 +1143,6 @@ srv_a_pca <- function(id, data, reporter, filter_panel_api, dat, plot_height, pl
11321143
verbatim_content = source_code_r,
11331144
title = "R Code for PCA"
11341145
)
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-
###
1146+
decorated_output_dims_q
11611147
})
11621148
}

‎R/tm_a_regression.R‎

Lines changed: 26 additions & 39 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,10 +287,6 @@ ui_a_regression <- function(id, ...) {
285287
tags$div(verbatimTextOutput(ns("text")))
286288
)),
287289
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-
###
292290
tags$label("Encodings", class = "text-primary"), tags$br(),
293291
teal.transform::datanames_input(args[c("response", "regressor")]),
294292
teal.transform::data_extract_ui(
@@ -387,17 +385,13 @@ ui_a_regression <- function(id, ...) {
387385
# Server function for the regression module
388386
srv_a_regression <- function(id,
389387
data,
390-
reporter,
391-
filter_panel_api,
392388
response,
393389
regressor,
394390
plot_height,
395391
plot_width,
396392
ggplot2_args,
397393
default_outlier_label,
398394
decorators) {
399-
with_reporter <- !missing(reporter) && inherits(reporter, "Reporter")
400-
with_filter <- !missing(filter_panel_api) && inherits(filter_panel_api, "FilterPanelAPI")
401395
checkmate::assert_class(data, "reactive")
402396
checkmate::assert_class(isolate(data()), "teal_data")
403397
moduleServer(id, function(input, output, session) {
@@ -463,9 +457,16 @@ srv_a_regression <- function(id,
463457
)
464458
})
465459

466-
qenv <- reactive(
467-
teal.code::eval_code(data(), 'library("ggplot2");library("dplyr")') # nolint quotes
468-
)
460+
qenv <- reactive({
461+
obj <- data()
462+
teal.reporter::teal_card(obj) <-
463+
c(
464+
teal.reporter::teal_card("# Linear Regression Plot"),
465+
teal.reporter::teal_card(obj),
466+
teal.reporter::teal_card("## Module's code")
467+
)
468+
teal.code::eval_code(obj, 'library("ggplot2");library("dplyr")') # nolint: quotes
469+
})
469470

470471
anl_merged_q <- reactive({
471472
req(anl_merged_input())
@@ -527,7 +528,7 @@ srv_a_regression <- function(id,
527528
)
528529
}
529530

530-
anl_merged_q() %>%
531+
anl_fit <- anl_merged_q() %>%
531532
teal.code::eval_code(substitute(fit <- stats::lm(form, data = ANL), env = list(form = form))) %>%
532533
teal.code::eval_code(quote({
533534
for (regressor in names(fit$contrasts)) {
@@ -537,7 +538,12 @@ srv_a_regression <- function(id,
537538
)
538539
}
539540
})) %>%
540-
teal.code::eval_code(quote(summary(fit)))
541+
teal.code::eval_code(quote({
542+
fit_summary <- summary(fit)
543+
fit_summary
544+
}))
545+
teal.reporter::teal_card(anl_fit) <- c(teal.reporter::teal_card(anl_fit), "## Plot")
546+
anl_fit
541547
})
542548

543549
label_col <- reactive({
@@ -995,15 +1001,15 @@ srv_a_regression <- function(id,
9951001
"decorator",
9961002
data = output_q,
9971003
decorators = select_decorators(decorators, "plot"),
998-
expr = plot
1004+
expr = quote(plot)
9991005
)
10001006

10011007
fitted <- reactive({
1002-
req(output_q())
1008+
req(decorated_output_q())
10031009
decorated_output_q()[["fit"]]
10041010
})
10051011
plot_r <- reactive({
1006-
req(output_q())
1012+
req(decorated_output_q())
10071013
decorated_output_q()[["plot"]]
10081014
})
10091015

@@ -1015,41 +1021,22 @@ srv_a_regression <- function(id,
10151021
width = plot_width
10161022
)
10171023

1024+
decorated_output_dims_q <- set_chunk_dims(pws, decorated_output_q)
1025+
10181026
output$text <- renderText({
10191027
req(iv_r()$is_valid())
10201028
req(iv_out$is_valid())
10211029
paste(utils::capture.output(summary(fitted()))[-1], collapse = "\n")
10221030
})
10231031

10241032
# Render R code.
1025-
source_code_r <- reactive(teal.code::get_code(req(decorated_output_q())))
1033+
source_code_r <- reactive(teal.code::get_code(req(decorated_output_dims_q())))
10261034

10271035
teal.widgets::verbatim_popup_srv(
10281036
id = "rcode",
10291037
verbatim_content = source_code_r,
10301038
title = "R code for the regression plot",
10311039
)
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-
###
1040+
decorated_output_dims_q
10541041
})
10551042
}

‎R/tm_data_table.R‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -309,7 +309,7 @@ srv_data_table <- function(id,
309309
teal::validate_has_data(df, min_nrow = 1L, msg = paste("data", dataname, "is empty"))
310310
qenv <- teal.code::eval_code(
311311
data(),
312-
'library("dplyr");library("DT")' # nolint quotes
312+
'library("dplyr");library("DT")' # nolint: quotes.
313313
)
314314
teal.code::eval_code(
315315
qenv,

0 commit comments

Comments
 (0)