R/build-presentations.R

Defines functions build_presentations

Documented in build_presentations

#' Each Rmarkdown Xaringan presentation in \code{Presentations/} is
#' rendered. Rendered html and associated directories are placed within \code{docs/presentations/}
#'
#' In addition, a small file is created in \code{Documents/presentations} that is embeds the presentation
#' into the website generated by packagePages()/projectPages()
#'
#' If you want articles that are not vignettes, either put them in
#' subdirectories or list in \code{.Rbuildignore}. An articles link
#' will be automatically added to the default navbar if the vignettes
#' directory is present: if you do not want this, you will need to
#' customise the navbar. See \code{\link{build_site}} details.
#'
#' Build presentations
#'
#' @param output_directory Output path. Relative paths are taken relative to the
#'     \code{pkg} directory.
#' @param depth Depth of path relative to root of documentation.  Used
#'     to adjust relative links in the navbar.
#' @param encoding The encoding of the input files.
#' @param quiet Set to `FALSE` to display output of knitr and
#'   pandoc. This is useful when debugging.
#' @param input_presentations_directory Defaults to Presentations
#' @export
build_presentations <- function(
                            output_directory = "docs/presentations", 
                            depth = 1L,
                            encoding = "UTF-8",
                            quiet = TRUE,
                            input_presentations_directory="Presentations") {

  ### render presentations
  home_directory <- getwd()
  setwd(input_presentations_directory)
  input_Rmd_presentations <- list.files(pattern="Rmd")
  for (presentations_names_iter in input_Rmd_presentations) {
      tmp_file_name <- gsub(".Rmd", "", presentations_names_iter)
      xfun::Rscript_call(rmarkdown::render, list(input=presentations_names_iter, clean='TRUE'))

      if (!dir.exists(paste0("../", output_directory))) dir.create(paste0("../", output_directory), showWarnings=FALSE)
      file.copy(paste0(tmp_file_name, ".html"), paste0("../", output_directory, "/", tmp_file_name, ".html"), overwrite=TRUE)
      if (!dir.exists(paste0("../", output_directory, "/", tmp_file_name, "_files"))) dir.create(paste0("../", output_directory, "/", tmp_file_name, "_files"), showWarnings=FALSE)
      cfaDocs::shareImage(presentations_names_iter, path_image=paste0("../", output_directory, "/", tmp_file_name, "_files/TITLE_SLIDE_image.png" ))
      unlink(paste0(tmp_file_name, ".html"))
      if (dir.exists(paste0(tmp_file_name, "_files"))) {
          R.utils::copyDirectory(paste0(tmp_file_name, "_files"), paste0("../", output_directory, "/", tmp_file_name, "_files"), recursive=TRUE)
          unlink(paste0(tmp_file_name, "_files"), recursive=TRUE)
      }
      unlink(paste0(tmp_file_name, "_files"), recursive=TRUE)
      R.utils::copyDirectory("libs", paste0("../", output_directory, "/libs"), recursive=TRUE)
      unlink("libs", recursive=TRUE)

      if (!dir.exists(file.path("..", "Documents", "presentations"))) {
          dir.create(file.path("..", "Documents", "presentations"), showWarnings=FALSE)
          presentation_stub <- readLines(system.file("templates", "xaringan-stub.Rmd", package = "packagePages"), encoding='UTF-8')
          presentation_stub <- gsub("TEMP_PRESENTATION_NAME", tmp_file_name, presentation_stub)
          presentation_stub <- gsub("TEMP_DATE", toOrdinalDate(Sys.Date()), presentation_stub)
          write(presentation_stub, file=paste0("../Documents/presentations/", presentations_names_iter))
      }
  }
  setwd(home_directory)
} ### build_presentations function
CenterForAssessment/packagePages documentation built on May 31, 2022, 1:56 p.m.