add tools to deal with episodes written in Rmd
[rnaseq-cwl-training.git] / bin / chunk-options.R
1 # These settings control the behavior of all chunks in the novice R materials.
2 # For example, to generate the lessons with all the output hidden, simply change
3 # `results` from "markup" to "hide".
4 # For more information on available chunk options, see
5 # http://yihui.name/knitr/options#chunk_options
6
7 library("knitr")
8
9 fix_fig_path <- function(pth) file.path("..", pth)
10
11 ## We use the swc-rmd- prefix for the figures generated by the lssons
12 ## so they can be easily identified and deleted by `make clean`.  The
13 ## working directory when the lessons are generated is the root so the
14 ## figures need to be saved in fig/, but when the site is generated,
15 ## the episodes will be one level down. We fix the path using the
16 ## `fig.process` option.
17 opts_chunk$set(tidy = FALSE, results = "markup", comment = NA,
18                fig.align = "center", fig.path = "fig/swc-rmd-",
19                fig.process = fix_fig_path)
20
21 # The hooks below add html tags to the code chunks and their output so that they
22 # are properly formatted when the site is built.
23 hook_in <- function(x, options) {
24   stringr::str_c("\n\n~~~{.r}\n",
25                  paste0(x, collapse="\n"),
26                  "\n~~~\n{: .source}\n\n")
27 }
28
29 hook_out <- function(x, options) {
30   stringr::str_c("\n\n~~~\n",
31                    paste0(x, collapse="\n"),
32                  "\n~~~\n{: .output}\n\n")
33 }
34
35 hook_error <- function(x, options) {
36   stringr::str_c("\n\n~~~\n",
37                  paste0(x, collapse="\n"),
38                  "\n~~~\n{: .error}\n\n")
39 }
40
41 knit_hooks$set(source = hook_in, output = hook_out, warning = hook_error,
42                error = hook_error, message = hook_out)