Skip to content

Commit

Permalink
added build_png to replace build_thumbnail() to fix part of #15
Browse files Browse the repository at this point in the history
  • Loading branch information
jhelvy committed Feb 25, 2021
1 parent 3b3bc7a commit 4b5e83d
Show file tree
Hide file tree
Showing 48 changed files with 1,219 additions and 697 deletions.
5 changes: 3 additions & 2 deletions DESCRIPTION
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
Package: xaringanBuilder
Title: Functions For Building Xaringan Slides To Different Outputs
Version: 0.0.5.9000
Version: 0.0.6.9000
Authors@R:
c(person(given = "John",
family = "Helveston",
Expand Down Expand Up @@ -30,7 +30,8 @@ Imports:
pagedown,
progress,
rmarkdown,
xaringan
xaringan,
zip
Suggests:
chromote,
officer,
Expand Down
1 change: 1 addition & 0 deletions NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ export(build_all)
export(build_gif)
export(build_html)
export(build_pdf)
export(build_png)
export(build_pptx)
export(build_social)
export(build_thumbnail)
15 changes: 8 additions & 7 deletions NEWS.md
Original file line number Diff line number Diff line change
@@ -1,16 +1,17 @@
# xaringanBuilder 0.0.5
# xaringanBuilder 0.0.6

* Depreciated `build_thumbnail()` and added `build_png()` as an improved replacement.
* `build_png()` fixes part of issue #15 so that the png aspect ratio matches that of the xaringan slides.
* `build_png()` also has options for changing the png density and building more than just the title slide (can build pngs of some or all slides using the `slides` argument).
* All documentation and examples updated to match new features.
* New example slides made to demonstrate new features.

## Summary of larger updates:
# xaringanBuilder 0.0.5

* Major improvements to how paths are handled by adding the build_paths() function (not exported). Now you can use a url to build to any output types except for social and html (which both require starting from the Rmd file).
* Added build_to_pdf() function (not exported) as an internal helper to build from a Rmd or html file to the pdf.

## Summary of smaller updates:

* Added `assert_chrome_installed()` for issue #12

## Bugs

# xaringanBuilder 0.0.4

* Added `build_social()` for making a png of the first slides sized for sharing on social media.
Expand Down
112 changes: 72 additions & 40 deletions R/all.R
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
#' Build xaringan slides to multiple outputs.
#'
#' Build xaringan slides to multiple outputs. Options are `"html"`, `"pdf"`,
#' `"gif"`, `"pptx"`, `"thumbnail"`, and `"social"`. See individual
#' build_..() functions for details about each output type.
#' `"gif"`, `"pptx"`, `"png"`, and `"social"`. See each individual
#' build_*() function for details about each output type.
#' @param input Path to Rmd file of xaringan slides.
#' @param include A vector of the different output types to build. Options are
#' `"html"`, `"pdf"`, `"gif"`, `"pptx"`, `"thumbnail"`, and `"social"`.
#' Defaults to `c("html", "pdf", "gif", "pptx", "thumbnail", "social")`.
#' `"html"`, `"pdf"`, `"gif"`, `"pptx"`, `"png"`, and `"social"`.
#' Defaults to `c("html", "pdf", "gif", "pptx", "png", "social")`.
#' @param exclude A vector of the different output types to NOT build. Options
#' are `"html"`, `"pdf"`, `"gif"`, `"pptx"`, `"thumbnail"`, and `"social"`.
#' are `"html"`, `"pdf"`, `"gif"`, `"pptx"`, `"png"`, and `"social"`.
#' Defaults to `NULL`, in which case all all output types are built.
#' @param complex_slides For "complex" slides (e.g. slides with panelsets or
#' other html widgets or advanced features), set `complex_slides = TRUE`.
Expand All @@ -21,40 +21,39 @@
#' @param delay Seconds of delay between advancing to and printing
#' a new slide. Only used if `complex_slides = TRUE` or `partial_slides =
#' TRUE`.
#' @param density Resolution of the resulting png files used in the png, gif,
#' and pptx output types file. Defaults to `"72x72"`.
#' @param slides A vector of the slide number(s) to return for the png output.
#' Defaults to `1`, returning only the title slide. You can get a zip
#' file of all the slides as pngs by setting `slides = "all"`).
#' @param fps Frames per second of the gif file.
#' @export
#' @examples
#' \dontrun{
#' # Builds every output by default
#' build_all("slides.Rmd")
#'
#' # Choose which output types to include
#' # Both of these build html, pdf, and gif outputs
#' build_all("slides.Rmd", include = c("html", "pdf", "gif"))
#'
#' # Choose which output types to exclude
#' build_all("slides.Rmd", exclude = c("pptx", "thumbnail"))
#' build_all("slides.Rmd", exclude = c("pptx", "png", "social"))
#' }
build_all <- function(
input,
include = c("html", "pdf", "gif", "pptx", "thumbnail", "social"),
include = c("html", "pdf", "gif", "pptx", "png", "social"),
exclude = NULL,
complex_slides = FALSE,
partial_slides = FALSE,
delay = 1
delay = 1,
density = "72x72",
slides = 1,
fps = 1
) {
# Check that input file has the correct extension
assert_path_ext(input, "rmd")

# Build input and output paths
paths <- build_paths(input, output_file = NULL)

include <- match.arg(include, several.ok = TRUE)
do_htm <- ("html" %in% include) && (! "html" %in% exclude)
do_pdf <- ("pdf" %in% include) && (! "pdf" %in% exclude)
do_gif <- ("gif" %in% include) && (! "gif" %in% exclude)
do_ppt <- ("pptx" %in% include) && (! "pptx" %in% exclude)
do_thm <- ("thumbnail" %in% include) && (! "thumbnail" %in% exclude)
do_soc <- ("social" %in% include) && (! "social" %in% exclude)

# Build hierarchy:
#
# Rmd
Expand All @@ -63,38 +62,52 @@ build_all <- function(
# |
# |--> html
# |
# |--> thumbnail (png)
# |
# |--> pdf
# |
# |--> gif
# |
# |--> pptx
# |--> png
# |
# |--> gif
# |
# |--> pptx
#
# currently calling a step out of order will create the intermediate steps
# if at some point intermediate files are removed if not requested, the
# logic here will need to be changed.

if (do_gif && (!fs::file_exists(paths$input$pdf) || do_htm)) {
# to make a gif we need the PDF file
# or if we update the HTML, we should also update the PDF for the gif
do_pdf <- TRUE
}
if (do_ppt && (!fs::file_exists(paths$input$pdf) || do_htm)) {
# to make a pptx we need the PDF file
# or if we update the HTML, we should also update the PDF for the pptx
do_pdf <- TRUE
include <- match.arg(include, several.ok = TRUE)
do_soc <- ("social" %in% include) && (! "social" %in% exclude)
do_htm <- ("html" %in% include) && (! "html" %in% exclude)
do_pdf <- ("pdf" %in% include) && (! "pdf" %in% exclude)
do_png <- ("png" %in% include) && (! "png" %in% exclude)
do_gif <- ("gif" %in% include) && (! "gif" %in% exclude)
do_ppt <- ("pptx" %in% include) && (! "pptx" %in% exclude)

if (!fs::file_exists(paths$input$pdf) || do_htm) {
# If the PDF doesn't exist or we're updating the html file,
# then we need to also update the PDF if we are going to
# built to png, gif, or pptx outputs
if (do_png | do_gif | do_ppt) {
do_pdf <- TRUE
}
}
if ((do_pdf || do_thm) && !fs::file_exists(paths$input$html)) {
# to make a PDF or thumbnail we need the html file

if (do_pdf && !fs::file_exists(paths$input$html)) {
# to make a PDF, we need the html file
do_htm <- TRUE
}

# Do each step in order to ensure updates propagate
# (or we use the current version of the required build step)
if (do_soc) build_social(paths$input$rmd, paths$output$social)
if (do_htm) build_html(paths$input$rmd, paths$output$html)
if (do_thm) build_thumbnail(paths$input$html, paths$output$thumbnail)
if (do_soc) {
build_social(
input = paths$input$rmd,
output_file = paths$output$social)
}
if (do_htm) {
build_html(
input = paths$input$rmd,
output_file = paths$output$html)
}
if (do_pdf) {
build_pdf(
input = paths$input$html,
Expand All @@ -103,8 +116,27 @@ build_all <- function(
partial_slides = partial_slides,
delay = delay)
}
if (do_gif) build_gif(paths$input$pdf, paths$output$gif)
if (do_ppt) build_pptx(paths$input$pdf, paths$output$pptx)
if (do_png) {
build_png(
input = paths$input$pdf,
output_file = paths$output$png,
density = density,
slides = slides
)
}
if (do_gif) {
build_gif(
input = paths$input$pdf,
output_file = paths$output$gif,
density = density,
fps = fps)
}
if (do_ppt) {
build_pptx(
input = paths$input$pdf,
output_file = paths$output$pptx,
density = density)
}

invisible(input)
}
8 changes: 5 additions & 3 deletions R/gif.R
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,12 @@
#'
#' Build xaringan slides as a gif file. The function builds to the pdf and
#' then converts it to a gif.
#' @param input Path to a Rmd, html, or pdf file, or a url of xaringan slides.
#' @param input Path to a Rmd file, html file, pdf file, or a url.
#' If the input is a url to xaringan slides on a website, you must provide the
#' full url ending in ".html".
#' @param output_file Name of the output gif file.
#' @param density Resolution of the resulting gif file.
#' @param density Resolution of the resulting gif file. Defaults to
#' `"72x72"`.
#' @param fps Frames per second of the resulting gif file.
#' @param complex_slides For "complex" slides (e.g. slides with panelsets or
#' other html widgets or advanced features), set `complex_slides = TRUE`.
Expand All @@ -22,10 +23,11 @@
#' @export
#' @examples
#' \dontrun{
#' # Build gif from Rmd, html, or pdf file
#' # Build gif from Rmd, html, pdf, or url
#' build_gif("slides.Rmd")
#' build_gif("slides.html")
#' build_gif("slides.pdf")
#' build_gif("https://jhelvy.github.io/xaringanBuilder/reference/figures/slides.html")
#' }
build_gif <- function(
input,
Expand Down
30 changes: 2 additions & 28 deletions R/pdf.R
Original file line number Diff line number Diff line change
Expand Up @@ -23,12 +23,10 @@
#' @export
#' @examples
#' \dontrun{
#' # Build a pdf from a Rmd or html file
#' # Build pdf from Rmd, html, or url
#' build_pdf("slides.Rmd")
#' build_pdf("slides.html")
#'
#' # Build a pdf from a url
#' build_pdf("http://www.mysite.com/myslides.html")
#' build_pdf("https://jhelvy.github.io/xaringanBuilder/reference/figures/slides.html")
#'
#' # Build a pdf with partial (continuation) slides
#' build_pdf("slides.Rmd", partial_slides = TRUE)
Expand Down Expand Up @@ -81,15 +79,6 @@ build_pdf <- function(
}
}

#' Print "simple" xaringan slides to PDF
#'
#' Prints "simple" xaringan slides - those without panelsets or other html
#' widgets or advanced features, and also slides without partial slides.
#'
#' @param input Path to Rmd or html file of xaringan slides.
#' @param output_file The name of the output file. If `NULL` (the default) then
#' the output filename will be based on filename for the input file.
#' If a filename is provided, a path to the output file can also be provided.
build_pdf_simple <- function(input, output_file = NULL) {
print_build_status(input, output_file)
pagedown::chrome_print(
Expand All @@ -101,21 +90,6 @@ build_pdf_simple <- function(input, output_file = NULL) {
# in v0.0.2. He also posted it on his blog here:
# https://www.garrickadenbuie.com/blog/print-xaringan-chromote/

#' Print "complex" xaringan slides to PDF
#'
#' Prints "complex" xaringan slides (e.g. slides with panelsets or other html
#' widgets or advanced features) to a PDF file. Requires a local installation
#' of Chrome.
#'
#' @param input Path to Rmd or html file of xaringan slides.
#' @param output_file The name of the output file. If `NULL` (the default) then
#' the output filename will be based on filename for the input file.
#' If a filename is provided, a path to the output file can also be provided.
#' @param partial_slides Should partial (continuation) slides be
#' included in the output? If `FALSE`, the default, only the complete slide
#' is included in the PDF.
#' @param delay Seconds of delay between advancing to and printing
#' a new slide.
build_pdf_complex <- function(input, output_file, partial_slides, delay) {
if (!requireNamespace("chromote", quietly = TRUE)) {
stop("`chromote` is required: devtools::install_github('rstudio/chromote')")
Expand Down
Loading

0 comments on commit 4b5e83d

Please sign in to comment.