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 lssons 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)
34 # The hooks below add html tags to the code chunks and their output so that they
35 # are properly formatted when the site is built.
37 hook_in <- function(x, options) {
38 stringr::str_c("\n\n~~~\n",
39 paste0(x, collapse="\n"),
43 hook_out <- function(x, options) {
44 x <- gsub("\n$", "", x)
45 stringr::str_c("\n\n~~~\n",
46 paste0(x, collapse="\n"),
47 "\n~~~\n{: .output}\n\n")
50 hook_error <- function(x, options) {
51 x <- gsub("\n$", "", x)
52 stringr::str_c("\n\n~~~\n",
53 paste0(x, collapse="\n"),
54 "\n~~~\n{: .error}\n\n")
57 knit_hooks$set(source = hook_in, output = hook_out, warning = hook_error,
58 error = hook_error, message = hook_out)