Skip to content

Instantly share code, notes, and snippets.

@erictleung
Created July 18, 2021 21:59
Show Gist options
  • Save erictleung/99ae58f5300bfce47600bd2850d5af75 to your computer and use it in GitHub Desktop.
Save erictleung/99ae58f5300bfce47600bd2850d5af75 to your computer and use it in GitHub Desktop.
Generate outline for {xaringan} presentations
# Load relevant packages
library(purrr)
library(stringr)
library(glue)
#' Programmatically generate xaringan outline
#'
#' @param file string name of generated xaringan RMarkdown
#' @param ... list of objects containing the structure for the presentation
#'
#' @example
#' outline_pres(sec_1 = list(title = "Intros", n_slides = 3),
#' sec_2 = list(title = "Part A", n_slides = 5))
outline_pres <- function(file = "untitled.Rmd", ...) {
header <- '---
title: "Presentation Ninja"
author: "Yihui Xie"
institute: "RStudio, PBC"
date: "2016/12/12 (updated: `r Sys.Date()`)"
output:
xaringan::moon_reader:
lib_dir: libs
nature:
highlightStyle: github
highlightLines: true
countIncrementalSlides: false
---
'
input <- list(...)
template <- map(input, process_sections)
fileConn <- file(file)
writeLines(str_c(c(header, template)), fileConn)
close(fileConn)
# Open file in RStudio
file.edit(file)
}
#' Process list configuration of slide structure
#'
#' @param config list with parameters `title` as string for the name of the
#' section and `n_slides` as an integer for the number of slides in this
#' section
#'
#' @return string
#' @export
#'
#' @examples
#' process_sections(list(title = "Section Name", n_slides = 3))
process_sections <- function(config) {
# Process input
section_name <- config$title
n_slides <- config$n_slides
# Template components
section <- glue("# {section_name}")
divider <- "---"
slide_heading <- "## Title"
collapse <- "\n\n"
# Create individual slides
slide_struc <- c(slide_heading, "", divider)
slides <- replicate(n_slides, str_c(slide_struc, collapse = collapse))
# Put slides and section title together
section_struc <- c(section, divider, slides, "")
section_txt <- str_c(section_struc, collapse = collapse)
return(section_txt)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment