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
9 fix_fig_path <- function(pth) file.path("..", pth)
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
17 knitr_fig_path <- function(prefix) {
18 new_path <- paste0(opts_chunk$get("fig.path"),
20 opts_chunk$set(fig.path = new_path)
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.
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,
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.
39 hook_in <- function(x, options) {
40 lg <- tolower(options$engine)
41 style <- paste0(".language-", lg)
43 stringr::str_c("\n\n~~~\n",
44 paste0(x, collapse="\n"),
45 "\n~~~\n{: ", style, "}\n\n")
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")
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")
62 knit_hooks$set(source = hook_in, output = hook_out, warning = hook_error,
63 error = hook_error, message = hook_out)