Merge branch '2016-06' of github.com:swcarpentry/styles into 2016-06
[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 rmd- prefix for the figures generated by the lssons so
12 ## they can be easily identified and deleted by `make clean-rmd`.  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
18 opts_chunk$set(tidy = FALSE, results = "markup", comment = NA,
19                fig.align = "center", fig.path = "fig/rmd-",
20                fig.process = fix_fig_path)
21
22 # The hooks below add html tags to the code chunks and their output so that they
23 # are properly formatted when the site is built.
24
25 hook_in <- function(x, options) {
26   stringr::str_c("\n\n~~~\n",
27                  paste0(x, collapse="\n"),
28                  "\n~~~\n{: .r}\n\n")
29 }
30
31 hook_out <- function(x, options) {
32   x <- gsub("\n$", "", x)
33   stringr::str_c("\n\n~~~\n",
34                    paste0(x, collapse="\n"),
35                  "\n~~~\n{: .output}\n\n")
36 }
37
38 hook_error <- function(x, options) {
39   x <- gsub("\n$", "", x)
40   stringr::str_c("\n\n~~~\n",
41                  paste0(x, collapse="\n"),
42                  "\n~~~\n{: .error}\n\n")
43 }
44
45 knit_hooks$set(source = hook_in, output = hook_out, warning = hook_error,
46                error = hook_error, message = hook_out)