From a410508b3b8a926f26f1c1d5e130f867ea307818 Mon Sep 17 00:00:00 2001 From: Maciej Nasinski Date: Fri, 1 Jul 2022 16:46:23 +0200 Subject: [PATCH] init reporter tmc (#518) * init reporter tmc --- DESCRIPTION | 1 + NEWS.md | 1 + R/tm_a_mmrm.R | 52 ++++++++++++++++++++++++++++++++++++++++ R/tm_g_km.R | 41 +++++++++++++++++++++++++++++++ R/tm_t_events.R | 40 +++++++++++++++++++++++++++++++ R/tm_t_summary.R | 40 +++++++++++++++++++++++++++++++ R/tm_t_tte.R | 40 +++++++++++++++++++++++++++++++ staged_dependencies.yaml | 3 +++ 8 files changed, 218 insertions(+) diff --git a/DESCRIPTION b/DESCRIPTION index 49c2ffa9cc..12caa7d791 100644 --- a/DESCRIPTION +++ b/DESCRIPTION @@ -53,6 +53,7 @@ Imports: styler, teal.code (>= 0.1.1), teal.logger (>= 0.1.0), + teal.reporter (>= 0.1.0), teal.transform (>= 0.1.1), teal.widgets (>= 0.1.1), tern.mmrm (>= 0.1.1), diff --git a/NEWS.md b/NEWS.md index 7e5436b07a..1607d4ed95 100644 --- a/NEWS.md +++ b/NEWS.md @@ -3,6 +3,7 @@ ### Enhancements * Reverted missing data checkbox in `tm_t_summary` (encoding and filtering should be separate). * Implemented a new widget that allows dragging and dropping to select comparison groups. +* Added the `teal.reporter` functionality to the `tm_t_summary`, `tm_g_km`, `tm_t_events`, `tm_t_tte` and `tm_a_mmrm` modules. ### Bug fixes * Fixed bug in `tm_g_barchart_simple` which prevented graph from being shown. diff --git a/R/tm_a_mmrm.R b/R/tm_a_mmrm.R index 4729b20092..19bf79c65a 100644 --- a/R/tm_a_mmrm.R +++ b/R/tm_a_mmrm.R @@ -594,6 +594,14 @@ ui_mmrm <- function(id, ...) { teal.widgets::plot_with_settings_ui(id = ns("mmrm_plot")) ), encoding = shiny::div( + ### Reporter + shiny::tags$div( + teal.reporter::add_card_button_ui(ns("addReportCard")), + teal.reporter::download_report_button_ui(ns("downloadButton")), + teal.reporter::reset_report_button_ui(ns("resetButton")) + ), + shiny::tags$br(), + ### shiny::tags$label("Encodings", class = "text-primary"), teal.transform::datanames_input(a[c("arm_var", "paramcd", "id_var", "visit_var", "cov_var", "aval_var")]), teal.widgets::panel_group( @@ -798,6 +806,7 @@ ui_mmrm <- function(id, ...) { #' @noRd srv_mmrm <- function(id, datasets, + reporter, dataname, parentname, arm_var, @@ -814,6 +823,7 @@ srv_mmrm <- function(id, basic_table_args, ggplot2_args) { stopifnot(is_cdisc_data(datasets)) + with_reporter <- !missing(reporter) && inherits(reporter, "Reporter") shiny::moduleServer(id, function(input, output, session) { teal.code::init_chunks() @@ -1403,5 +1413,47 @@ srv_mmrm <- function(id, code_header = label, disable_buttons = disable_r_code ) + + ### REPORTER + if (with_reporter) { + card_fun <- function(comment) { + card <- teal.reporter::TealReportCard$new() + card$set_name("MMRM") + card$append_text("Mixed Model Repeated Measurements (MMRM) analysis", "header2") + card$append_text( + paste0( + "Mixed Models procedure analyzes results from repeated measures designs", + "in which the outcome is continuous and measured at fixed time points" + ), + "header3" + ) + card$append_text("Filter State", "header3") + card$append_fs(datasets) + card$append_text("Main Element", "header3") + if (!is.null(mmrm_table())) { + card$append_table(mmrm_table()) + } + if (!is.null(mmrm_plot_reactive())) { + card$append_plot(mmrm_plot_reactive()) + } + if (!comment == "") { + card$append_text("Comment", "header3") + card$append_text(comment) + } + card$append_text("Show R Code", "header3") + card$append_src(paste(get_rcode( + chunks = teal.code::get_chunks_object(parent_idx = 1L), + datasets = datasets, + title = "", + description = "" + ), collapse = "\n")) + card + } + + teal.reporter::add_card_button_srv("addReportCard", reporter = reporter, card_fun = card_fun) + teal.reporter::download_report_button_srv("downloadButton", reporter = reporter) + teal.reporter::reset_report_button_srv("resetButton", reporter) + } + ### }) } diff --git a/R/tm_g_km.R b/R/tm_g_km.R index cd6fd08b75..df8e4077e3 100644 --- a/R/tm_g_km.R +++ b/R/tm_g_km.R @@ -418,6 +418,14 @@ ui_g_km <- function(id, ...) { ) ), encoding = shiny::div( + ### Reporter + shiny::tags$div( + teal.reporter::add_card_button_ui(ns("addReportCard")), + teal.reporter::download_report_button_ui(ns("downloadButton")), + teal.reporter::reset_report_button_ui(ns("resetButton")) + ), + shiny::tags$br(), + ### shiny::tags$label("Encodings", class = "text-primary"), teal.transform::datanames_input(a[c("arm_var", "paramcd", "strata_var", "facet_var", "aval_var", "cnsr_var")]), teal.transform::data_extract_ui( @@ -580,6 +588,7 @@ ui_g_km <- function(id, ...) { #' srv_g_km <- function(id, datasets, + reporter, dataname, parentname, paramcd, @@ -594,6 +603,7 @@ srv_g_km <- function(id, plot_height, plot_width) { stopifnot(is_cdisc_data(datasets)) + with_reporter <- !missing(reporter) && inherits(reporter, "Reporter") shiny::moduleServer(id, function(input, output, session) { teal.code::init_chunks() @@ -762,5 +772,36 @@ srv_g_km <- function(id, ), modal_title = label ) + + ### REPORTER + if (with_reporter) { + card_fun <- function(comment) { + card <- teal.reporter::TealReportCard$new() + card$set_name("Kaplan Meier Plot") + card$append_text("Kaplan Meier Plot", "header2") + card$append_text("Non-parametric method used to estimate the survival function from lifetime data", "header3") + card$append_text("Filter State", "header3") + card$append_fs(datasets) + card$append_text("Main Element", "header3") + card$append_plot(km_plot()) + if (!comment == "") { + card$append_text("Comment", "header3") + card$append_text(comment) + } + card$append_text("Show R Code", "header3") + card$append_src(paste(get_rcode( + chunks = teal.code::get_chunks_object(parent_idx = 1L), + datasets = datasets, + title = "", + description = "" + ), collapse = "\n")) + card + } + + teal.reporter::add_card_button_srv("addReportCard", reporter = reporter, card_fun = card_fun) + teal.reporter::download_report_button_srv("downloadButton", reporter = reporter) + teal.reporter::reset_report_button_srv("resetButton", reporter) + } + ### }) } diff --git a/R/tm_t_events.R b/R/tm_t_events.R index cc8bdaddaf..492739df02 100644 --- a/R/tm_t_events.R +++ b/R/tm_t_events.R @@ -551,6 +551,14 @@ ui_t_events_byterm <- function(id, ...) { teal.widgets::table_with_settings_ui(ns("table")) ), encoding = shiny::div( + ### Reporter + shiny::tags$div( + teal.reporter::add_card_button_ui(ns("addReportCard")), + teal.reporter::download_report_button_ui(ns("downloadButton")), + teal.reporter::reset_report_button_ui(ns("resetButton")) + ), + shiny::tags$br(), + ### shiny::tags$label("Encodings", class = "text-primary"), teal.transform::datanames_input(a[c("arm_var", "hlt", "llt")]), teal.transform::data_extract_ui( @@ -619,6 +627,7 @@ ui_t_events_byterm <- function(id, ...) { #' @noRd srv_t_events_byterm <- function(id, datasets, + reporter, dataname, parentname, event_type, @@ -629,6 +638,7 @@ srv_t_events_byterm <- function(id, label, basic_table_args) { stopifnot(is_cdisc_data(datasets)) + with_reporter <- !missing(reporter) && inherits(reporter, "Reporter") shiny::moduleServer(id, function(input, output, session) { teal.code::init_chunks() @@ -757,5 +767,35 @@ srv_t_events_byterm <- function(id, modal_title = "Event Table", code_header = label ) + + ### REPORTER + if (with_reporter) { + card_fun <- function(comment) { + card <- teal.reporter::TealReportCard$new() + card$set_name("Events by Term Table") + card$append_text("Events by Term Table", "header2") + card$append_text("Filter State", "header3") + card$append_fs(datasets) + card$append_text("Main Element", "header3") + card$append_table(table()) + if (!comment == "") { + card$append_text("Comment", "header3") + card$append_text(comment) + } + card$append_text("Show R Code", "header3") + card$append_src(paste(get_rcode( + chunks = teal.code::get_chunks_object(parent_idx = 1L), + datasets = datasets, + title = "", + description = "" + ), collapse = "\n")) + card + } + + teal.reporter::add_card_button_srv("addReportCard", reporter = reporter, card_fun = card_fun) + teal.reporter::download_report_button_srv("downloadButton", reporter = reporter) + teal.reporter::reset_report_button_srv("resetButton", reporter) + } + ### }) } diff --git a/R/tm_t_summary.R b/R/tm_t_summary.R index 07b20a7666..32976e8c93 100644 --- a/R/tm_t_summary.R +++ b/R/tm_t_summary.R @@ -331,6 +331,14 @@ ui_summary <- function(id, ...) { teal.widgets::standard_layout( output = teal.widgets::white_small_well(teal.widgets::table_with_settings_ui(ns("table"))), encoding = shiny::div( + ### Reporter + shiny::tags$div( + teal.reporter::add_card_button_ui(ns("addReportCard")), + teal.reporter::download_report_button_ui(ns("downloadButton")), + teal.reporter::reset_report_button_ui(ns("resetButton")) + ), + shiny::tags$br(), + ### shiny::tags$label("Encodings", class = "text-primary"), teal.transform::datanames_input(a[c("arm_var", "summarize_vars")]), teal.transform::data_extract_ui( @@ -403,6 +411,7 @@ ui_summary <- function(id, ...) { #' @noRd srv_summary <- function(id, datasets, + reporter, dataname, parentname, arm_var, @@ -413,6 +422,7 @@ srv_summary <- function(id, label, basic_table_args) { stopifnot(is_cdisc_data(datasets)) + with_reporter <- !missing(reporter) && inherits(reporter, "Reporter") shiny::moduleServer(id, function(input, output, session) { teal.code::init_chunks() @@ -546,5 +556,35 @@ srv_summary <- function(id, modal_title = "R Code for the current Summary Table", code_header = label ) + + ### REPORTER + if (with_reporter) { + card_fun <- function(comment) { + card <- teal.reporter::TealReportCard$new() + card$set_name("Summary Table") + card$append_text("Summary Table", "header2") + card$append_text("Filter State", "header3") + card$append_fs(datasets) + card$append_text("Main Element", "header3") + card$append_table(table()) + if (!comment == "") { + card$append_text("Comment", "header3") + card$append_text(comment) + } + card$append_text("Show R Code", "header3") + card$append_src(paste(get_rcode( + chunks = teal.code::get_chunks_object(parent_idx = 1L), + datasets = datasets, + title = "", + description = "" + ), collapse = "\n")) + card + } + + teal.reporter::add_card_button_srv("addReportCard", reporter = reporter, card_fun = card_fun) + teal.reporter::download_report_button_srv("downloadButton", reporter = reporter) + teal.reporter::reset_report_button_srv("resetButton", reporter) + } + ### }) } diff --git a/R/tm_t_tte.R b/R/tm_t_tte.R index 7c8eebe385..33d1ccf180 100644 --- a/R/tm_t_tte.R +++ b/R/tm_t_tte.R @@ -514,6 +514,14 @@ ui_t_tte <- function(id, ...) { teal.widgets::standard_layout( output = teal.widgets::white_small_well(teal.widgets::table_with_settings_ui(ns("table"))), encoding = shiny::div( + ### Reporter + shiny::tags$div( + teal.reporter::add_card_button_ui(ns("addReportCard")), + teal.reporter::download_report_button_ui(ns("downloadButton")), + teal.reporter::reset_report_button_ui(ns("resetButton")) + ), + shiny::tags$br(), + ### shiny::tags$label("Encodings", class = "text-primary"), teal.transform::datanames_input( a[c("arm_var", "paramcd", "aval_var", "cnsr_var", "strata_var", "event_desc_var")] @@ -681,6 +689,7 @@ ui_t_tte <- function(id, ...) { #' @noRd srv_t_tte <- function(id, datasets, + reporter, arm_var, paramcd, aval_var, @@ -695,6 +704,7 @@ srv_t_tte <- function(id, label, basic_table_args) { stopifnot(is_cdisc_data(datasets)) + with_reporter <- !missing(reporter) && inherits(reporter, "Reporter") shiny::moduleServer(id, function(input, output, session) { teal.code::init_chunks() @@ -878,5 +888,35 @@ srv_t_tte <- function(id, ), modal_title = label ) + + ### REPORTER + if (with_reporter) { + card_fun <- function(comment) { + card <- teal.reporter::TealReportCard$new() + card$set_name("Time To Event Table") + card$append_text("Time To Event Table", "header2") + card$append_text("Filter State", "header3") + card$append_fs(datasets) + card$append_text("Main Element", "header3") + card$append_table(table()) + if (!comment == "") { + card$append_text("Comment", "header3") + card$append_text(comment) + } + card$append_text("Show R Code", "header3") + card$append_src(paste(get_rcode( + chunks = teal.code::get_chunks_object(parent_idx = 1L), + datasets = datasets, + title = "", + description = "" + ), collapse = "\n")) + card + } + + teal.reporter::add_card_button_srv("addReportCard", reporter = reporter, card_fun = card_fun) + teal.reporter::download_report_button_srv("downloadButton", reporter = reporter) + teal.reporter::reset_report_button_srv("resetButton", reporter) + } + ### }) } diff --git a/staged_dependencies.yaml b/staged_dependencies.yaml index 2bc6ff653c..74674aa2be 100644 --- a/staged_dependencies.yaml +++ b/staged_dependencies.yaml @@ -23,6 +23,9 @@ upstream_repos: insightsengineering/teal.logger: repo: insightsengineering/teal.logger host: https://github.com + insightsengineering/teal.reporter: + repo: insightsengineering/teal.reporter + host: https://github.com insightsengineering/teal.widgets: repo: insightsengineering/teal.widgets host: https://github.com