1+ roxygen2_knitr_note <- function () {
2+ sprintf(
3+ paste(
4+ " `%s` `knitr` utilities are exported for use in" ,
5+ " reports maintained by the R Validation Hub." ,
6+ " If you choose to use these functions for other purposes, be aware" ,
7+ " that these are not considered stable for broader use."
8+ ),
9+ packageName()
10+ )
11+ }
12+
113# ' Handler for complex option passing through from a quarto parameter
214# '
315# ' Importantly, handles [`S7::new_class()`] objects which cannot be passed
416# ' through as a `quarto` command-line parameter because they can not be
517# ' deparsed. This helper allows passing arbitrary expressions using the
618# ' `!expr` prefix, which is standardized by `yaml`.
719# '
20+ # ' @note `r roxygen2_knitr_note()`
21+ # '
822# ' @param opts A `list` of options. For any non-serializable values that would
923# ' fail using your preferred interface to `knitr`, you may pass them as an
1024# ' expression string such as `"!expr mtcars"`. Notably,
@@ -57,32 +71,25 @@ knitr_update_options <- function(opts, envir = parent.frame()) {
5771# ' a mutable environment that we can modify, which will be used to update the
5872# ' knitr document header upon render completion.
5973# '
74+ # ' @note `r roxygen2_knitr_note()`
75+ # '
6076# ' @param params Optionally, provide default parameters to initialize with
6177# ' @param envir Only used when `params` is not provided, as the source for where
6278# ' to try to discover the default knitr frontmatter parameters.
6379# '
6480# ' @importFrom yaml yaml.load
6581# ' @export
66- knitr_mutable_header <- function (params = NULL , envir = parent.frame() ) {
82+ knitr_mutable_header <- function () {
6783 header <- new.env(parent = emptyenv())
6884
69- # if not provided, try to get params from envir
70- if (is.null(params )) {
71- params <- get0(" params" , envir = envir , ifnotfound = list ())
72- }
73-
74- # initialize with param values
75- for (name in names(params )) {
76- header [[name ]] <- params [[name ]]
77- }
78-
7985 # add a hook that will replace the hard-coded front-matter with dynamic
8086 # front-matter just before rendering.
8187 knitr :: knit_hooks $ set(
8288 document = local({
8389 default_document_hook <- knitr :: knit_hooks $ get(" document" )
84- function (x , output ) {
90+ function (x , options ) {
8591 # extract and split our document front-matter and body
92+ x <- paste(x , collapse = " \n " )
8693 body <- sub(" ^\\ s*(---|\\ .\\ .\\ .).*\\ 1" , " " , x )
8794 fm <- substring(x , 0L , nchar(x ) - nchar(body ))
8895
@@ -115,7 +122,13 @@ knit_print.knitr_log <- local({
115122 prefix <- " \u 205A " # vertical two dot punctuation
116123 last_log_trailing_newline <- FALSE
117124
118- function (x , ... ) {
125+ # "progress" is used by knitr::knit function internally to store whether
126+ # progress should be written to console, equivalent to !quiet
127+ function (x , ... , quiet = ! knitr :: opts_knit $ get(" progress" )) {
128+ if (quiet ) {
129+ return ()
130+ }
131+
119132 # prefix newline only for the first message in each chunk
120133 knitr_log_env <- environment(knitr_logger )
121134 first_chunk_log <- knitr_log_env $ first_chunk_log
@@ -156,6 +169,7 @@ knit_print.knitr_log <- local({
156169 if (first_chunk_log ) {
157170 cat(" \n " , file = stderr(), sep = " " )
158171 }
172+
159173 cat(x , file = stderr(), sep = " " )
160174 }
161175})
@@ -165,6 +179,8 @@ knit_print.knitr_log <- local({
165179# ' Sets necessary knitr hooks and returns a logging function that will emit
166180# ' messages to the console during knitting.
167181# '
182+ # ' @note `r roxygen2_knitr_note()`
183+ # '
168184# ' @return A `function` accepting `...` arguments, which will be used for
169185# ' printing out to the console while rendering the knitr document. Character
170186# ' values are logged directrly, while any other object is printed as though
0 commit comments