use quietly to avoid spurrious warnings
[rnaseq-cwl-training.git] / bin / generate_md_episodes.R
1 generate_md_episodes <- function() {
2
3   library("methods")
4   
5   if (!require("remotes", quietly = TRUE)) {
6     install.packages("remotes", repos = c(CRAN = "https://cloud.r-project.org/"))
7   }
8
9   if (!require("requirements", quietly = TRUE)) {
10     remotes::install_github("hadley/requirements")
11   }
12
13   required_pkgs <- unique(c(
14     ## Packages for episodes
15     requirements:::req_dir("_episodes_rmd"),
16     ## Pacakges for tools
17     requirements:::req_dir("bin")
18   ))
19
20   missing_pkgs <- setdiff(required_pkgs, rownames(installed.packages()))
21
22   if (length(missing_pkgs)) {
23     message("Installing missing required packages: ",
24             paste(missing_pkgs, collapse=", "))
25     install.packages(missing_pkgs)
26   }
27
28   if (require("knitr") && packageVersion("knitr") < '1.9.19')
29     stop("knitr must be version 1.9.20 or higher")
30
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")
35   }
36
37   src_rmd <- args[1]
38   dest_md <- args[2]
39
40   ## knit the Rmd into markdown
41   knitr::knit(src_rmd, output = dest_md)
42
43     # Read the generated md files and add comments advising not to edit them
44     vapply(dest_md, function(y) {
45       con <- file(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)
53       close(con)
54       return(paste("Warning added to YAML header of", y))
55     },
56     character(1))
57 }
58
59 generate_md_episodes()