Last active
April 2, 2019 10:28
-
-
Save malcolmbarrett/7c5935d1734016dcb0b2c81b9bc3b549 to your computer and use it in GitHub Desktop.
Move R Markdown HTML slides to Blogdown and Push to Web
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
# install.packages(c("here", "fs", "stringr", "purrr", "git2r")) | |
# to add invisibly in your R profile, open with usethis::edit_r_profile() | |
# then define it in an environment, e.g. | |
# .env <- new.env() | |
# .env$move_slides_to_web <- {function definition} | |
move_slides_to_web <- function(folder = NULL, index = NULL) { | |
if (is.null(folder)) { | |
# get working dir folder's name if `folder` isn't specified | |
directory <- stringr::str_split(here::here(), "/")[[1]] | |
folder <- directory[length(directory)] | |
} | |
website_dir <- file.path( | |
"path", | |
"to", | |
"your", | |
"blogdown", | |
"website" | |
) | |
# create slides folder if necessary | |
slides_folder <- file.path(website_dir, "static", "slides") | |
if (!fs::dir_exists(slides_folder)) fs::dir_create(slides_folder) | |
# build path to blogdown static files | |
slides_path <- file.path( | |
website_dir, | |
"static", | |
"slides", | |
folder | |
) | |
# replace existing dir if necessary | |
if (fs::dir_exists(slides_path)) fs::dir_delete(slides_path) | |
fs::dir_create(slides_path) | |
# get all files except Rproj, gitignore, and Rmd | |
files <- stringr::str_subset(fs::dir_ls(), pattern = "Rproj|gitignore|Rmd", negate = TRUE) | |
# copy files and directories to slides dir | |
fs::file_copy(files, slides_path) | |
purrr::map_if(files, fs::is_dir, ~fs::dir_copy(.x, slides_path)) | |
if (is.null(index)) { | |
# find html file to rename | |
index <- stringr::str_subset(files, pattern = "html") | |
# stop if there's not exactly one html file | |
# need to specify with `index` | |
stopifnot(length(index) == 1) | |
} | |
# set html file to be index | |
file.rename(file.path(slides_path, index), file.path(slides_path, "index.html")) | |
# commit and push! | |
git2r::add(website_dir, path = ".") | |
git2r::commit(website_dir, paste("Update", folder, "Slides", Sys.time())) | |
git2r::push(website_dir, credentials = git2r::cred_token()) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment