R/render_html_and_pdf.R

Defines functions render_html_and_pdf0 render_html_and_pdf

Documented in render_html_and_pdf render_html_and_pdf0

#' Render an Rmarkdown presentation to HTML and PDF
#'
#' This function is for the Rstudio addin API. 
#' 
#' @param notes_slides Create a PDF with space for notes?
#' @return Returns a list with the output file paths
#' @export
#' @importFrom rstudioapi getActiveDocumentContext
#' @importFrom tools file_path_sans_ext
#' @importFrom rmarkdown render yaml_front_matter
#' @importFrom pagedown chrome_print
#'
render_html_and_pdf <-
function( notes_slides = pkg_options()$notes_slides){
  ctx <- rstudioapi::getActiveDocumentContext()
  rmd_filename <- ctx$path

  res <- render_html_and_pdf0(rmd_filename, notes_slides)
  
  invisible( res )
}

#' Render an Rmarkdown presentation to HTML and PDF
#'
#' @param rmd_filename Rmd file to compile
#' @param notes_slides Create a PDF with space for notes?
#' @return Returns a list with the output file paths
#' @export
#' @importFrom rstudioapi getActiveDocumentContext
#' @importFrom tools file_path_sans_ext
#' @importFrom rmarkdown render yaml_front_matter
#' @importFrom pagedown chrome_print
#'
render_html_and_pdf0 <-
  function( rmd_filename, notes_slides = pkg_options()$notes_slides){

    fn_extn <- tools::file_ext(rmd_filename)
    fn_base <- tools::file_path_sans_ext(rmd_filename)
    
    stopifnot(fn_extn %in% c("Rmd", "rmd"))
    out_html <- rmarkdown::render( rmd_filename )
    
    yaml <- rmarkdown::yaml_front_matter( rmd_filename )
    if( !("ioslides_presentation" %in% names(yaml$output)) )
      stop("Output must be ioslides_presentation.")
    
    paperHeight <- pkg_options()$paperHeight
    widescreen <- yaml$output$ioslides_presentation$widescreen
    
    if(is.null(widescreen)){
      paperWidth = paperHeight / pkg_options()$aspect
    }else{
      paperWidth = paperHeight / pkg_options()$widescreen_aspect
    }
    
    out_extn <- tools::file_ext(out_html)
    stopifnot(out_extn == "html")
    
    out_pdf <- pagedown::chrome_print(out_html,
                                      wait = pkg_options()$wait,
                                      options = list(
                                        landscape = TRUE,
                                        paperHeight = paperHeight,
                                        paperWidth = paperWidth,
                                        marginTop = pkg_options()$marginTop,
                                        marginBottom = pkg_options()$marginBottom,
                                        marginLeft = pkg_options()$marginLeft,
                                        marginRight = pkg_options()$marginRight,
                                        printBackground = pkg_options()$printBackground)
    )
    
    if(notes_slides)
      render_notes_slides_from_pdf( out_pdf )
    
    invisible( c(html = out_html, pdf = out_pdf) )
  }
richarddmorey/RcompileSlides documentation built on April 17, 2020, 3:24 a.m.