From 81ae566cfb939b1ed05ee789e5ca10d122ca1578 Mon Sep 17 00:00:00 2001 From: kartikeya kirar Date: Fri, 13 Oct 2023 15:27:48 +0530 Subject: [PATCH] 198 Include user's card labels when generating the report (#241) this PR fixes https://github.com/insightsengineering/teal.goshawk/issues/240 this is follow-up after https://github.com/insightsengineering/teal.reporter/pull/219 --------- Co-authored-by: github-actions <41898282+github-actions[bot]@users.noreply.github.com> Co-authored-by: 27856297+dependabot-preview[bot]@users.noreply.github.com <27856297+dependabot-preview[bot]@users.noreply.github.com> --- .lintr | 3 +- R/tm_g_gh_boxplot.R | 30 +++++++++------- R/tm_g_gh_correlationplot.R | 30 +++++++++------- R/tm_g_gh_density_distribution_plot.R | 19 +++++----- R/tm_g_gh_lineplot.R | 30 +++++++++------- R/tm_g_gh_scatterplot.R | 30 +++++++++------- R/tm_g_gh_spaghettiplot.R | 19 +++++----- R/utils-data_constraints.r | 2 +- R/utils.R | 51 +++++++++++++++++++++++++++ man/report_card_template_goshawk.Rd | 38 ++++++++++++++++++++ 10 files changed, 182 insertions(+), 70 deletions(-) create mode 100644 man/report_card_template_goshawk.Rd diff --git a/.lintr b/.lintr index 34473d27..0a0bb22f 100644 --- a/.lintr +++ b/.lintr @@ -1,5 +1,6 @@ linters: linters_with_defaults( line_length_linter = line_length_linter(120), cyclocomp_linter = NULL, - object_usage_linter = NULL + object_usage_linter = NULL, + indentation_linter = NULL ) diff --git a/R/tm_g_gh_boxplot.R b/R/tm_g_gh_boxplot.R index 08187005..0ab843d3 100644 --- a/R/tm_g_gh_boxplot.R +++ b/R/tm_g_gh_boxplot.R @@ -548,20 +548,24 @@ srv_g_boxplot <- function(id, ### REPORTER if (with_reporter) { - card_fun <- function(comment) { - card <- teal::TealReportCard$new() - card$set_name("Box Plot") - card$append_text("Box Plot", "header2") - if (with_filter) card$append_fs(filter_panel_api$get_filter_state()) - card$append_text("Selected Options", "header3") - card$append_text( - paste( - formatted_data_constraint(input$constraint_var, input$constraint_range_min, input$constraint_range_max), - "\nFacet By:", - if (length(input$facet_var) != 0) input$facet_var else "None", - "\nSelect an X-axis Variable:", - input$xaxis_var + card_fun <- function(comment, label) { + constraint_description <- paste( + "\nFacet By:", + if (length(input$facet_var) != 0) input$facet_var else "None", + "\nSelect an X-axis Variable:", + input$xaxis_var + ) + card <- report_card_template_goshawk( + title = "Box Plot", + label = label, + with_filter = with_filter, + filter_panel_api = filter_panel_api, + constraint_list = list( + constraint_var = input$constraint_var, + constraint_range_min = input$constraint_range_min, + constraint_range_max = input$constraint_range_max ), + constraint_description = constraint_description, style = "verbatim" ) card$append_text("Plot", "header3") diff --git a/R/tm_g_gh_correlationplot.R b/R/tm_g_gh_correlationplot.R index 4d7d267d..0c99a31e 100644 --- a/R/tm_g_gh_correlationplot.R +++ b/R/tm_g_gh_correlationplot.R @@ -875,20 +875,24 @@ srv_g_correlationplot <- function(id, ### REPORTER if (with_reporter) { - card_fun <- function(comment) { - card <- teal::TealReportCard$new() - card$set_name("Correlation Plot") - card$append_text("Correlation Plot", "header2") - if (with_filter) card$append_fs(filter_panel_api$get_filter_state()) - card$append_text("Selected Options", "header3") - card$append_text( - paste( - formatted_data_constraint(input$constraint_var, input$constraint_range_min, input$constraint_range_max), - "\nTreatment Variable Faceting:", - input$trt_facet, - "\nRegression Line:", - input$reg_line + card_fun <- function(comment, label) { + constraint_description <- paste( + "\nTreatment Variable Faceting:", + input$trt_facet, + "\nRegression Line:", + input$reg_line + ) + card <- report_card_template_goshawk( + title = "Correlation Plot", + label = label, + with_filter = with_filter, + filter_panel_api = filter_panel_api, + constraint_list = list( + constraint_var = input$constraint_var, + constraint_range_min = input$constraint_range_min, + constraint_range_max = input$constraint_range_max ), + constraint_description = constraint_description, style = "verbatim" ) card$append_text("Plot", "header3") diff --git a/R/tm_g_gh_density_distribution_plot.R b/R/tm_g_gh_density_distribution_plot.R index 8ec4e5c0..bdd062b8 100644 --- a/R/tm_g_gh_density_distribution_plot.R +++ b/R/tm_g_gh_density_distribution_plot.R @@ -449,14 +449,17 @@ srv_g_density_distribution_plot <- function(id, # nolint ### REPORTER if (with_reporter) { - card_fun <- function(comment) { - card <- teal::TealReportCard$new() - card$set_name("Density Distribution Plot") - card$append_text("Density Distribution Plot", "header2") - if (with_filter) card$append_fs(filter_panel_api$get_filter_state()) - card$append_text("Selected Options", "header3") - card$append_text( - formatted_data_constraint(input$constraint_var, input$constraint_range_min, input$constraint_range_max) + card_fun <- function(comment, label) { + card <- report_card_template_goshawk( + title = "Density Distribution Plot", + label = label, + with_filter = with_filter, + filter_panel_api = filter_panel_api, + constraint_list = list( + constraint_var = input$constraint_var, + constraint_range_min = input$constraint_range_min, + constraint_range_max = input$constraint_range_max + ) ) card$append_text("Plot", "header3") card$append_plot(plot_r(), dim = plot_data$dim()) diff --git a/R/tm_g_gh_lineplot.R b/R/tm_g_gh_lineplot.R index e3c8e07e..564afcf0 100644 --- a/R/tm_g_gh_lineplot.R +++ b/R/tm_g_gh_lineplot.R @@ -793,20 +793,24 @@ srv_lineplot <- function(id, ### REPORTER if (with_reporter) { - card_fun <- function(comment) { - card <- teal::TealReportCard$new() - card$set_name("Line Plot") - card$append_text("Line Plot", "header2") - if (with_filter) card$append_fs(filter_panel_api$get_filter_state()) - card$append_text("Selected Options", "header3") - card$append_text( - paste( - formatted_data_constraint(input$constraint_var, input$constraint_range_min, input$constraint_range_max), - "\nSelect Line Splitting Variable:", - if (!is.null(input$shape)) input$shape else "None", - "\nContributing Observations Threshold:", - input$count_threshold + card_fun <- function(comment, label) { + constraint_description <- paste( + "\nSelect Line Splitting Variable:", + if (!is.null(input$shape)) input$shape else "None", + "\nContributing Observations Threshold:", + input$count_threshold + ) + card <- report_card_template_goshawk( + title = "Line Plot", + label = label, + with_filter = with_filter, + filter_panel_api = filter_panel_api, + constraint_list = list( + constraint_var = input$constraint_var, + constraint_range_min = input$constraint_range_min, + constraint_range_max = input$constraint_range_max ), + constraint_description = constraint_description, style = "verbatim" ) card$append_text("Plot", "header3") diff --git a/R/tm_g_gh_scatterplot.R b/R/tm_g_gh_scatterplot.R index b2d94b1c..752dae0e 100644 --- a/R/tm_g_gh_scatterplot.R +++ b/R/tm_g_gh_scatterplot.R @@ -382,20 +382,24 @@ srv_g_scatterplot <- function(id, ### REPORTER if (with_reporter) { - card_fun <- function(comment) { - card <- teal::TealReportCard$new() - card$set_name("Scatter Plot") - card$append_text("Scatter Plot", "header2") - if (with_filter) card$append_fs(filter_panel_api$get_filter_state()) - card$append_text("Selected Options", "header3") - card$append_text( - paste( - formatted_data_constraint(input$constraint_var, input$constraint_range_min, input$constraint_range_max), - "\nTreatment Variable Faceting:", - input$trt_facet, - "\nRegression Line:", - input$reg_line + card_fun <- function(comment, label) { + constraint_description <- paste( + "\nTreatment Variable Faceting:", + input$trt_facet, + "\nRegression Line:", + input$reg_line + ) + card <- report_card_template_goshawk( + title = "Scatter Plot", + label = label, + with_filter = with_filter, + filter_panel_api = filter_panel_api, + constraint_list = list( + constraint_var = input$constraint_var, + constraint_range_min = input$constraint_range_min, + constraint_range_max = input$constraint_range_max ), + constraint_description = constraint_description, style = "verbatim" ) card$append_text("Scatter Plot", "header3") diff --git a/R/tm_g_gh_spaghettiplot.R b/R/tm_g_gh_spaghettiplot.R index 8361f542..0add074f 100644 --- a/R/tm_g_gh_spaghettiplot.R +++ b/R/tm_g_gh_spaghettiplot.R @@ -521,14 +521,17 @@ srv_g_spaghettiplot <- function(id, ### REPORTER if (with_reporter) { - card_fun <- function(comment) { - card <- teal::TealReportCard$new() - card$set_name("Spaghetti Plot") - card$append_text("Spaghetti Plot", "header2") - if (with_filter) card$append_fs(filter_panel_api$get_filter_state()) - card$append_text("Selected Options", "header3") - card$append_text( - formatted_data_constraint(input$constraint_var, input$constraint_range_min, input$constraint_range_max) + card_fun <- function(comment, label) { + card <- report_card_template_goshawk( + title = "Spaghetti Plot", + label = label, + with_filter = with_filter, + filter_panel_api = filter_panel_api, + constraint_list = list( + constraint_var = input$constraint_var, + constraint_range_min = input$constraint_range_min, + constraint_range_max = input$constraint_range_max + ) ) card$append_text("Spaghetti Plot", "header3") card$append_plot(plot_r(), dim = plot_data$dim()) diff --git a/R/utils-data_constraints.r b/R/utils-data_constraints.r index ae521c11..5df85821 100644 --- a/R/utils-data_constraints.r +++ b/R/utils-data_constraints.r @@ -236,7 +236,7 @@ create_anl_constraint_reactive <- function(anl_param, input, param_id, min_rows) ) } -# for outputting the constaint in the report +# for outputting the constraint in the report formatted_data_constraint <- function(constraint_var, constraint_range_min, constraint_range_max) { constraint_var_label <- switch(constraint_var, "BASE2" = "Screening", diff --git a/R/utils.R b/R/utils.R index 644f7132..6a881b39 100644 --- a/R/utils.R +++ b/R/utils.R @@ -31,3 +31,54 @@ plots_per_row_validate_rules <- function(required = TRUE) { shinyvalidate::sv_gt(0, message_fmt = msg) ) } + +#' Template Function for `TealReportCard` Creation and Customization in `teal.goshawk` +#' +#' This function generates a report card with a title, +#' an optional description, and the option to append the filter state list. +#' Additionally, it display selected constraint options. +#' +#' @inheritParams teal.reporter::card_template +#' @param constraint_list (`list`) a list containing constraint variables, including: +#' - constraint_var (`character(1)`) the constraint variable name. +#' - constraint_range_min (`numeric(1)`) the minimum constraint range value. +#' - constraint_range_max (`numeric(1)`) the maximum constraint range value. +#' @param constraint_description (`character(1)`) description of the constraints. +#' @param style (`character(1)`) style of the constraint text block. +#' options: `default`, `verbatim` (default is `default`). +#' +#' @return (`TealReportCard`) populated with a title, description, and filter state +#' +#' @keywords internal +report_card_template_goshawk <- function(title, + label, + with_filter, + filter_panel_api, + constraint_list, + constraint_description = NULL, + style = "default") { + checkmate::assert_subset(names(constraint_list), c("constraint_var", "constraint_range_min", "constraint_range_max")) + checkmate::assert_string(constraint_description, null.ok = TRUE) + checkmate::assert_choice(style, c("default", "verbatim")) + + card <- teal::report_card_template( + title = title, + label = label, + with_filter = with_filter, + filter_panel_api = filter_panel_api + ) + + card$append_text("Selected Options", "header3") + card$append_text( + paste( + formatted_data_constraint( + constraint_list$constraint_var, + constraint_list$constraint_range_min, + constraint_list$constraint_range_max + ), + constraint_description + ), + style = style + ) + card +} diff --git a/man/report_card_template_goshawk.Rd b/man/report_card_template_goshawk.Rd new file mode 100644 index 00000000..c5101303 --- /dev/null +++ b/man/report_card_template_goshawk.Rd @@ -0,0 +1,38 @@ +% Generated by roxygen2: do not edit by hand +% Please edit documentation in R/utils.R +\name{report_card_template_goshawk} +\alias{report_card_template_goshawk} +\title{Template Function for \code{TealReportCard} Creation and Customization in \code{teal.goshawk}} +\usage{ +report_card_template_goshawk( + title, + label, + with_filter, + filter_panel_api, + constraint_list, + constraint_description = NULL, + style = "default" +) +} +\arguments{ +\item{constraint_list}{(\code{list}) a list containing constraint variables, including: +\itemize{ +\item constraint_var (\code{character(1)}) the constraint variable name. +\item constraint_range_min (\code{numeric(1)}) the minimum constraint range value. +\item constraint_range_max (\code{numeric(1)}) the maximum constraint range value. +}} + +\item{constraint_description}{(\code{character(1)}) description of the constraints.} + +\item{style}{(\code{character(1)}) style of the constraint text block. +options: \code{default}, \code{verbatim} (default is \code{default}).} +} +\value{ +(\code{TealReportCard}) populated with a title, description, and filter state +} +\description{ +This function generates a report card with a title, +an optional description, and the option to append the filter state list. +Additionally, it display selected constraint options. +} +\keyword{internal}