From cd5e7c7ccc4d23ddcebb8fc50d6b4756dae2523d Mon Sep 17 00:00:00 2001
From: David Dorchies <david.dorchies@inrae.fr>
Date: Thu, 31 Aug 2023 15:05:19 +0200
Subject: [PATCH] feat: add render_reports function

---
 DESCRIPTION           |  5 +++++
 NAMESPACE             |  2 ++
 R/render_report.R     | 42 +++++++++++++++++++++++++++++++++++++++++
 R/render_reports.R    | 39 ++++++++++++++++++++++++++++++++++++++
 man/render_reports.Rd | 44 +++++++++++++++++++++++++++++++++++++++++++
 5 files changed, 132 insertions(+)
 create mode 100644 R/render_report.R
 create mode 100644 R/render_reports.R
 create mode 100644 man/render_reports.Rd

diff --git a/DESCRIPTION b/DESCRIPTION
index a780ff6..7c76f72 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 9fa9a25..6134e34 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 0000000..436e1a2
--- /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 0000000..72fb582
--- /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 0000000..25e6d56
--- /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
+}
-- 
GitLab