Warning, /tutorial-analysis/bin/chunk-options.R is written in an unsupported language. File is not indexed.
0001 # These settings control the behavior of all chunks in the novice R materials.
0002 # For example, to generate the lessons with all the output hidden, simply change
0003 # `results` from "markup" to "hide".
0004 # For more information on available chunk options, see
0005 # http://yihui.name/knitr/options#chunk_options
0006
0007 library("knitr")
0008
0009 fix_fig_path <- function(pth) file.path("..", pth)
0010
0011
0012 ## We set the path for the figures globally below, so if we want to
0013 ## customize it for individual episodes, we can append a prefix to the
0014 ## global path. For instance, if we call knitr_fig_path("01-") in the
0015 ## first episode of the lesson, it will generate the figures in
0016 ## `fig/rmd-01-`
0017 knitr_fig_path <- function(prefix) {
0018 new_path <- paste0(opts_chunk$get("fig.path"),
0019 prefix)
0020 opts_chunk$set(fig.path = new_path)
0021 }
0022
0023 ## We use the rmd- prefix for the figures generated by the lessons so
0024 ## they can be easily identified and deleted by `make clean-rmd`. The
0025 ## working directory when the lessons are generated is the root so the
0026 ## figures need to be saved in fig/, but when the site is generated,
0027 ## the episodes will be one level down. We fix the path using the
0028 ## `fig.process` option.
0029
0030 opts_chunk$set(tidy = FALSE, results = "markup", comment = NA,
0031 fig.align = "center", fig.path = "fig/rmd-",
0032 fig.process = fix_fig_path,
0033 fig.width = 8.5, fig.height = 8.5,
0034 fig.retina = 2)
0035
0036 # The hooks below add html tags to the code chunks and their output so that they
0037 # are properly formatted when the site is built.
0038
0039 hook_in <- function(x, options) {
0040 lg <- tolower(options$engine)
0041 style <- paste0(".language-", lg)
0042
0043 stringr::str_c("\n\n~~~\n",
0044 paste0(x, collapse="\n"),
0045 "\n~~~\n{: ", style, "}\n\n")
0046 }
0047
0048 hook_out <- function(x, options) {
0049 x <- gsub("\n$", "", x)
0050 stringr::str_c("\n\n~~~\n",
0051 paste0(x, collapse="\n"),
0052 "\n~~~\n{: .output}\n\n")
0053 }
0054
0055 hook_error <- function(x, options) {
0056 x <- gsub("\n$", "", x)
0057 stringr::str_c("\n\n~~~\n",
0058 paste0(x, collapse="\n"),
0059 "\n~~~\n{: .error}\n\n")
0060 }
0061
0062 hook_warning <- function(x, options) {
0063 x <- gsub("\n$", "", x)
0064 stringr::str_c("\n\n~~~\n",
0065 paste0(x, collapse = "\n"),
0066 "\n~~~\n{: .warning}\n\n")
0067 }
0068
0069 knit_hooks$set(source = hook_in, output = hook_out, warning = hook_warning,
0070 error = hook_error, message = hook_out)