-
-
Save eddjberry/d2918b05b9f12e24db46167b675b9e1f to your computer and use it in GitHub Desktop.
#!/usr/bin/env Rscript | |
# to run from command line: | |
## chmod +x knit_dir.R | |
## ./knit_dir.R <dir-name> | |
# from https://stackoverflow.com/a/49950761 | |
# to avoid conflicts between packages | |
# breaking things | |
clean_search <- function() { | |
defaults <- c(".GlobalEnv", | |
paste0("package:", getOption("defaultPackages")), | |
"Autoloads", | |
"package:base") | |
currentList <- search() | |
deletes <- setdiff(currentList, defaults) | |
for (entry in deletes) | |
detach(entry, character.only = TRUE) | |
} | |
# function to knit directory | |
knit_dir <- function(dir) { | |
# create the full path | |
full_dir <- normalizePath(dir) | |
# list the Rmd files with full names | |
files <- | |
list.files(full_dir, pattern = '*.Rmd$', full.names = TRUE) | |
# render the files using all output formats in the YAML | |
purrr::map(files, function(file) { | |
# clean search list to avoid conflicts | |
clean_search() | |
# render the file using all | |
# output formats in a new env | |
rmarkdown::render(file, | |
output_format = "all", | |
envir = new.env()) | |
}) | |
} | |
# pull out the first command line argument | |
dir <- commandArgs(trailingOnly = TRUE)[1] | |
# run the function | |
knit_dir(dir) |
This is awesome, thanks for sharing! Two minor comments that might be helpful
full_dir <- here::here(dir)I would recommend using
fs::path_abs(dir)
ornormalizePath(dir)
here instead. If you're running from the command line you'd probably expect the directory to be relative to where you run the command, buthere()
could move that on you. Eg. the (unlikely) scenario where you havedocs/docs
in your project root and runknit_dir.R docs
from insidedocs/
will try the firstdocs/
rather thandocs/docs
# render the files using all output formats in the YAML purrr::map(files, rmarkdown::render, output_format = 'all')You might want to move
rmarkdown::render
into an anonymous function so that you can render each document in a new environment.purrr::map(files, function(file) { rmarkdown::render(file, output_format = "all", envir = new.env()) }This ensures that each rmd render is performed in a fresh environment. You might even want to go as far as using
envir = new.env(parent = emptyenv())
.
Thanks a lot for your comment. I've made those changes :)
I removed the parent = emptyenv()
bit as this was causing an error when having
date: "17/12/2019 (updated: `r Sys.Date()`)"
in my YAML header (could not find function "Sys.Date"
)
Glad that the comments were helpful! On second thought parent = emptyenv()
is certainly way too conservative!
Have added the cleanSearch()
function from https://stackoverflow.com/a/49950761 to fix conflicts between packages
This is awesome, thanks for sharing! Two minor comments that might be helpful
I would recommend using
fs::path_abs(dir)
ornormalizePath(dir)
here instead. If you're running from the command line you'd probably expect the directory to be relative to where you run the command, buthere()
could move that on you. Eg. the (unlikely) scenario where you havedocs/docs
in your project root and runknit_dir.R docs
from insidedocs/
will try the firstdocs/
rather thandocs/docs
You might want to move
rmarkdown::render
into an anonymous function so that you can render each document in a new environment.This ensures that each rmd render is performed in a fresh environment. You might even want to go as far as using
envir = new.env(parent = emptyenv())
.