Remove software carpentries logo
[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
12 ## We set the path for the figures globally below, so if we want to
13 ## customize it for individual episodes, we can append a prefix to the
14 ## global path. For instance, if we call knitr_fig_path("01-") in the
15 ## first episode of the lesson, it will generate the figures in
16 ## `fig/rmd-01-`
17 knitr_fig_path <- function(prefix) {
18     new_path <- paste0(opts_chunk$get("fig.path"),
19                       prefix)
20     opts_chunk$set(fig.path = new_path)
21 }
22
23 ## We use the rmd- prefix for the figures generated by the lessons so
24 ## they can be easily identified and deleted by `make clean-rmd`.  The
25 ## working directory when the lessons are generated is the root so the
26 ## figures need to be saved in fig/, but when the site is generated,
27 ## the episodes will be one level down. We fix the path using the
28 ## `fig.process` option.
29
30 opts_chunk$set(tidy = FALSE, results = "markup", comment = NA,
31                fig.align = "center", fig.path = "fig/rmd-",
32                fig.process = fix_fig_path,
33                fig.width = 8.5, fig.height = 8.5,
34                fig.retina = 2)
35
36 # The hooks below add html tags to the code chunks and their output so that they
37 # are properly formatted when the site is built.
38
39 hook_in <- function(x, options) {
40   lg <- tolower(options$engine)
41   style <- paste0(".language-", lg)
42
43   stringr::str_c("\n\n~~~\n",
44     paste0(x, collapse="\n"),
45     "\n~~~\n{: ", style, "}\n\n")
46 }
47
48 hook_out <- function(x, options) {
49   x <- gsub("\n$", "", x)
50   stringr::str_c("\n\n~~~\n",
51     paste0(x, collapse="\n"),
52     "\n~~~\n{: .output}\n\n")
53 }
54
55 hook_error <- function(x, options) {
56   x <- gsub("\n$", "", x)
57   stringr::str_c("\n\n~~~\n",
58     paste0(x, collapse="\n"),
59     "\n~~~\n{: .error}\n\n")
60 }
61
62 hook_warning <- function(x, options) {
63   x <- gsub("\n$", "", x)
64   stringr::str_c("\n\n~~~\n",
65     paste0(x, collapse = "\n"),
66     "\n~~~\n{: .warning}\n\n")
67 }
68
69 knit_hooks$set(source = hook_in, output = hook_out, warning = hook_warning,
70   error = hook_error, message = hook_out)