# These settings control the behavior of all chunks in the novice R materials.
# For example, to generate the lessons with all the output hidden, simply change
# `results` from "markup" to "hide".
# For more information on available chunk options, see
# http://yihui.name/knitr/options#chunk_options
## Setting the directory where `fig/` will live. This is the top of the lesson
## repository and will resolve to the absolute path.
knitr::opts_knit$set(base.dir = normalizePath(".."))
## Function to fix the rendered paths for Jekyll. The reason for this is
## that Jekyll renders each episode inside its own folder with index.html,
## but the shared folders are still above those directories. This tells Jekyll
## to look up before it pulls the figure.
##
## Note that this resolves to the relative path.
fix_fig_path <- function(pth) file.path("..", pth)
## We set the path for the figures globally below, so if we want to
## customize it for individual episodes, we can append a prefix to the
## global path. For instance, if we call knitr_fig_path("01-") in the
## first episode of the lesson, it will generate the figures in
## `fig/rmd-01-`
knitr_fig_path <- function(prefix) {
new_path <- paste0(
knitr::opts_chunk$get("fig.path"),
prefix
)
knitr::opts_chunk$set(fig.path = new_path)
}
## We use the rmd- prefix for the figures generated by the lessons so
## they can be easily identified and deleted by `make clean-rmd`. The
## working directory when the lessons are generated is the root so the
## figures need to be saved in fig/, but when the site is generated,
## the episodes will be one level down. We fix the path using the
## `fig.process` option.
knitr::opts_chunk$set(
tidy = FALSE,
results = "markup",
comment = NA,
fig.align = "center",
fig.path = "fig/rmd-",
fig.width = 8.5,
fig.height = 8.5,
fig.retina = 2,
fig.process = fix_fig_path
)
# The hooks below add html tags to the code chunks and their output so that they
# are properly formatted when the site is built.
hook_in <- function(x, options) {
lg <- tolower(options$engine)
style <- paste0(".language-", lg)
paste0(
"\n\n~~~\n",
paste0(x, collapse = "\n"),
"\n~~~\n{: ", style, "}\n\n"
)
}
hook_out <- function(x, options) {
x <- gsub("\n$", "", x)
paste0(
"\n\n~~~\n",
paste0(x, collapse = "\n"),
"\n~~~\n{: .output}\n\n"
)
}
hook_error <- function(x, options) {
x <- gsub("\n$", "", x)
paste0(
"\n\n~~~\n",
paste0(x, collapse = "\n"),
"\n~~~\n{: .error}\n\n"
)
}
hook_warning <- function(x, options) {
x <- gsub("\n$", "", x)
paste0(
"\n\n~~~\n",
paste0(x, collapse = "\n"),
"\n~~~\n{: .warning}\n\n"
)
}
knitr::knit_hooks$set(
source = hook_in, output = hook_out, warning = hook_warning,
error = hook_error, message = hook_out
)
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.