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 stringr::str_c("\n\n~~~\n",
41 paste0(x, collapse="\n"),
42 "\n~~~\n{: .language-r}\n\n")
45 hook_out <- function(x, options) {
46 x <- gsub("\n$", "", x)
47 stringr::str_c("\n\n~~~\n",
48 paste0(x, collapse="\n"),
49 "\n~~~\n{: .output}\n\n")
52 hook_error <- function(x, options) {
53 x <- gsub("\n$", "", x)
54 stringr::str_c("\n\n~~~\n",
55 paste0(x, collapse="\n"),
56 "\n~~~\n{: .error}\n\n")
59 knit_hooks$set(source = hook_in, output = hook_out, warning = hook_error,
60 error = hook_error, message = hook_out)