simplify code checking for knitr
[rnaseq-cwl-training.git] / bin / generate_md_episodes.R
1 generate_md_episodes <- function() {
2
3     if (require("knitr") && packageVersion("knitr") < '1.9.19')
4         stop("knitr must be version 1.9.20 or higher")
5
6     if (!require("stringr"))
7         stop("The package stringr is required for generating the lessons.")
8
9     if (require("checkpoint")) {
10         required_pkgs <-
11             checkpoint:::projectScanPackages(project = "_episodes_rmd",
12                                              verbose=FALSE, use.knitr = TRUE)$pkgs
13     } else {
14         stop("The checkpoint package is required to build the lessons.")
15     }
16
17     missing_pkgs <- required_pkgs[!(required_pkgs %in% rownames(installed.packages()))]
18
19     if (length(missing_pkgs)) {
20         message("Installing missing required packages: ",
21                 paste(missing_pkgs, collapse=", "))
22         install.packages(missing_pkgs)
23     }
24
25     ## find all the Rmd files, and generates the paths for their respective outputs
26     src_rmd <- list.files(pattern = "??-*.Rmd$", path = "_episodes_rmd", full.names = TRUE)
27     dest_md <- file.path("_episodes", gsub("Rmd$", "md", basename(src_rmd)))
28
29     ## knit the Rmd into markdown
30     mapply(function(x, y) {
31         knitr::knit(x, output = y)
32     }, src_rmd, dest_md)
33
34 }
35
36 generate_md_episodes()