diff --git a/DESCRIPTION b/DESCRIPTION index a780ff6e956f11cdc9fc76e66156e0307e891275..7c76f72ebb0b5e579ae33aba9c45bbd79a45ec1d 100644 --- a/DESCRIPTION +++ b/DESCRIPTION @@ -8,3 +8,8 @@ License: AGPL (>= 3) Encoding: UTF-8 Roxygen: list(markdown = TRUE) RoxygenNote: 7.2.3 +Imports: + bookdown, + yaml +Suggests: + tinytex diff --git a/NAMESPACE b/NAMESPACE index 9fa9a25a5d58c2d86178e29384865f6d71ae5846..6134e340480914e7d067d2fbf55d973cf9206769 100644 --- a/NAMESPACE +++ b/NAMESPACE @@ -5,4 +5,6 @@ export(add_gitignore) export(getDataPath) export(iconv_filename) export(loadConfig) +export(render_report) +export(render_reports) importFrom(magrittr,"%>%") diff --git a/R/render_report.R b/R/render_report.R new file mode 100644 index 0000000000000000000000000000000000000000..436e1a26816c6d6efe92625ae789d6f63caafed0 --- /dev/null +++ b/R/render_report.R @@ -0,0 +1,42 @@ +#' @rdname render_reports +#' @export +render_report <- function(path_report, + path_publish, + output_format = Sys.getenv("BOOKDOWN_FORMAT", "bookdown::gitbook"), + clean_cache = FALSE, + ...) { + + stopifnot(output_format %in% c("bookdown::gitbook", "bookdown::pdf_book")) + + if (output_format == "bookdown::pdf_book") { + if (!require("tinytex")) install.packages("tinytex") + } + + if (clean_cache) { + cache_dir <- list.files(path = path, + pattern = "_cache$", + include.dirs = TRUE, + full.names = TRUE) + unlink(cache_dir, recursive = TRUE) + } + cfg_bookdown <- + yaml::read_yaml(file.path(path, "_bookdown.yml")) + unlink(file.path(path, paste0(cfg_bookdown$book_filename, ".*"))) + output_dir <- file.path(path_publish, file) + dir.create(output_dir, + recursive = TRUE, + showWarnings = FALSE) + attached_packages <- (.packages()) + bookdown::render_book(path, + output_format = output_format, + output_dir = output_dir, + envir = new.env(), + ...) + # detach all packages used in the knit + sapply( + setdiff((.packages()), attached_packages), + function(x) detach(paste0("package:", x), + unload = TRUE, + character.only = TRUE)) + NULL +} diff --git a/R/render_reports.R b/R/render_reports.R new file mode 100644 index 0000000000000000000000000000000000000000..72fb5827320c480ca7058ad3dde32d7cd319e74b --- /dev/null +++ b/R/render_reports.R @@ -0,0 +1,39 @@ +#' Render reports +#' +#' @param path_reports [character] path for the reports +#' @param path_report [character] report path +#' @param reports [character] subfolders containing each report +#' @param path_publish [character] rendering output path +#' @param output_format output format, see [bookdown::render_book]. Only +#' `"bookdown::gitbook"` and `"bookdown::pdf_book"` are currently handled +#' @param clean_cache [logical] clean Rmarkdown cache files +#' @param ... Parameters passed to [render_report] and [bookdown::render_book] +#' +#' @return `NULL`, this function is used for side effect. +#' @export +#' @rdname render_reports +#' +#' @examples +render_reports <- function(path_reports = "./reports", + reports = list.files(path_reports, pattern = "^[0-9]{2}-.*$"), + path_publish = "./public/reports", + ...) { + + + + + options(knitr.duplicate.label = 'allow') + + message("output_format=", output_format) + message("folder list:") + invisible(sapply(files, message)) + + for(file in files){ + message("*******************************************************************") + message("** RENDER ", file) + message("*******************************************************************") + path <- file.path(path_reports, file) + render_report() + } + NULL +} diff --git a/man/render_reports.Rd b/man/render_reports.Rd new file mode 100644 index 0000000000000000000000000000000000000000..25e6d56d8953831f448241cf32ae9d2b976c0591 --- /dev/null +++ b/man/render_reports.Rd @@ -0,0 +1,44 @@ +% Generated by roxygen2: do not edit by hand +% Please edit documentation in R/render_report.R, R/render_reports.R +\name{render_report} +\alias{render_report} +\alias{render_reports} +\title{Render reports} +\usage{ +render_report( + path_report, + path_publish, + output_format = Sys.getenv("BOOKDOWN_FORMAT", "bookdown::gitbook"), + clean_cache = FALSE, + ... +) + +render_reports( + path_reports = "./reports", + reports = list.files(path_reports, pattern = "^[0-9]{2}-.*$"), + path_publish = "./public/reports", + ... +) +} +\arguments{ +\item{path_report}{\link{character} report path} + +\item{path_publish}{\link{character} rendering output path} + +\item{output_format}{output format, see \link[bookdown:render_book]{bookdown::render_book}. Only +\code{"bookdown::gitbook"} and \code{"bookdown::pdf_book"} are currently handled} + +\item{clean_cache}{\link{logical} clean Rmarkdown cache files} + +\item{...}{Parameters passed to \link{render_report} and \link[bookdown:render_book]{bookdown::render_book}} + +\item{path_reports}{\link{character} path for the reports} + +\item{reports}{\link{character} subfolders containing each report} +} +\value{ +\code{NULL}, this function is used for side effect. +} +\description{ +Render reports +}