Skip to content
Snippets Groups Projects
render_report.R 1.45 KiB
Newer Older
#' @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
}