R/reports.R

#' #' List reports found in report directory
#' #'
#' #' Makes a list of raw report files in `report_source_folders`.
#' #' This function descends the directories rooted at `report_source_folders` to find all reports to be run.
#' #' Reports to be run are identified by filename extensions,
#' #' by default `c(".Rmd", ".Rnw")`.
#' #'
#' #' Users can override the default value of `report_source_folders` to supply their own
#' #' report directory (directories) of reports.
#' #' If users want to also include the default reports,
#' #' be sure to include `system.file(file.path("reports"), package = "PFUWorkflow")`
#' #' in the vector of paths supplied to `report_source_folders`.
#' #'
#' #' @param report_source_folders A string that identifies a directory of reports to be generated.
#' #'                              Default is `system.file(file.path("reports"), package = "PFUWorkflow")`,
#' #'                              which provides some default reports.
#' #' @param report_suffixes A vector of strings of report suffixes. Default is `c(".Rmd", ".Rnw")`.
#' #'
#' #' @return A vector of absolute paths to reports to be generated.
#' #'
#' #' @export
#' report_source_paths <- function(report_source_folders = system.file(file.path("reports"), package = "PFUWorkflow"),
#'                           report_suffixes = c(".Rmd", ".Rnw")) {
#'
#'   # Make a list in which we'll collect paths to all reports that need to be run.
#'   report_files <- list()
#'   # Cycle through all paths
#'   for (rs in report_source_folders) {
#'     all_files <- list.files(path = rs, full.names = TRUE, recursive = TRUE, include.dirs = TRUE)
#'     for (suff in report_suffixes) {
#'       which_files_are_reports <- which(endsWith(all_files, suffix = suff), arr.ind = TRUE)
#'       if (length(which_files_are_reports) > 0) {
#'         # report_files <- append(report_files, all_files[which_files_are_reports])
#'       }
#'     }
#'   }
#'   unlist(report_files)
#' }
#'
#'
#' #' Create file paths for reports output
#' #'
#' #' Create a vector of file paths for reports output.
#' #'
#' #' @param report_source_files A vector of report source file names, probably created with `report_source_paths()`.
#' #' @param report_dest_folder A directory for report output.
#' #'
#' #' @return A vector of file paths for reports output.
#' #'
#' #' @export
#' report_dest_paths <- function(report_source_files, report_dest_folder) {
#'   base_names <- basename(report_source_files)
#'   file.path(report_dest_folder, paste0(tools::file_path_sans_ext(base_names), ".pdf"))
#' }
#'
#'
#' #' Generate reports found in report directory
#' #'
#' #' This function builds all `report_source_files` with `knitr::knit()`.
#' #'
#' #' Each report name is suffixed by date and time (down to the second).
#' #' Duplicate report names in `report_source_files` generates an error.
#' #'
#' #' Previous reports are shuffled to a folder named `<<report_name>>_prev`.
#' #'
#' #' @param report_source_files A vector of absolute paths to reports that need to be run,
#' #'                            probably generated by `report_source_paths()`.
#' #' @param report_dest_files A vector of absolute paths into which completed reports are
#' #'                          to be written.
#' #' @param archive_reports A boolean that tells whether to archive reports in an adjacent folder.
#' #'                        Set `TRUE` to archive, `FALSE` to not archive.
#' #'                        Default is `TRUE`.
#' #' @param timestamp_sep A string that separates the date/time string from the report name for the file name on output.
#' #'                      Default is "__".
#' #'
#' #' @return A boolean. `TRUE` for success with all reports, `FALSE` if an error occurred with any report.
#' #'
#' #' @export
#' # generate_reports <- function(report_source_files, report_dest_files, archive_reports = TRUE, timestamp_sep = "__") {
#'   dt <- Sys.time() %>%
#'     as.character(tz = "UTC") %>%
#'     gsub(pattern = " ", replacement = "T", .) %>%
#'     gsub(pattern = ":", replacement = "_", .)
#'   fname <- paste0(bn, timestamp_sep, dt, ".pdf")
#'
#'
#'   for (f in report_source_files) {
#'     # Figure out the file name of the output file
#'     bn <- basename(f)
#'     dt <- Sys.time() %>%
#'       anytime::iso8601()
#'     fname <-paste0(bn, timestamp_sep, dt, ".pdf")
#'     output_path <- file.path(report_dest_folder, fname)
#'
#'     # Move any existing files whose base name starts with bn to the nearby folder.
#'
#'     # Now make the new report.
#'     knitr::knit(input = f, output = output_path)
#'
#'   }
#'
#'
#'   return(TRUE)
#' }
#'
#'
#'
MatthewHeun/SEAPSUTWorkflow documentation built on April 22, 2022, 2:21 p.m.