From 6da9ea12f7955055b75b84c35ead3c87f3374501 Mon Sep 17 00:00:00 2001 From: Mateusz Nizwantowski Date: Thu, 24 Oct 2024 16:42:19 +0200 Subject: [PATCH] feat: additional notes in report (#192) * inital draft of notes * implement additional notes in template * implement in package code * tests and multiline support * improved docs and new test * typos fix * docs rerun --- R/generate_report.R | 16 ++++++++++++---- R/get-nmfi.R | 2 +- .../templates/levey_jennings_report_template.Rmd | 2 ++ inst/templates/plate_report_template.Rmd | 13 +++++++++++-- man/generate_plate_report.Rd | 10 ++++++++-- man/get_nmfi.Rd | 2 +- tests/testthat/test-generate_report.R | 4 ++++ 7 files changed, 39 insertions(+), 10 deletions(-) diff --git a/R/generate_report.R b/R/generate_report.R index 577899eb..200758e3 100644 --- a/R/generate_report.R +++ b/R/generate_report.R @@ -10,6 +10,7 @@ #' @param output_dir (`character(1)`) The directory where the report should be saved. Default is 'reports'. #' @param counts_lower_threshold (`numeric(1)`) The lower threshold for the counts plots (works for each analyte). Default is 50. #' @param counts_higher_threshold (`numeric(1)`) The higher threshold for the counts plots (works for each analyte). Default is 70. +#' @param additional_notes (`character(1)`) Additional notes to be included in the report. Contents of this fields are left to the user's discretion. If not provided, the field will not be included in the report. #' #' #' @return A report. @@ -18,15 +19,18 @@ #' #' plate_file <- system.file("extdata", "CovidOISExPONTENT.csv", package = "PvSTATEM") #' layout_file <- system.file("extdata", "CovidOISExPONTENT_layout.csv", package = "PvSTATEM") +#' note <- "This is a test report.\n**Author**: Jane Doe \n**Tester**: John Doe" #' #' plate <- read_luminex_data(plate_file, layout_file) #' tmp_dir <- tempdir(check = TRUE) #' generate_plate_report(plate, #' output_dir = tmp_dir, -#' counts_lower_threshold = 40, counts_higher_threshold = 50 +#' counts_lower_threshold = 40, +#' counts_higher_threshold = 50, +#' additional_notes = note #' ) #' @export -generate_plate_report <- function(plate, use_model = TRUE, filename = NULL, output_dir = "reports", counts_lower_threshold = 50, counts_higher_threshold = 70) { +generate_plate_report <- function(plate, use_model = TRUE, filename = NULL, output_dir = "reports", counts_lower_threshold = 50, counts_higher_threshold = 70, additional_notes = NULL) { message("Generating report... This will take approximately 30 seconds.") output_file <- if (is.null(filename)) { paste0(plate$plate_name, "_report.html") @@ -34,12 +38,16 @@ generate_plate_report <- function(plate, use_model = TRUE, filename = NULL, outp filename } - template_path <- system.file("templates", "plate_report_template.Rmd", package = "PvSTATEM", mustWork = TRUE) + # markdown does not support single line breaks, so we need to replace them with two spaces and a line break + if (!is.null(additional_notes)) { + additional_notes <- gsub(pattern = "\n", replacement = " \n", x = additional_notes) + } + rmarkdown::render( template_path, - params = list(plate = plate, use_model = use_model, counts_lower_threshold = counts_lower_threshold, counts_higher_threshold = counts_higher_threshold), + params = list(plate = plate, use_model = use_model, counts_lower_threshold = counts_lower_threshold, counts_higher_threshold = counts_higher_threshold, additional_notes = additional_notes), output_file = output_file, output_dir = output_dir, quiet = TRUE diff --git a/R/get-nmfi.R b/R/get-nmfi.R index 1aaf3666..02b34735 100644 --- a/R/get-nmfi.R +++ b/R/get-nmfi.R @@ -41,7 +41,7 @@ #' # calculate the nMFI values #' nmfi <- get_nmfi(plate, reference_dilution = 1 / 400) #' -#' # we don't do any extrapolation and the values should be comparable accross plates +#' # we don't do any extrapolation and the values should be comparable across plates #' head(nmfi) #' # different params #' nmfi <- get_nmfi(plate, reference_dilution = "1/50") diff --git a/inst/templates/levey_jennings_report_template.Rmd b/inst/templates/levey_jennings_report_template.Rmd index af144017..4a85645a 100644 --- a/inst/templates/levey_jennings_report_template.Rmd +++ b/inst/templates/levey_jennings_report_template.Rmd @@ -4,6 +4,8 @@ output: html_document params: list_of_plates: !r NULL --- +Report generated on: `r format(Sys.time(), "%d-%m-%Y %H:%M:%S")`. +------------------ ###   diff --git a/inst/templates/plate_report_template.Rmd b/inst/templates/plate_report_template.Rmd index 388df945..68b1adb9 100644 --- a/inst/templates/plate_report_template.Rmd +++ b/inst/templates/plate_report_template.Rmd @@ -6,6 +6,7 @@ params: use_model: !r NULL counts_lower_threshold: !r NULL counts_higher_threshold: !r NULL + additional_notes: !r NULL --- ```{r param-check, echo=FALSE} @@ -33,6 +34,9 @@ if (!is(params$counts_lower_threshold, "numeric")) { if (!is(params$counts_higher_threshold, "numeric")) { stop("The `counts_higher_threshold` must be a numeric value.") } +if (!is.null(params$additional_notes) && !is(params$additional_notes, "character")) { + stop("The `additional_notes` must be a character value.") +} ``` Report generated on: `r format(Sys.time(), "%d-%m-%Y %H:%M:%S")`. @@ -41,8 +45,13 @@ Plate batch name: `r if(!is.null(params$plate$batch_name)) params$plate$batch_na This is plate with `r paste0(length(params$plate$sample_names), " samples and ", length(params$plate$analyte_names), " analytes")`. Standard curve sample dilutions: `r format_dilutions(params$plate$dilutions, params$plate$dilution_values, params$plate$sample_types)`. +------------------ + +`r if(!is.null(params$additional_notes)) "### Additional notes \n" ` + +`r if(!is.null(params$additional_notes)) params$additional_notes ` -------------------- +`r if(!is.null(params$additional_notes)) "\n -------------------" ` ### Plate layout @@ -177,7 +186,7 @@ cat('') ------------------ -### Details for given analayte {.tabset .tabset-fade} +### Details for given analyte {.tabset .tabset-fade} ```{r quality-control, results='asis', echo=FALSE, message=FALSE, out.width="50%", dev='jpeg', dpi=72} # Code used to create dynamic tabs based on the number of analytes diff --git a/man/generate_plate_report.Rd b/man/generate_plate_report.Rd index b95da485..a4549666 100644 --- a/man/generate_plate_report.Rd +++ b/man/generate_plate_report.Rd @@ -10,7 +10,8 @@ generate_plate_report( filename = NULL, output_dir = "reports", counts_lower_threshold = 50, - counts_higher_threshold = 70 + counts_higher_threshold = 70, + additional_notes = NULL ) } \arguments{ @@ -25,6 +26,8 @@ generate_plate_report( \item{counts_lower_threshold}{(\code{numeric(1)}) The lower threshold for the counts plots (works for each analyte). Default is 50.} \item{counts_higher_threshold}{(\code{numeric(1)}) The higher threshold for the counts plots (works for each analyte). Default is 70.} + +\item{additional_notes}{(\code{character(1)}) Additional notes to be included in the report. Contents of this fields are left to the user's discretion. If not provided, the field will not be included in the report.} } \value{ A report. @@ -37,11 +40,14 @@ The report is generated using the \code{plate_report_template.Rmd} template. plate_file <- system.file("extdata", "CovidOISExPONTENT.csv", package = "PvSTATEM") layout_file <- system.file("extdata", "CovidOISExPONTENT_layout.csv", package = "PvSTATEM") +note <- "This is a test report.\n**Author**: Jane Doe \n**Tester**: John Doe" plate <- read_luminex_data(plate_file, layout_file) tmp_dir <- tempdir(check = TRUE) generate_plate_report(plate, output_dir = tmp_dir, - counts_lower_threshold = 40, counts_higher_threshold = 50 + counts_lower_threshold = 40, + counts_higher_threshold = 50, + additional_notes = note ) } diff --git a/man/get_nmfi.Rd b/man/get_nmfi.Rd index 3c9c4a6a..fbd771ad 100644 --- a/man/get_nmfi.Rd +++ b/man/get_nmfi.Rd @@ -55,7 +55,7 @@ plate$data[["Median"]][plate$sample_types == "TEST", ] <- # calculate the nMFI values nmfi <- get_nmfi(plate, reference_dilution = 1 / 400) -# we don't do any extrapolation and the values should be comparable accross plates +# we don't do any extrapolation and the values should be comparable across plates head(nmfi) # different params nmfi <- get_nmfi(plate, reference_dilution = "1/50") diff --git a/tests/testthat/test-generate_report.R b/tests/testthat/test-generate_report.R index 1b0870c9..8ce3b66a 100644 --- a/tests/testthat/test-generate_report.R +++ b/tests/testthat/test-generate_report.R @@ -30,6 +30,10 @@ test_that("Test generate_plate_report function", { expect_no_error(generate_plate_report(plate)) expect_no_error(generate_plate_report(plate, filename = "test_report.html")) expect_no_error(generate_plate_report(plate, counts_lower_threshold = 40, counts_higher_threshold = 80)) + expect_no_error(generate_plate_report(plate, additional_notes = "This is a test report.")) + + multiline_note <- "This is a test report.\nThis is a test report." + expect_no_error(generate_plate_report(plate, additional_notes = multiline_note)) }) test_that("Test generate_levey_jennings_report function", {