1 generate_md_episodes <- function() {
5 if (!require("remotes", quietly = TRUE)) {
6 install.packages("remotes", repos = c(CRAN = "https://cloud.r-project.org/"))
9 if (!require("requirements", quietly = TRUE)) {
10 remotes::install_github("hadley/requirements")
13 required_pkgs <- unique(c(
14 ## Packages for episodes
15 requirements:::req_dir("_episodes_rmd"),
17 requirements:::req_dir("bin")
20 missing_pkgs <- setdiff(required_pkgs, rownames(installed.packages()))
22 if (length(missing_pkgs)) {
23 message("Installing missing required packages: ",
24 paste(missing_pkgs, collapse=", "))
25 install.packages(missing_pkgs)
28 if (require("knitr") && packageVersion("knitr") < '1.9.19')
29 stop("knitr must be version 1.9.20 or higher")
31 ## get the Rmd file to process from the command line, and generate the path for their respective outputs
32 args <- commandArgs(trailingOnly = TRUE)
33 if (!identical(length(args), 2L)) {
34 stop("input and output file must be passed to the script")
40 ## knit the Rmd into markdown
41 knitr::knit(src_rmd, output = dest_md)
43 # Read the generated md files and add comments advising not to edit them
44 vapply(dest_md, function(y) {
46 mdfile <- readLines(con)
47 if (mdfile[1] != "---")
48 stop("Input file does not have a valid header")
49 mdfile <- append(mdfile, "# Please do not edit this file directly; it is auto generated.", after = 1)
50 mdfile <- append(mdfile, paste("# Instead, please edit",
51 basename(y), "in _episodes_rmd/"), after = 2)
52 writeLines(mdfile, con)
54 return(paste("Warning added to YAML header of", y))
59 generate_md_episodes()