check packages required and install them if needed
[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     } else stop("knitr 1.9.20 or above is needed to build the lessons.")
6
7     if (!require("stringr"))
8         stop("The package stringr is required for generating the lessons.")
9
10     if (require("checkpoint")) {
11         required_pkgs <-
12             checkpoint:::projectScanPackages(project = "_episodes_rmd",
13                                              verbose=FALSE, use.knitr = TRUE)$pkgs
14     } else {
15         stop("The checkpoint package is required to build the lessons.")
16     }
17
18     missing_pkgs <- required_pkgs[!(required_pkgs %in% rownames(installed.packages()))]
19
20     if (length(missing_pkgs)) {
21         message("Installing missing required packages: ",
22                 paste(missing_pkgs, collapse=", "))
23         install.packages(missing_pkgs)
24     }
25
26     ## find all the Rmd files, and generates the paths for their respective outputs
27     src_rmd <- list.files(pattern = "??-*.Rmd$", path = "_episodes_rmd", full.names = TRUE)
28     dest_md <- file.path("_episodes", gsub("Rmd$", "md", basename(src_rmd)))
29
30     ## knit the Rmd into markdown
31     mapply(function(x, y) {
32         knitr::knit(x, output = y)
33     }, src_rmd, dest_md)
34
35 }
36
37 generate_md_episodes()