Skip to content

Commit

Permalink
files and NEWS
Browse files Browse the repository at this point in the history
  • Loading branch information
andersone1 committed Oct 31, 2024
1 parent 4274916 commit a788434
Show file tree
Hide file tree
Showing 5 changed files with 140 additions and 0 deletions.
2 changes: 2 additions & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
52 changes: 52 additions & 0 deletions R/renderQCReport.R
Original file line number Diff line number Diff line change
@@ -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)
}

}
40 changes: 40 additions & 0 deletions inst/templates/QCReport.Rmd
Original file line number Diff line number Diff line change
@@ -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()
```
30 changes: 30 additions & 0 deletions man/renderQCReport.Rd

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

16 changes: 16 additions & 0 deletions tests/testthat/test-renderQCReport.R
Original file line number Diff line number Diff line change
@@ -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"))))

})
}

0 comments on commit a788434

Please sign in to comment.