Debian Codegraph @

R/knit.R

raw
1 #' knit engine
2 #'
3 #' You can include templates and their rendered output in an RMarkdown document.
4 #' This is achieved using the `jinjar` RMarkdown engine, which reads chunk options:
5 #'
6 #' * `data`: A named list of variables to pass to the template.
7 #' * `engine.opts`: A named list of engine options:
8 #' * `lang`: A string (e.g. `"sql"`, `"html"`) to specify the syntax
9 #' highlighting applied to the template and its rendered output.
10 #' * `config`: An engine configuration object (see [jinjar_config()]).
11 #' @noRd
12 knit_jinjar <- function(options) {
13 engine_output <- get("engine_output", envir = asNamespace("knitr"))
14
15 if (identical(.Platform$GUI, "RStudio") && is.character(options$data)) {
16 options$data <- get(options$data, envir = globalenv()) # nocov
17 }
18
19 code <- paste(options$code, collapse = "\n")
20 out <- if (options$eval) {
21 config <- options$engine.opts$config %||% default_config()
22 render(code, !!!options$data, .config = config)
23 } else ""
24
25 # override styling and syntax highlighting
26 lang <- options$engine.opts$lang
27 options$class.source <- c(options$class.source, lang, "bg-info")
28 options$class.output <- c(options$class.output, lang, "bg-success")
29 options$comment <- NULL
30
31 engine_output(options, code, out)
32 }