Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
File renamed without changes.
10 changes: 6 additions & 4 deletions DESCRIPTION
Original file line number Diff line number Diff line change
Expand Up @@ -39,15 +39,17 @@ Imports:
tidyselect,
plotly,
scico,
magick (>= 2.9.0),
tinytex (>= 0.58)
rmarkdown
Suggests:
covr,
testthat (>= 3.0.0),
withr,
spelling,
knitr,
rmarkdown,
spelling
magick (>= 2.9.0),
tinytex (>= 0.58),
gridExtra,
grid
Config/testthat/edition: 3
URL: https://cabajr.github.io/ClockCyteR.spatial/
Language: en-US
87 changes: 54 additions & 33 deletions R/modules_foo.R
Original file line number Diff line number Diff line change
Expand Up @@ -1121,11 +1121,16 @@ validate_params <- function(p) {

#' generate_reports
#'
#' Renders a per-file HTML or PDF report for each file in the project,
#' collecting all plot types across intervals and channels.
#' Renders a per-file report for each file in the project, collecting all plot
#' types across intervals and channels. Output is HTML by default (no system
#' dependencies); set \code{pdf = TRUE} for PDF output (requires additional
#' system dependencies — see Details).
#'
#' @param params A named list of analysis parameters as produced by \code{make_params()}.
#' @param file_rows A tibble of file metadata as produced by \code{index_files()}.
#' @param pdf Logical; if \code{TRUE} render PDF reports instead of HTML.
#' Requires \pkg{magick}, \pkg{tinytex}, and a TinyTeX installation
#' (\code{tinytex::install_tinytex()}). Defaults to \code{FALSE}.
#'
#' @return Called for its side effects (report files written to disk). Returns
#' \code{NULL} invisibly.
Expand All @@ -1134,80 +1139,96 @@ validate_params <- function(p) {
#' @examples
#' \dontrun{
#' generate_reports(params, file_rows)
#' generate_reports(params, file_rows, pdf = TRUE)
#' }
generate_reports <- function(params, file_rows) {
intervals <- names(params$time$intervals)
channels <- names(params$channels)
plot_types <- c(params$plotting$plot_types, params$plotting$network_plot_types)
base_dir <- params$paths$base_dir
generate_reports <- function(params, file_rows, pdf = FALSE) {
intervals <- names(params$time$intervals)
channels <- names(params$channels)
plot_types <- c(params$plotting$plot_types, params$plotting$network_plot_types)
base_dir <- params$paths$base_dir
reports_dir <- file.path(base_dir, "reports")

if (!dir.exists(reports_dir)) dir.create(reports_dir)

n_files <- length(file_rows$file_id)
pb_id <- cli::cli_progress_bar("Generating reports", total = n_files)
pb_id <- cli::cli_progress_bar("Generating reports", total = n_files)

purrr::walk(file_rows$file_id, function(fid){
purrr::walk(file_rows$file_id, function(fid) {
generate_file_report(
file_id =fid,
params = params,
base_dir = base_dir,
intervals = intervals,
channels = channels,
plot_types = plot_types
)
file_id = fid,
params = params,
base_dir = base_dir,
intervals = intervals,
channels = channels,
plot_types = plot_types,
pdf = pdf
)
cli::cli_progress_update(id = pb_id)
})
})

cli::cli_progress_done(id = pb_id)
message("Reports generated.")
}

#' generate_plot_type_reports
#'
#' Renders one PDF report per plot type, collating the corresponding plot
#' across all files, intervals, and channels using an Rmd template.
#' Renders one report per plot type, collating the corresponding plot across
#' all files, intervals, and channels using an Rmd template. Output is HTML
#' by default; set \code{pdf = TRUE} for PDF output (requires additional
#' system dependencies — see Details).
#'
#' @param params A named list of analysis parameters as produced by \code{make_params()}.
#' @param file_rows A tibble of file metadata as produced by \code{index_files()}.
#' @param storage_fold Character string naming the subfolder within each
#' file's results directory where plots are stored. Defaults to
#' \code{"plots"}.
#' @param plot_report_template Character string giving the filename of the Rmd
#' template to use. Defaults to \code{"plot_type_report_template.Rmd"}.
#' @param plot_report_dir Character string giving the output directory for
#' rendered reports. Defaults to \code{file.path(params$paths$base_dir,
#' "reports", "plot_reports")}.
#' @param pdf Logical; if \code{TRUE} render PDF reports instead of HTML.
#' Requires \pkg{magick}, \pkg{tinytex}, and a TinyTeX installation
#' (\code{tinytex::install_tinytex()}). Defaults to \code{FALSE}.
#'
#' @return Called for its side effects (PDF reports written to
#' @return Called for its side effects (reports written to
#' \code{plot_report_dir}). Returns \code{NULL} invisibly.
#' @export
#'
#' @examples
#' \dontrun{
#' generate_plot_type_reports(params, file_rows)
#' generate_plot_type_reports(params, file_rows, pdf = TRUE)
#' }
generate_plot_type_reports <- function(
params,
file_rows,
storage_fold = "plots",
plot_report_template = "plot_type_report_template.Rmd",
plot_report_dir = file.path(params$paths$base_dir, "reports", "plot_reports")
storage_fold = "plots",
plot_report_dir = file.path(params$paths$base_dir, "reports", "plot_reports"),
pdf = FALSE
) {
intervals <- names(params$time$intervals)
channels <- names(params$channels)
plot_types <- c(params$plotting$plot_types, params$plotting$network_plot_types)
base_dir <- params$paths$base_dir
if (pdf) {
check_pdf_deps()
plot_report_template <- "plot_type_report_template_pdf.Rmd"
output_ext <- ".pdf"
} else {
plot_report_template <- "plot_type_report_template.Rmd"
output_ext <- ".html"
}

intervals <- names(params$time$intervals)
channels <- names(params$channels)
plot_types <- c(params$plotting$plot_types, params$plotting$network_plot_types)
base_dir <- params$paths$base_dir

if (!dir.exists(plot_report_dir)) dir.create(plot_report_dir, recursive = TRUE)

n_plot_types <- length(plot_types)
pb_id <- cli::cli_progress_bar("Generating plot-type reports", total = n_plot_types)
pb_id <- cli::cli_progress_bar("Generating plot-type reports", total = n_plot_types)

purrr::walk(plot_types, function(pt) {
rmarkdown::render(
input = system.file("rmd", plot_report_template, package = "ClockCyteR.spatial"),
output_file = file.path(plot_report_dir, paste0("plot_report_", pt, ".pdf")),
params = list(
input = system.file("rmd", plot_report_template, package = "ClockCyteR.spatial"),
output_file = file.path(plot_report_dir, paste0("plot_report_", pt, output_ext)),
params = list(
channel_params = params$channels,
file_rows = file_rows,
base_dir = base_dir,
Expand Down
49 changes: 43 additions & 6 deletions R/utils_foo.R
Original file line number Diff line number Diff line change
Expand Up @@ -248,6 +248,34 @@ extract_vars <- function(params, file_rows, vars, network = FALSE, individual =

}

check_pdf_deps <- function() {
missing_pkgs <- character(0)
if (!requireNamespace("magick", quietly = TRUE)) missing_pkgs <- c(missing_pkgs, "magick")
if (!requireNamespace("tinytex", quietly = TRUE)) missing_pkgs <- c(missing_pkgs, "tinytex")
if (!requireNamespace("gridExtra", quietly = TRUE)) missing_pkgs <- c(missing_pkgs, "gridExtra")

if (length(missing_pkgs) > 0) {
stop(
"PDF report generation requires additional packages: ",
paste(missing_pkgs, collapse = ", "), ".\n",
"Install them with: install.packages(c(",
paste0('"', missing_pkgs, '"', collapse = ", "), "))",
call. = FALSE
)
}

if (!tinytex::is_tinytex()) {
stop(
"TinyTeX (LaTeX) is not installed. PDF generation requires it.\n",
"Install it with: tinytex::install_tinytex()\n",
"Or use the default HTML output by leaving pdf = FALSE.",
call. = FALSE
)
}

invisible(TRUE)
}

#' Render an Rmd report for a single file
#'
#' @param file_id Character; file identifier.
Expand All @@ -259,12 +287,13 @@ extract_vars <- function(params, file_rows, vars, network = FALSE, individual =
#' report.
#' @param storage_fold Character; subdirectory containing the plots. Defaults
#' to \code{"plots"}.
#' @param report_template Character; filename of the Rmd template. Defaults
#' to \code{"report_template.Rmd"}.
#' @param output_dir Character; directory where the rendered report is saved.
#' Defaults to a \code{reports} subdirectory inside \code{base_dir}.
#' @param pdf Logical; if \code{TRUE} render a PDF instead of HTML. Requires
#' \pkg{magick}, \pkg{tinytex}, and a TinyTeX installation. Defaults to
#' \code{FALSE}.
#'
#' @return Called for its side effect of rendering an HTML report; returns
#' @return Called for its side effect of rendering a report; returns
#' the output file path invisibly.
#' @keywords internal
generate_file_report <- function(
Expand All @@ -275,9 +304,17 @@ generate_file_report <- function(
channels,
plot_types,
storage_fold = "plots",
report_template = "report_template.Rmd",
output_dir = file.path(base_dir, "reports")
output_dir = file.path(base_dir, "reports"),
pdf = FALSE
) {
if (pdf) {
check_pdf_deps()
report_template <- "report_template_pdf.Rmd"
output_ext <- ".pdf"
} else {
report_template <- "report_template.Rmd"
output_ext <- ".html"
}
# Build period summary path
period_summary_path <- file.path(base_dir, paste0(file_id, "_results"),
"rds",
Expand Down Expand Up @@ -307,7 +344,7 @@ generate_file_report <- function(
# Render the report
rmarkdown::render(
input = system.file("rmd", report_template, package = "ClockCyteR.spatial"),
output_file = file.path(output_dir, paste0("report_", file_id, ".pdf")),
output_file = file.path(output_dir, paste0("report_", file_id, output_ext)),
params = list(
channel_params = params$channels,
file_id = file_id,
Expand Down
Loading
Loading