diff --git a/NEWS.md b/NEWS.md index 9399e77..d95a44c 100644 --- a/NEWS.md +++ b/NEWS.md @@ -2,6 +2,8 @@ ## New features and changes +- `renderQCReport` Render a QC Report Document. (#135) + - `logRemove` added to package to assist with removing files from the QC log. (#105) - `diffFiles` can now display the entire file that is being diffed. (#115) diff --git a/R/renderQCReport.R b/R/renderQCReport.R new file mode 100644 index 0000000..e8f3bfa --- /dev/null +++ b/R/renderQCReport.R @@ -0,0 +1,52 @@ +#' Render a QC Report Document +#' +#' This function generates a QC report based on the files within the QC log +#' and renders it into a PDF report. +#' +#' The QC status of all files in the QC log will be displayed. +#' +#' @param .output_dir Character string (optional). Path to the directory where the output PDF +#' should be saved. If not provided, the document will not be saved locally. +#' +#' @return Invisible. The function will save a PDF report named "qc-report-(current date).pdf" +#' to the specified output directory (if available). If the R session is interactive, it will also +#' open the PDF in the default browser. +#' +#' @examples +#' \dontrun{ +#' renderQCReport() +#' } +#' +#' @export +renderQCReport <- function(.output_dir = NULL) { + + reportRes <- list() + + reportRes$project <- basename(logRoot()) + + reportRes$logSum <- logSummary() + + if (is.null(.output_dir)) { + .output_dir <- tempdir() + } else { + if (!dir.exists(.output_dir)) { + stop(.output_dir, " does not exist") + } + } + + output_file <- paste0("qc-report-", Sys.Date(), ".pdf") + output_path <- file.path(.output_dir, output_file) + + rmarkdown::render( + input = system.file("templates", "QCReport.Rmd", package = "review"), + output_file = output_path, + params = list(reportRes = reportRes), + envir = new.env(), + quiet = TRUE + ) + + if (interactive()) { + utils::browseURL(output_path) + } + +} diff --git a/inst/templates/QCReport.Rmd b/inst/templates/QCReport.Rmd new file mode 100644 index 0000000..6830ed6 --- /dev/null +++ b/inst/templates/QCReport.Rmd @@ -0,0 +1,40 @@ +--- +params: + reportRes: "`r list()`" +title: "QC Report" +subtitle: "`r params$reportRes$project`" +date: "`r format(Sys.time(), '%d %B, %Y %H:%M %Z')`" +output: pdf_document +header-includes: + - \usepackage{caption} +--- + +```{r setup, include=FALSE} +knitr::opts_chunk$set(echo = FALSE) +``` + +```{r message=FALSE, warning=FALSE} + +log_summary_data <- params$reportRes$logSum + +log_summary_data %>% + dplyr::select(file, revf, headf, time) %>% + dplyr::mutate( + Status = dplyr::case_when( + revf == headf ~ "QCed", + TRUE ~ "Needs QC" + ), + `QC date` = dplyr::if_else(revf == headf, time, NA_character_), + `QC date` = as.Date(`QC date`) + ) %>% + dplyr::select(-time) %>% + dplyr::rename( + File = file, + `QCed...revision` = revf, + `Last modified...revision` = headf, + ) %>% + pmtables::st_new() %>% + pmtables::st_left(File = pmtables::col_ragged(5)) %>% + pmtables::stable_long() %>% + pmtables::st_asis() +``` diff --git a/man/renderQCReport.Rd b/man/renderQCReport.Rd new file mode 100644 index 0000000..310dc97 --- /dev/null +++ b/man/renderQCReport.Rd @@ -0,0 +1,30 @@ +% Generated by roxygen2: do not edit by hand +% Please edit documentation in R/renderQCReport.R +\name{renderQCReport} +\alias{renderQCReport} +\title{Render a QC Report Document} +\usage{ +renderQCReport(.output_dir = NULL) +} +\arguments{ +\item{.output_dir}{Character string (optional). Path to the directory where the output PDF +should be saved. If not provided, the document will not be saved locally.} +} +\value{ +Invisible. The function will save a PDF report named "qc-report-(current date).pdf" +to the specified output directory (if available). If the R session is interactive, it will also +open the PDF in the default browser. +} +\description{ +This function generates a QC report based on the files within the QC log +and renders it into a PDF report. +} +\details{ +The QC status of all files in the QC log will be displayed. +} +\examples{ +\dontrun{ +renderQCReport() +} + +} diff --git a/tests/testthat/test-renderQCReport.R b/tests/testthat/test-renderQCReport.R new file mode 100644 index 0000000..054bac8 --- /dev/null +++ b/tests/testthat/test-renderQCReport.R @@ -0,0 +1,16 @@ +if (Sys.getenv("METWORX_VERSION") != "") { + + create_test_svn() + + logAssign("script/model-management.R") + logAccept("script/box-sample-code.R") + + test_that("renderQCReport works with valid directory", { + + renderQCReport(.output_dir = logRoot()) + + # Check that the output file was created + expect_true(file.exists(file.path(logRoot(), paste0("qc-report-", Sys.Date(), ".pdf")))) + + }) +}