Skip to content

Instantly share code, notes, and snippets.

@mattwarkentin
Last active October 9, 2019 20:47
Show Gist options
  • Save mattwarkentin/2a3c6d8deba3737ca41405083e254ac5 to your computer and use it in GitHub Desktop.
Save mattwarkentin/2a3c6d8deba3737ca41405083e254ac5 to your computer and use it in GitHub Desktop.
R function to double-render a R Markdown document destined to be a Beamer presentation. Solves the total frame number issue.
render_beamer <- function(input = NULL, cleanup = FALSE, ...){
assertthat::assert_that(fs::file_exists(input))
bare_input <- fs::path_ext_remove(input)
out_dir <- fs::path_dir(input)
rmarkdown::render(input, ...)
pre_dir <- rbind(tibble::enframe(fs::dir_ls(out_dir)),
glue::glue('{fs::as_fs_path(bare_input)}.pdf'),
glue::glue('{fs::as_fs_path(bare_input)}.tex'))
if(!fs::file_exists(glue::glue('{bare_input}.tex'))){
stop('You must keep the TeX file after knitting your Rmd.')
}
tex_engine <- getOption('latex.engine', default = Sys.which('pdflatex'))
system(glue::glue('{tex_engine} --output-directory={out_dir} {bare_input}.tex'))
system(glue::glue('{tex_engine} --output-directory={out_dir} {bare_input}.tex'))
if(cleanup){
post_dir <- tibble::enframe(fs::dir_ls(out_dir))
to_remove <- dplyr::anti_join(post_dir, pre_dir, by = 'value')
fs::file_delete(to_remove$value)
}
if(Sys.info()[1] %in% 'Windows'){
system(glue::glue('start {bare_input}.pdf'))
} else {
system(glue::glue('open {bare_input}.pdf'))
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment