7
7
# ' the default parameter values in the R code it emits.
8
8
# '
9
9
# ' @param text Character vector containing the document text
10
- # '
10
+ # ' @param evaluate If TRUE, expression values embedded within the YAML will be
11
+ # ' evaluated. This is the default. When FALSE, parameters defined by an
12
+ # ' expression will have the parsed expression in its \code{value} field.
13
+ # '
11
14
# ' @return List of objects of class \code{knit_param} that correspond to the
12
15
# ' parameters declared in the \code{params} section of the YAML front matter.
13
16
# ' These objects have the following fields:
14
17
# '
15
18
# ' \describe{
16
19
# ' \item{\code{name}}{The parameter name.}
17
20
# ' \item{\code{value}}{The default value for the parameter.}
18
- # ' \item{\code{class}}{The R class names of the parameter's default value.}
19
21
# ' \item{\code{expr}}{The R expression (if any) that yielded the default value.}
20
22
# ' }
21
23
# '
68
70
# ' }
69
71
# '
70
72
# ' @export
71
- knit_params = function (text ) {
73
+ knit_params = function (text , evaluate = TRUE ) {
72
74
73
75
# make sure each element is on one line
74
76
text = split_lines(text )
@@ -78,12 +80,33 @@ knit_params = function(text) {
78
80
if (is.null(yaml )) return (list ())
79
81
80
82
yaml = enc2utf8(yaml )
83
+ knit_params_yaml(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 TRUE, expression values embedded within the YAML will be
93
+ # ' evaluated. This is the default. When FALSE, parameters defined by an
94
+ # ' expression will have the parsed expression in its \code{value} field.
95
+ # '
96
+ # ' @return List of objects of class \code{knit_param} that correspond to the
97
+ # ' parameters declared in the \code{params} section of the YAML. See
98
+ # ' \code{\link{knit_params}} for a full description of these objects.
99
+ # '
100
+ # ' @seealso \code{\link{knit_params}}
101
+ # '
102
+ # ' @export
103
+ knit_params_yaml = function (yaml , evaluate = TRUE ) {
81
104
# parse the yaml using our handlers
82
- parsed_yaml = yaml :: yaml.load(yaml , handlers = knit_params_handlers())
105
+ parsed_yaml = yaml :: yaml.load(yaml , handlers = knit_params_handlers(evaluate = evaluate ))
83
106
84
107
# if we found paramters then resolve and return them
85
108
if (is.list(parsed_yaml ) && ! is.null(parsed_yaml $ params )) {
86
- resolve_params(mark_utf8(parsed_yaml $ params ))
109
+ resolve_params(mark_utf8(parsed_yaml $ params ), evaluate = evaluate )
87
110
} else {
88
111
list ()
89
112
}
@@ -149,14 +172,20 @@ yaml_front_matter = function(lines) {
149
172
150
173
151
174
# define custom handlers for knitr_params
152
- knit_params_handlers = function () {
175
+ knit_params_handlers = function (evaluate = TRUE ) {
153
176
154
177
# generic handler for r expressions where we want to preserve both the original
155
178
# code and the fact that it was an expression.
156
179
expr_handler = function (value ) {
157
- evaluated_value = eval(parse_only(value ))
158
- attr(evaluated_value , " expr" ) = value
159
- evaluated_value
180
+ expression = parse_only(value )
181
+ transformed_value = if (evaluate ) {
182
+ eval(expression )
183
+ } else {
184
+ # When we are not evaluating, provide the parsed expression as the transformed value
185
+ expression
186
+ }
187
+ attr(transformed_value , " expr" ) = value
188
+ transformed_value
160
189
}
161
190
162
191
list (
@@ -189,7 +218,7 @@ knit_params_handlers = function() {
189
218
190
219
# resolve the raw params list into the full params data structure (with name,
191
220
# type, value, and other optional fields included)
192
- resolve_params = function (params ) {
221
+ resolve_params = function (params , evaluate = TRUE ) {
193
222
194
223
# get the expr attribute (if any)
195
224
expr_attr = function (value ) {
@@ -241,16 +270,6 @@ resolve_params = function(params) {
241
270
# normalize parameter value (i.e. strip attributes, list -> vector)
242
271
param $ value = param_value(param $ value )
243
272
244
- # record parameter class (must be explicit for null values)
245
- if (! is.null(param $ value )) {
246
- param $ class = class(param $ value )
247
- } else {
248
- if (is.null(param $ class ))
249
- stop(" no class field specified for YAML parameter '" , name , " '" ,
250
- " (fields with a value of null must specify an explicit class)" ,
251
- call. = FALSE )
252
- }
253
-
254
273
# add knit_param class
255
274
param = structure(param , class = " knit_param" )
256
275
0 commit comments