77# ' the default parameter values in the R code it emits.
88# '
99# ' @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+ # '
1114# ' @return List of objects of class \code{knit_param} that correspond to the
1215# ' parameters declared in the \code{params} section of the YAML front matter.
1316# ' These objects have the following fields:
1417# '
1518# ' \describe{
1619# ' \item{\code{name}}{The parameter name.}
1720# ' \item{\code{value}}{The default value for the parameter.}
18- # ' \item{\code{class}}{The R class names of the parameter's default value.}
1921# ' \item{\code{expr}}{The R expression (if any) that yielded the default value.}
2022# ' }
2123# '
6870# ' }
6971# '
7072# ' @export
71- knit_params = function (text ) {
73+ knit_params = function (text , evaluate = TRUE ) {
7274
7375 # make sure each element is on one line
7476 text = split_lines(text )
@@ -78,12 +80,33 @@ knit_params = function(text) {
7880 if (is.null(yaml )) return (list ())
7981
8082 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 ) {
81104 # 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 ))
83106
84107 # if we found paramters then resolve and return them
85108 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 )
87110 } else {
88111 list ()
89112 }
@@ -149,14 +172,20 @@ yaml_front_matter = function(lines) {
149172
150173
151174# define custom handlers for knitr_params
152- knit_params_handlers = function () {
175+ knit_params_handlers = function (evaluate = TRUE ) {
153176
154177 # generic handler for r expressions where we want to preserve both the original
155178 # code and the fact that it was an expression.
156179 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
160189 }
161190
162191 list (
@@ -189,7 +218,7 @@ knit_params_handlers = function() {
189218
190219# resolve the raw params list into the full params data structure (with name,
191220# type, value, and other optional fields included)
192- resolve_params = function (params ) {
221+ resolve_params = function (params , evaluate = TRUE ) {
193222
194223 # get the expr attribute (if any)
195224 expr_attr = function (value ) {
@@ -241,16 +270,6 @@ resolve_params = function(params) {
241270 # normalize parameter value (i.e. strip attributes, list -> vector)
242271 param $ value = param_value(param $ value )
243272
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-
254273 # add knit_param class
255274 param = structure(param , class = " knit_param" )
256275
0 commit comments