| 1 |
#' Extract knit parameters from a document |
| 2 |
#' |
| 3 |
#' This function reads the YAML front-matter section of a document and returns a |
| 4 |
#' list of any parameters declared there. This function exists primarily to |
| 5 |
#' support the parameterized reports feature of the \pkg{rmarkdown} package, |
| 6 |
#' however is also used by the knitr \code{\link{purl}} function to include |
| 7 |
#' the default parameter values in the R code it emits. |
| 8 |
#' |
| 9 |
#' @param text Character vector containing the document text. |
| 10 |
#' @param evaluate Boolean. If \code{TRUE} (the default), expression values embedded |
| 11 |
#' within the YAML will be evaluated. If \code{FALSE}, parameters defined with an |
| 12 |
#' expression will have the parsed but unevaluated expression in their \code{value} |
| 13 |
#' field. |
| 14 |
#' |
| 15 |
#' @return List of objects of class \code{knit_param} that correspond to the |
| 16 |
#' parameters declared in the \code{params} section of the YAML front matter. |
| 17 |
#' These objects have the following fields: |
| 18 |
#' |
| 19 |
#' \describe{ |
| 20 |
#' \item{\code{name}}{The parameter name.} |
| 21 |
#' \item{\code{value}}{The default value for the parameter.} |
| 22 |
#' \item{\code{expr}}{The R expression (if any) that yielded the default value.} |
| 23 |
#' } |
| 24 |
#' |
| 25 |
#' In addition, other fields included in the YAML may also be present |
| 26 |
#' alongside the name, type, and value fields (e.g. a \code{label} field |
| 27 |
#' that provides front-ends with a human readable name for the parameter). |
| 28 |
#' |
| 29 |
#' @details |
| 30 |
#' |
| 31 |
#' Parameters are included in YAML front matter using the \code{params} key. |
| 32 |
#' This key can have any number of subkeys each of which represents a |
| 33 |
#' parameter. For example: |
| 34 |
#' |
| 35 |
#' \preformatted{ |
| 36 |
#' --- |
| 37 |
#' title: My Document |
| 38 |
#' output: html_document |
| 39 |
#' params: |
| 40 |
#' frequency: 10 |
| 41 |
#' show_details: true |
| 42 |
#' --- |
| 43 |
#' } |
| 44 |
#' |
| 45 |
#' Parameter values can be provided inline as illustrated above or can be |
| 46 |
#' included in a \code{value} sub-key. For example: |
| 47 |
#' |
| 48 |
#' \preformatted{ |
| 49 |
#' --- |
| 50 |
#' title: My Document |
| 51 |
#' output: html_document |
| 52 |
#' params: |
| 53 |
#' frequency: |
| 54 |
#' value: 10 |
| 55 |
#' --- |
| 56 |
#' } |
| 57 |
#' |
| 58 |
#' This second form is useful when you need to provide additional details |
| 59 |
#' about the parameter (e.g. a \code{label} field as describe above). |
| 60 |
#' |
| 61 |
#' You can also use R code to yield the value of a parameter by prefacing the value |
| 62 |
#' with \code{!r}, for example: |
| 63 |
#' |
| 64 |
#' \preformatted{ |
| 65 |
#' --- |
| 66 |
#' title: My Document |
| 67 |
#' output: html_document |
| 68 |
#' params: |
| 69 |
#' start: !r Sys.Date() |
| 70 |
#' --- |
| 71 |
#' } |
| 72 |
#' |
| 73 |
#' @export |
| 74 |
knit_params = function(text, evaluate = TRUE) { |
| 75 |
|
| 76 |
# make sure each element is on one line |
| 77 |
text = split_lines(text) |
| 78 |
|
| 79 |
# read the yaml front matter and see if there is a params element in it |
| 80 |
yaml = yaml_front_matter(text) |
| 81 |
if (is.null(yaml)) return(list()) |
| 82 |
|
| 83 |
knit_params_yaml(enc2utf8(yaml), evaluate = evaluate) |
| 84 |
} |
| 85 |
|
| 86 |
#' Extract knit parameters from YAML text |
| 87 |
#' |
| 88 |
#' This function reads the YAML front-matter that has already been extracted |
| 89 |
#' from a document and returns a list of any parameters declared there. |
| 90 |
#' |
| 91 |
#' @param yaml Character vector containing the YAML text. |
| 92 |
#' @param evaluate If \code{TRUE} (the default) expression values |
| 93 |
#' embedded within the YAML will be evaluated. If \code{FALSE}, parameters |
| 94 |
#' defined with an expression will have the parsed but unevaluated expression |
| 95 |
#' in their \code{value} field. |
| 96 |
#' |
| 97 |
#' @return List of objects of class \code{knit_param} that correspond to the |
| 98 |
#' parameters declared in the \code{params} section of the YAML. See |
| 99 |
#' \code{\link{knit_params}} for a full description of these objects. |
| 100 |
#' |
| 101 |
#' @seealso \code{\link{knit_params}} |
| 102 |
#' |
| 103 |
#' @export |
| 104 |
knit_params_yaml = function(yaml, evaluate = TRUE) { |
| 105 |
# parse the yaml using our handlers |
| 106 |
parsed_yaml = yaml::yaml.load( |
| 107 |
yaml, handlers = knit_params_handlers(evaluate = evaluate), eval.expr = TRUE |
| 108 |
) |
| 109 |
|
| 110 |
# if we found paramters then resolve and return them |
| 111 |
if (is.list(parsed_yaml) && !is.null(parsed_yaml$params)) { |
| 112 |
resolve_params(parsed_yaml$params, evaluate = evaluate) |
| 113 |
} else { |
| 114 |
list() |
| 115 |
} |
| 116 |
} |
| 117 |
|
| 118 |
# turn params into a named list of values |
| 119 |
flatten_params = function(params) { |
| 120 |
res = list() |
| 121 |
for (param in params) res[[param$name]] = param$value |
| 122 |
res |
| 123 |
} |
| 124 |
|
| 125 |
# Extract the yaml front matter (if any) from the passed lines. The front |
| 126 |
# matter is returned as a single-element character vector (with newlines |
| 127 |
# delimited by \n) suitable for passing to yaml::load. This code is based on |
| 128 |
# the partition_yaml_front_matter and parse_yaml_front_matter functions here: |
| 129 |
# https://github.com/rstudio/rmarkdown/blob/master/R/output_format.R |
| 130 |
yaml_front_matter = function(lines) { |
| 131 |
|
| 132 |
# verify that the first two front matter delimiters (---) are not preceded |
| 133 |
# by other content |
| 134 |
has_front_matter = function(delimiters) { |
| 135 |
length(delimiters) >= 2 && (delimiters[2] - delimiters[1] > 1) && |
| 136 |
(delimiters[1] == 1 || is_blank(head(lines, delimiters[1] - 1))) && |
| 137 |
grepl("^---\\s*$", lines[delimiters[1]]) |
| 138 |
} |
| 139 |
|
| 140 |
# find delimiters in the document |
| 141 |
delimiters = grep("^(---|\\.\\.\\.)\\s*$", lines) |
| 142 |
|
| 143 |
# if it's valid then return front matter as a text block suitable for passing |
| 144 |
# to yaml::load |
| 145 |
if (!has_front_matter(delimiters)) return() |
| 146 |
|
| 147 |
# return the yaml as a single-element character vector if |
| 148 |
# appears to be valid yaml |
| 149 |
front_matter_lines = lines[(delimiters[1]):(delimiters[2])] |
| 150 |
if (length(front_matter_lines) <= 2) return() |
| 151 |
|
| 152 |
front_matter = front_matter_lines |
| 153 |
front_matter = front_matter[2:(length(front_matter) - 1)] |
| 154 |
if (length(grep('^params:', front_matter)) == 0) return() # no params in YAML |
| 155 |
front_matter = paste(front_matter, collapse = "\n") |
| 156 |
|
| 157 |
# ensure that the front-matter doesn't terminate with ':', so it won't cause a |
| 158 |
# crash when passed to yaml::load |
| 159 |
if (!grepl(":\\s*$", front_matter)) front_matter |
| 160 |
} |
| 161 |
|
| 162 |
|
| 163 |
# define custom handlers for knitr_params |
| 164 |
knit_params_handlers = function(evaluate = TRUE) { |
| 165 |
|
| 166 |
# generic handler for r expressions where we want to preserve both the original |
| 167 |
# code and the fact that it was an expression. |
| 168 |
expr_handler = function(value) { |
| 169 |
expression = parse_only(value) |
| 170 |
transformed_value = if (evaluate) { |
| 171 |
eval(expression) |
| 172 |
} else { |
| 173 |
# When we are not evaluating, provide the parsed expression as the transformed value |
| 174 |
expression |
| 175 |
} |
| 176 |
|
| 177 |
wrapped = list( |
| 178 |
value = transformed_value, |
| 179 |
expr = value) |
| 180 |
wrapped = structure(wrapped, class = "knit_param_expr") |
| 181 |
wrapped |
| 182 |
} |
| 183 |
|
| 184 |
list( |
| 185 |
|
| 186 |
# r expressions where we want to preserve both the original code |
| 187 |
# and the fact that it was an expression. |
| 188 |
r = expr_handler, |
| 189 |
expr = expr_handler, |
| 190 |
|
| 191 |
# date and datetime (for backward compatibility with previous syntax) |
| 192 |
date = function(value) { |
| 193 |
value = as.Date(value) |
| 194 |
value |
| 195 |
}, |
| 196 |
datetime = function(value) { |
| 197 |
value = as.POSIXct(value, tz = "GMT") |
| 198 |
value |
| 199 |
}, |
| 200 |
|
| 201 |
# workaround default yaml parsing behavior to allow keys named 'y' and 'n' |
| 202 |
`bool#yes` = function(value) { |
| 203 |
if (tolower(value) == "y") value else TRUE |
| 204 |
}, |
| 205 |
`bool#no` = function(value) { |
| 206 |
if (tolower(value) == "n") value else FALSE |
| 207 |
} |
| 208 |
) |
| 209 |
} |
| 210 |
|
| 211 |
|
| 212 |
# resolve the raw params list into the full params data structure (with name, |
| 213 |
# type, value, and other optional fields included) |
| 214 |
resolve_params = function(params, evaluate = TRUE) { |
| 215 |
|
| 216 |
# params we will return |
| 217 |
resolved_params = list() |
| 218 |
|
| 219 |
# iterate over names |
| 220 |
for (name in names(params)) { |
| 221 |
|
| 222 |
# get the parameter |
| 223 |
param = params[[name]] |
| 224 |
|
| 225 |
if (inherits(param, "knit_param_expr")) { |
| 226 |
# We have a key: !r expr |
| 227 |
param = list(expr = param$expr, value = param$value) |
| 228 |
} else if (is.list(param)) { |
| 229 |
if ("value" %in% names(param)) { |
| 230 |
# This looks like a complex parameter configuration. |
| 231 |
value = param$value |
| 232 |
if (inherits(value, "knit_param_expr")) { |
| 233 |
# We have a key: { value: !r expr } |
| 234 |
param$expr = value$expr |
| 235 |
param$value = value$value |
| 236 |
} |
| 237 |
} else { |
| 238 |
stop2("no value field specified for YAML parameter '", name, "'") |
| 239 |
} |
| 240 |
} else { |
| 241 |
# A simple key: value |
| 242 |
param = list(value = param) |
| 243 |
} |
| 244 |
|
| 245 |
# param is now always a named list. record name and add knit_param class. |
| 246 |
param$name = name |
| 247 |
param = structure(param, class = "knit_param") |
| 248 |
|
| 249 |
# add the parameter |
| 250 |
resolved_params[[name]] = param |
| 251 |
} |
| 252 |
|
| 253 |
# return params |
| 254 |
resolved_params |
| 255 |
} |