Skip to content

Commit

Permalink
feat: additional notes in report (#192)
Browse files Browse the repository at this point in the history
* 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
  • Loading branch information
nizwant authored Oct 24, 2024
1 parent bad7da7 commit 6da9ea1
Show file tree
Hide file tree
Showing 7 changed files with 39 additions and 10 deletions.
16 changes: 12 additions & 4 deletions R/generate_report.R
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -18,28 +19,35 @@
#'
#' 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")
} else {
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
Expand Down
2 changes: 1 addition & 1 deletion R/get-nmfi.R
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand Down
2 changes: 2 additions & 0 deletions inst/templates/levey_jennings_report_template.Rmd
Original file line number Diff line number Diff line change
Expand Up @@ -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")`.
------------------

### &nbsp;

Expand Down
13 changes: 11 additions & 2 deletions inst/templates/plate_report_template.Rmd
Original file line number Diff line number Diff line change
Expand Up @@ -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}
Expand Down Expand Up @@ -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")) {

Check warning on line 37 in inst/templates/plate_report_template.Rmd

View workflow job for this annotation

GitHub Actions / lint

file=inst/templates/plate_report_template.Rmd,line=37,col=81,[line_length_linter] Lines should not be more than 80 characters. This line is 85 characters.

Check warning on line 37 in inst/templates/plate_report_template.Rmd

View workflow job for this annotation

GitHub Actions / lint

file=inst/templates/plate_report_template.Rmd,line=37,col=81,[line_length_linter] Lines should not be more than 80 characters. This line is 85 characters.
stop("The `additional_notes` must be a character value.")
}
```

Report generated on: `r format(Sys.time(), "%d-%m-%Y %H:%M:%S")`.
Expand All @@ -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
Expand Down Expand Up @@ -177,7 +186,7 @@ cat('</div>')

------------------

### 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
Expand Down
10 changes: 8 additions & 2 deletions man/generate_plate_report.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion man/get_nmfi.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 4 additions & 0 deletions tests/testthat/test-generate_report.R
Original file line number Diff line number Diff line change
Expand Up @@ -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", {
Expand Down

0 comments on commit 6da9ea1

Please sign in to comment.