move data/ into _episodes/ and _episodes_rmd/
[rnaseq-cwl-training.git] / bin / generate_md_episodes.R
1 generate_md_episodes <- function() {
2
3     if (require("knitr")) {
4         if (packageVersion("knitr") < '1.9.19') {
5             stop("knitr must be version 1.9.20 or higher")
6         }
7     } else stop("knitr 1.9.20 or above is needed to build the lessons.")
8
9     if (!require("stringr"))
10         stop("The package stringr is required for generating the lessons.")
11
12     ## where the Rmd files and the datasets are located
13     rmd_path <- "_episodes_rmd"
14     rmd_data <- file.path(rmd_path, "data")
15
16     ## where the markdown files and the datasets will end up
17     dest_path <- "_episodes"
18     dest_data <- file.path(dest_path, "data")
19
20     ## find all the Rmd files, and generates the paths for their respective outputs
21     src_rmd <- list.files(pattern = "??-*.Rmd$", path = rmd_path, full.names = TRUE)
22     dest_md <- file.path(dest_path, gsub("Rmd$", "md", basename(src_rmd)))
23
24     ## knit the Rmd into markdown
25     mapply(function(x, y) {
26         knitr::knit(x, output = y)
27     }, src_rmd, dest_md)
28
29
30     ## copy the datasets from _episodes_rmd/data to _episodes/data
31     rmd_data_files <- list.files(path = rmd_data, full.names = TRUE)
32     dest_data_files <- file.path(dest_data, basename(rmd_data_files))
33
34     if (!dir.exists(file.path(dest_data)))
35         dir.create(file.path(dest_data))
36
37     apply(cbind(rmd_data_files, dest_data_files), 1,
38           function(x) file.copy(x[1], x[2]))
39 }
40
41 generate_md_episodes()