Last active
October 9, 2019 20:47
-
-
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.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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