From af863009d05b74d363e8ca4e4c5080d54d38a23c Mon Sep 17 00:00:00 2001
From: David Dorchies <david.dorchies@inrae.fr>
Date: Thu, 31 Aug 2023 18:39:35 +0200
Subject: [PATCH] fix: bugs in render_reports

---
 R/render_report.R     | 15 ++++++++-------
 R/render_reports.R    | 28 +++++++++++++---------------
 man/render_reports.Rd | 25 +++++++++++++++++--------
 3 files changed, 38 insertions(+), 30 deletions(-)

diff --git a/R/render_report.R b/R/render_report.R
index 436e1a2..a6f215a 100644
--- a/R/render_report.R
+++ b/R/render_report.R
@@ -1,7 +1,8 @@
 #' @rdname render_reports
+#' @inheritParams bookdown::render_book
 #' @export
-render_report <- function(path_report,
-                          path_publish,
+render_report <- function(input,
+                          output_dir,
                           output_format = Sys.getenv("BOOKDOWN_FORMAT", "bookdown::gitbook"),
                           clean_cache = FALSE,
                           ...) {
@@ -11,23 +12,23 @@ render_report <- function(path_report,
   if (output_format == "bookdown::pdf_book") {
     if (!require("tinytex")) install.packages("tinytex")
   }
+  message("output_format=", output_format)
 
   if (clean_cache) {
-    cache_dir <- list.files(path = path,
+    cache_dir <- list.files(path = input,
                             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)
+    yaml::read_yaml(file.path(input, "_bookdown.yml"))
+  unlink(file.path(input, paste0(cfg_bookdown$book_filename, ".*")))
   dir.create(output_dir,
              recursive = TRUE,
              showWarnings = FALSE)
   attached_packages <- (.packages())
-  bookdown::render_book(path,
+  bookdown::render_book(input,
                         output_format =  output_format,
                         output_dir = output_dir,
                         envir =  new.env(),
diff --git a/R/render_reports.R b/R/render_reports.R
index 72fb582..78dd7bf 100644
--- a/R/render_reports.R
+++ b/R/render_reports.R
@@ -1,9 +1,9 @@
 #' Render reports
 #'
-#' @param path_reports [character] path for the reports
-#' @param path_report [character] report path
+#' @param reports_dir [character] path for the reports
 #' @param reports [character] subfolders containing each report
-#' @param path_publish [character] rendering output path
+#' @param publish_dir [character] rendering output path relatively to the report
+#' folder which is in format `"./reports/my_report"`
 #' @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
@@ -14,26 +14,24 @@
 #' @rdname render_reports
 #'
 #' @examples
-render_reports <- function(path_reports = "./reports",
-                           reports = list.files(path_reports, pattern = "^[0-9]{2}-.*$"),
-                           path_publish = "./public/reports",
+render_reports <- function(reports_dir = "./reports",
+                           reports = list.dirs(reports_dir),
+                           publish_dir = "../../public/reports",
                            ...) {
 
-
-
-
   options(knitr.duplicate.label = 'allow')
 
-  message("output_format=", output_format)
   message("folder list:")
-  invisible(sapply(files, message))
+  invisible(sapply(reports, message))
 
-  for(file in files){
+  for(report in reports){
     message("*******************************************************************")
-    message("** RENDER ", file)
+    message("** RENDER ", report)
     message("*******************************************************************")
-    path <- file.path(path_reports, file)
-    render_report()
+    path <-
+    render_report(input = file.path(reports_dir, report),
+                  output_dir = file.path(publish_dir, report),
+                  ...)
   }
   NULL
 }
diff --git a/man/render_reports.Rd b/man/render_reports.Rd
index 25e6d56..851d69d 100644
--- a/man/render_reports.Rd
+++ b/man/render_reports.Rd
@@ -6,24 +6,31 @@
 \title{Render reports}
 \usage{
 render_report(
-  path_report,
-  path_publish,
+  input,
+  output_dir,
   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",
+  reports_dir = "./reports",
+  reports = list.files(reports_dir, pattern = "^[0-9]{2}-.*$"),
+  publish_dir = "./public/reports",
   ...
 )
 }
 \arguments{
-\item{path_report}{\link{character} report path}
+\item{input}{A directory, an input filename or multiple filenames. For a
+directory, \file{index.Rmd} will be used if it exists in this (book)
+project directory. For filenames, if \code{preview = TRUE}, only files
+specified in this argument are rendered, otherwise all R Markdown files
+specified by the book are rendered.}
 
-\item{path_publish}{\link{character} rendering output path}
+\item{output_dir}{The output directory. If \code{NULL}, a field named
+\code{output_dir} in the configuration file \file{_bookdown.yml} will be
+used (possibly not specified, either, in which case a directory name
+\file{_book} will be used).}
 
 \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}
@@ -32,9 +39,11 @@ render_reports(
 
 \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_dir}{\link{character} path for the reports}
 
 \item{reports}{\link{character} subfolders containing each report}
+
+\item{publish_dir}{\link{character} rendering output path}
 }
 \value{
 \code{NULL}, this function is used for side effect.
-- 
GitLab