# forked from https://github.com/yihui/knitr/blob/master/R/defaults.R
#' @importFrom stats setNames
new_defaults <- function(value = list()) {
defaults <- value
get <- function(name, default = FALSE, drop = TRUE, regex=FALSE, ...) {
if (default) defaults <- value # this is only a local version
if (missing(name)) {
defaults
} else {
if (drop && length(name) == 1) {
if (regex) {
name_grep <- grep(name, names(defaults), value = TRUE, ...)
stats::setNames(defaults[name_grep], name_grep)
} else {
defaults[[name]]
}
} else {
stats::setNames(defaults[name], name)
}
}
}
set <- function(...) {
dots <- list(...)
if (length(dots) == 0) return()
if (is.null(names(dots)) && length(dots) == 1 && is.list(dots[[1]])) {
if (length(dots <- dots[[1]]) == 0) {
return()
}
}
defaults <<- merge(dots)
invisible(NULL)
}
merge <- function(values) merge_list(defaults, values)
restore <- function(target = value) defaults <<- target
append <- function(...) {
dots <- list(...)
if (length(dots) == 0) return()
if (is.null(names(dots)) && length(dots) == 1 && is.list(dots[[1]])) {
if (length(dots <- dots[[1]]) == 0) {
return()
}
}
dots <- sapply(names(dots), function(x) dots[[x]] <- c(defaults[[x]], dots[[x]]), simplify = FALSE)
defaults <<- merge(dots)
invisible(NULL)
}
list(get = get, set = set, append = append, merge = merge, restore = restore)
}
#' @title Default and current chunky options
#'
#' @description Options for functions in the chunky package. When running R code, the object \code{chunky_opts}
#' (default options) is not modified by chunk headers (local chunk options are
#' merged with default options), whereas \code{chunky_opts_current} (current options)
#' changes with different chunk headers and it always reflects the options for
#' the current chunk.
#'
#' Normally we set up the global options once in the first code chunk in a
#' document using \code{chunky_opts$set()}, so that all \emph{latter} chunks will
#' use these options. Note the global options set in one chunk will not affect
#' the options in this chunk itself, and that is why we often need to set global
#' options in a separate chunk.
#'
#' Below is a list of default chunk options, retrieved via
#' \code{chunky_opts$get()}:
#'
#' \Sexpr[results=verbatim]{str(chunky::chunky_opts$get())}
# @references Usage: \url{https://yihui.name/knitr/objects/}
#'
# A list of available options:
# \url{https://yihui.name/knitr/options/#chunk_options}
#' @note \code{chunky_opts_current} is read-only in the sense that it does nothing if
#' you call \code{chunky_opts_current$set()}; you can only query the options via
#' \code{chunky_opts_current$get()}.
#' @export
#' @rdname chunkyOpts
#' @examples chunky_opts$get('name')
chunky_opts <- new_defaults(list(
basic=FALSE,
name='chunky',
counter=TRUE,
chunk_opts=NULL,
full_doc=FALSE,
token='^#{2} -{4}(.*?)-{4+}$'
))
#' @rdname chunkyOpts
#' @export
chunky_opts_current <- new_defaults()
# merge elements of y into x with the same names
merge_list <- function(x, y) {
x[names(y)] <- y
x
}
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.