Skip to content

Commit 74c81d9

Browse files
m7prgithub-actions[bot]Copilotaverissimo
authored
📑 Reporter Refactor: Adds support for new teal_report and teal_card class (#308)
Tested with - #318 Fixes - #299 ### Companion PRs: - insightsengineering/teal#1541 - insightsengineering/teal.code#255 - insightsengineering/teal.data#370 - insightsengineering/teal.reporter#331 - insightsengineering/teal.modules.general#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]> Co-authored-by: github-actions <41898282+github-actions[bot]@users.noreply.github.com> Co-authored-by: Copilot <[email protected]> Co-authored-by: André Veríssimo <[email protected]>
1 parent dd1c8d5 commit 74c81d9

13 files changed

+449
-411
lines changed

‎DESCRIPTION‎

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ Depends:
2424
osprey (>= 0.1.17),
2525
R (>= 3.6),
2626
shiny (>= 1.8.1),
27-
teal (>= 1.0.0),
27+
teal (>= 1.0.0.9003),
2828
teal.transform (>= 0.7.0)
2929
Imports:
3030
bslib (>= 0.8.0),
@@ -37,7 +37,7 @@ Imports:
3737
teal.code (>= 0.7.0),
3838
teal.data (>= 0.8.0),
3939
teal.logger (>= 0.4.0),
40-
teal.reporter (>= 0.5.0),
40+
teal.reporter (>= 0.5.0.9001),
4141
teal.widgets (>= 0.5.0),
4242
tern (>= 0.9.7),
4343
tidyr (>= 0.8.3)
@@ -49,7 +49,9 @@ Suggests:
4949
testthat (>= 3.2.3),
5050
withr (>= 3.0.0)
5151
Remotes:
52-
insightsengineering/osprey
52+
insightsengineering/osprey,
53+
insightsengineering/teal,
54+
insightsengineering/teal.reporter
5355
Config/Needs/verdepcheck: insightsengineering/osprey, rstudio/shiny,
5456
insightsengineering/teal, insightsengineering/teal.slice,
5557
insightsengineering/teal.transform, mllg/checkmate, tidyverse/dplyr,
@@ -65,4 +67,4 @@ Encoding: UTF-8
6567
Language: en-US
6668
LazyData: true
6769
Roxygen: list(markdown = TRUE)
68-
RoxygenNote: 7.3.2
70+
RoxygenNote: 7.3.3

‎R/tm_g_ae_oview.R‎

Lines changed: 15 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
#' sub-groups (e.g. Serious events, Related events, etc.)
1313
#'
1414
#' @inherit argument_convention return
15+
#' @inheritSection teal::example_module Reporting
1516
#'
1617
#' @export
1718
#'
@@ -129,10 +130,6 @@ ui_g_ae_oview <- function(id, ...) {
129130
plot_decorate_output(id = ns(NULL))
130131
),
131132
encoding = tags$div(
132-
### Reporter
133-
teal.reporter::add_card_button_ui(ns("add_reporter"), label = "Add Report Card"),
134-
tags$br(), tags$br(),
135-
###
136133
teal.widgets::optionalSelectInput(
137134
ns("arm_var"),
138135
"Arm Variable",
@@ -198,14 +195,10 @@ ui_g_ae_oview <- function(id, ...) {
198195

199196
srv_g_ae_oview <- function(id,
200197
data,
201-
filter_panel_api,
202-
reporter,
203198
dataname,
204199
label,
205200
plot_height,
206201
plot_width) {
207-
with_reporter <- !missing(reporter) && inherits(reporter, "Reporter")
208-
with_filter <- !missing(filter_panel_api) && inherits(filter_panel_api, "FilterPanelAPI")
209202
checkmate::assert_class(data, "reactive")
210203
checkmate::assert_class(isolate(data()), "teal_data")
211204

@@ -286,7 +279,16 @@ srv_g_ae_oview <- function(id,
286279
output_q <- shiny::debounce(
287280
millis = 200,
288281
r = reactive({
289-
ANL <- data()[[dataname]]
282+
obj <- data()
283+
teal.reporter::teal_card(obj) <-
284+
c(
285+
teal.reporter::teal_card("# AE Overview"),
286+
teal.reporter::teal_card(obj),
287+
teal.reporter::teal_card("## Module's code")
288+
)
289+
obj <- teal.code::eval_code(obj, "library(dplyr)")
290+
291+
ANL <- obj[[dataname]]
290292

291293
teal::validate_has_data(ANL, min_nrow = 10, msg = sprintf("%s has not enough data", dataname))
292294

@@ -297,7 +299,7 @@ srv_g_ae_oview <- function(id,
297299
"Treatment or Control not found in Arm Variable. Perhaps they have been filtered out?"
298300
))
299301

300-
q1 <- teal.code::eval_code(data(), "library(dplyr)") %>%
302+
q1 <- obj %>%
301303
teal.code::eval_code(
302304
code = as.expression(c(
303305
bquote(anl_labels <- formatters::var_labels(.(as.name(dataname)), fill = FALSE)),
@@ -309,6 +311,8 @@ srv_g_ae_oview <- function(id,
309311
))
310312
)
311313

314+
teal.reporter::teal_card(q1) <- c(teal.reporter::teal_card(q1), "## Plot")
315+
312316
teal.code::eval_code(
313317
q1,
314318
code = as.expression(c(
@@ -339,25 +343,6 @@ srv_g_ae_oview <- function(id,
339343
verbatim_content = reactive(teal.code::get_code(output_q())),
340344
title = paste("R code for", label)
341345
)
342-
### REPORTER
343-
if (with_reporter) {
344-
card_fun <- function(comment, label) {
345-
card <- teal::report_card_template(
346-
title = "AE Overview",
347-
label = label,
348-
with_filter = with_filter,
349-
filter_panel_api = filter_panel_api
350-
)
351-
card$append_text("Plot", "header3")
352-
card$append_plot(plot_r(), dim = pws$dim())
353-
if (!comment == "") {
354-
card$append_text("Comment", "header3")
355-
card$append_text(comment)
356-
}
357-
card$append_src(teal.code::get_code(output_q()))
358-
card
359-
}
360-
teal.reporter::add_card_button_srv("add_reporter", reporter = reporter, card_fun = card_fun)
361-
}
346+
set_chunk_dims(pws, output_q)
362347
})
363348
}

‎R/tm_g_ae_sub.R‎

Lines changed: 38 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
#' @author Molly He (hey59) \email{[email protected]}
1414
#'
1515
#' @inherit argument_convention return
16+
#' @inheritSection teal::example_module Reporting
1617
#'
1718
#' @export
1819
#'
@@ -104,10 +105,6 @@ ui_g_ae_sub <- function(id, ...) {
104105
plot_decorate_output(id = ns(NULL))
105106
),
106107
encoding = tags$div(
107-
### Reporter
108-
teal.reporter::add_card_button_ui(ns("add_reporter"), label = "Add Report Card"),
109-
tags$br(), tags$br(),
110-
###
111108
tags$label("Encodings", class = "text-primary"),
112109
helpText("Analysis data:", tags$code("ADAE")),
113110
teal.widgets::optionalSelectInput(
@@ -175,14 +172,10 @@ ui_g_ae_sub <- function(id, ...) {
175172

176173
srv_g_ae_sub <- function(id,
177174
data,
178-
filter_panel_api,
179-
reporter,
180175
dataname,
181176
label,
182177
plot_height,
183178
plot_width) {
184-
with_reporter <- !missing(reporter) && inherits(reporter, "Reporter")
185-
with_filter <- !missing(filter_panel_api) && inherits(filter_panel_api, "FilterPanelAPI")
186179
checkmate::assert_class(data, "reactive")
187180
checkmate::assert_class(shiny::isolate(data()), "teal_data")
188181

@@ -317,8 +310,16 @@ srv_g_ae_sub <- function(id,
317310
output_q <- shiny::debounce(
318311
millis = 200,
319312
r = reactive({
320-
ANL <- data()[[dataname]]
321-
ADSL <- data()[["ADSL"]]
313+
obj <- data()
314+
teal.reporter::teal_card(obj) <-
315+
c(
316+
teal.reporter::teal_card("# AE by Subgroups"),
317+
teal.reporter::teal_card(obj),
318+
teal.reporter::teal_card("## Module's code")
319+
)
320+
321+
ANL <- obj[[dataname]]
322+
ADSL <- obj[["ADSL"]]
322323

323324
teal::validate_has_data(ANL, min_nrow = 10, msg = sprintf("%s has not enough data", dataname))
324325

@@ -347,29 +348,33 @@ srv_g_ae_sub <- function(id,
347348
bquote(group_labels <- setNames(.(group_labels), .(input$groups)))
348349
}
349350

350-
teal.code::eval_code(data(), code = group_labels_call) %>%
351-
teal.code::eval_code(code = "") %>%
352-
teal.code::eval_code(
353-
code = as.expression(c(
354-
bquote(
355-
plot <- osprey::g_ae_sub(
356-
id = .(as.name(dataname))$USUBJID,
357-
arm = as.factor(.(as.name(dataname))[[.(input$arm_var)]]),
358-
arm_sl = as.character(ADSL[[.(input$arm_var)]]),
359-
trt = .(input$arm_trt),
360-
ref = .(input$arm_ref),
361-
subgroups = .(as.name(dataname))[.(input$groups)],
362-
subgroups_sl = ADSL[.(input$groups)],
363-
subgroups_levels = group_labels,
364-
conf_level = .(input$conf_level),
365-
diff_ci_method = .(input$ci),
366-
fontsize = .(font_size()),
367-
arm_n = .(input$arm_n),
368-
draw = TRUE
369-
)
351+
q1 <- teal.code::eval_code(obj, code = group_labels_call) %>%
352+
teal.code::eval_code(code = "")
353+
354+
teal.reporter::teal_card(q1) <- c(teal.reporter::teal_card(q1), "## Plot")
355+
356+
teal.code::eval_code(
357+
q1,
358+
code = as.expression(c(
359+
bquote(
360+
plot <- osprey::g_ae_sub(
361+
id = .(as.name(dataname))$USUBJID,
362+
arm = as.factor(.(as.name(dataname))[[.(input$arm_var)]]),
363+
arm_sl = as.character(ADSL[[.(input$arm_var)]]),
364+
trt = .(input$arm_trt),
365+
ref = .(input$arm_ref),
366+
subgroups = .(as.name(dataname))[.(input$groups)],
367+
subgroups_sl = ADSL[.(input$groups)],
368+
subgroups_levels = group_labels,
369+
conf_level = .(input$conf_level),
370+
diff_ci_method = .(input$ci),
371+
fontsize = .(font_size()),
372+
arm_n = .(input$arm_n),
373+
draw = TRUE
370374
)
371-
))
372-
)
375+
)
376+
))
377+
)
373378
})
374379
)
375380

@@ -380,26 +385,6 @@ srv_g_ae_sub <- function(id,
380385
verbatim_content = reactive(teal.code::get_code(output_q())),
381386
title = paste("R code for", label),
382387
)
383-
384-
### REPORTER
385-
if (with_reporter) {
386-
card_fun <- function(comment, label) {
387-
card <- teal::report_card_template(
388-
title = "AE Subgroups",
389-
label = label,
390-
with_filter = with_filter,
391-
filter_panel_api = filter_panel_api
392-
)
393-
card$append_text("Plot", "header3")
394-
card$append_plot(plot_r(), dim = pws$dim())
395-
if (!comment == "") {
396-
card$append_text("Comment", "header3")
397-
card$append_text(comment)
398-
}
399-
card$append_src(teal.code::get_code(output_q()))
400-
card
401-
}
402-
teal.reporter::add_card_button_srv("add_reporter", reporter = reporter, card_fun = card_fun)
403-
}
388+
set_chunk_dims(pws, output_q)
404389
})
405390
}

0 commit comments

Comments
 (0)