R/parser.R
convert_chunk_header.Rd
This is a helper function for moving chunk options from the chunk header to the chunk body using the new syntax.
File path to the document with code chunks to convert.
The default NULL
will output to console. Other values
can be a file path to write the converted content into or a function which
takes input
as argument and returns a file path to write into (e.g.,
output = identity
to overwrite the input file).
This determines how the in-body options will be formatted.
"mutiline"
(the default, except for qmd
documents, for which
the default is "yaml"
) will write each chunk option on a separate
line. Long chunk option values will be wrapped onto several lines, and you
can use width = 0
to keep one line per option only. "wrap"
will wrap all chunk options together using
base::strwrap()
. "yaml"
will convert
chunk options to YAML.
An integer passed to base::strwrap()
for type =
"wrap"
and type = "multiline"
. If set to 0
, deactivate the
wrapping (for type = "multiline"
only).
A character vector of converted input
when output =
NULL
. The output file path with converted content otherwise.
Learn more about the new chunk option syntax in https://yihui.org/en/2022/01/knitr-news/
Historical chunk option syntax have chunk option in the chunk header using
valid R syntax. This is an example for .Rmd
document
```{r, echo = FALSE, fig.width: 10}
```
New syntax allows to pass option inside the chunk using several variants
Passing options one per line using valid R syntax. This corresponds to convert_chunk_header(type = "multiline")
.
```{r}
#| echo = FALSE,
#| fig.width = 10
```
Passing option part from header in-chunk with several line if wrapping is
needed. This corresponds to convert_chunk_header(type = "wrap")
```{r}
#| echo = FALSE, fig.width = 10
```
Passing options key value pairs in-chunk using YAML syntax. Values are no
more R expression but valid YAML syntax. This corresponds to
convert_chunk_header(type = "yaml")
(not implement yet).
```{r}
#| echo: false,
#| fig.width: 10
```
knitr_example = function(...) system.file("examples", ..., package = "knitr")
# Convert a document for multiline type
convert_chunk_header(knitr_example("knitr-minimal.Rmd"))
#> [1] "# A Minimal Example for Markdown"
#> [2] ""
#> [3] "This is a minimal example of using **knitr** to produce an _HTML_ page from _Markdown_."
#> [4] ""
#> [5] "## R code chunks"
#> [6] ""
#> [7] "```{r}"
#> [8] "#| label = \"setup\""
#> [9] "# set global chunk options: images will be 7x5 inches"
#> [10] "knitr::opts_chunk$set(fig.width=7, fig.height=5)"
#> [11] "options(digits = 4)"
#> [12] "```"
#> [13] ""
#> [14] "Now we write some code chunks in this markdown file:"
#> [15] ""
#> [16] "```{r}"
#> [17] "#| label = \"computing\""
#> [18] "x <- 1+1 # a simple calculator"
#> [19] "set.seed(123)"
#> [20] "rnorm(5) # boring random numbers"
#> [21] "```"
#> [22] ""
#> [23] "We can also produce plots:"
#> [24] ""
#> [25] "```{r}"
#> [26] "#| label = \"graphics\""
#> [27] "par(mar = c(4, 4, .1, .1))"
#> [28] "with(mtcars, {"
#> [29] " plot(mpg~hp, pch=20, col='darkgray')"
#> [30] " lines(lowess(hp, mpg))"
#> [31] "})"
#> [32] "```"
#> [33] ""
#> [34] "## Inline code"
#> [35] ""
#> [36] "Inline R code is also supported, e.g. the value of `x` is `r x`, and 2 × π"
#> [37] "= `r 2*pi`."
#> [38] ""
#> [39] "## Math"
#> [40] ""
#> [41] "LaTeX math as usual: $f(\\alpha, \\beta) \\propto x^{\\alpha-1}(1-x)^{\\beta-1}$."
#> [42] ""
#> [43] "## Misc"
#> [44] ""
#> [45] "You can indent code chunks so they can nest within other environments such as lists."
#> [46] ""
#> [47] "1. the area of a circle with radius x"
#> [48] " ```{r}"
#> [49] " #| label = \"foo\""
#> [50] " pi * x^2"
#> [51] " ```"
#> [52] "2. OK, that is great"
#> [53] ""
#> [54] "To compile me, use"
#> [55] ""
#> [56] "```{r}"
#> [57] "#| label = \"compile\","
#> [58] "#| eval = FALSE"
#> [59] "library(knitr)"
#> [60] "knit('knitr-minimal.Rmd')"
#> [61] "```"
#> [62] ""
#> [63] "## Conclusion"
#> [64] ""
#> [65] "Markdown is super easy to write. Go to **knitr** [homepage](https://yihui.org/knitr/) for details."
# Convert a document for wrap type
convert_chunk_header(knitr_example("knitr-minimal.Rmd"), type = "wrap")
#> [1] "# A Minimal Example for Markdown"
#> [2] ""
#> [3] "This is a minimal example of using **knitr** to produce an _HTML_ page from _Markdown_."
#> [4] ""
#> [5] "## R code chunks"
#> [6] ""
#> [7] "```{r}"
#> [8] "#| setup"
#> [9] "# set global chunk options: images will be 7x5 inches"
#> [10] "knitr::opts_chunk$set(fig.width=7, fig.height=5)"
#> [11] "options(digits = 4)"
#> [12] "```"
#> [13] ""
#> [14] "Now we write some code chunks in this markdown file:"
#> [15] ""
#> [16] "```{r}"
#> [17] "#| computing"
#> [18] "x <- 1+1 # a simple calculator"
#> [19] "set.seed(123)"
#> [20] "rnorm(5) # boring random numbers"
#> [21] "```"
#> [22] ""
#> [23] "We can also produce plots:"
#> [24] ""
#> [25] "```{r}"
#> [26] "#| graphics"
#> [27] "par(mar = c(4, 4, .1, .1))"
#> [28] "with(mtcars, {"
#> [29] " plot(mpg~hp, pch=20, col='darkgray')"
#> [30] " lines(lowess(hp, mpg))"
#> [31] "})"
#> [32] "```"
#> [33] ""
#> [34] "## Inline code"
#> [35] ""
#> [36] "Inline R code is also supported, e.g. the value of `x` is `r x`, and 2 × π"
#> [37] "= `r 2*pi`."
#> [38] ""
#> [39] "## Math"
#> [40] ""
#> [41] "LaTeX math as usual: $f(\\alpha, \\beta) \\propto x^{\\alpha-1}(1-x)^{\\beta-1}$."
#> [42] ""
#> [43] "## Misc"
#> [44] ""
#> [45] "You can indent code chunks so they can nest within other environments such as lists."
#> [46] ""
#> [47] "1. the area of a circle with radius x"
#> [48] " ```{r}"
#> [49] " #| foo"
#> [50] " pi * x^2"
#> [51] " ```"
#> [52] "2. OK, that is great"
#> [53] ""
#> [54] "To compile me, use"
#> [55] ""
#> [56] "```{r}"
#> [57] "#| compile, eval=FALSE"
#> [58] "library(knitr)"
#> [59] "knit('knitr-minimal.Rmd')"
#> [60] "```"
#> [61] ""
#> [62] "## Conclusion"
#> [63] ""
#> [64] "Markdown is super easy to write. Go to **knitr** [homepage](https://yihui.org/knitr/) for details."
# Reduce default wrapping width
convert_chunk_header(knitr_example("knitr-minimal.Rmd"), type = "wrap", width = 0.6 *
getOption("width"))
#> [1] "# A Minimal Example for Markdown"
#> [2] ""
#> [3] "This is a minimal example of using **knitr** to produce an _HTML_ page from _Markdown_."
#> [4] ""
#> [5] "## R code chunks"
#> [6] ""
#> [7] "```{r}"
#> [8] "#| setup"
#> [9] "# set global chunk options: images will be 7x5 inches"
#> [10] "knitr::opts_chunk$set(fig.width=7, fig.height=5)"
#> [11] "options(digits = 4)"
#> [12] "```"
#> [13] ""
#> [14] "Now we write some code chunks in this markdown file:"
#> [15] ""
#> [16] "```{r}"
#> [17] "#| computing"
#> [18] "x <- 1+1 # a simple calculator"
#> [19] "set.seed(123)"
#> [20] "rnorm(5) # boring random numbers"
#> [21] "```"
#> [22] ""
#> [23] "We can also produce plots:"
#> [24] ""
#> [25] "```{r}"
#> [26] "#| graphics"
#> [27] "par(mar = c(4, 4, .1, .1))"
#> [28] "with(mtcars, {"
#> [29] " plot(mpg~hp, pch=20, col='darkgray')"
#> [30] " lines(lowess(hp, mpg))"
#> [31] "})"
#> [32] "```"
#> [33] ""
#> [34] "## Inline code"
#> [35] ""
#> [36] "Inline R code is also supported, e.g. the value of `x` is `r x`, and 2 × π"
#> [37] "= `r 2*pi`."
#> [38] ""
#> [39] "## Math"
#> [40] ""
#> [41] "LaTeX math as usual: $f(\\alpha, \\beta) \\propto x^{\\alpha-1}(1-x)^{\\beta-1}$."
#> [42] ""
#> [43] "## Misc"
#> [44] ""
#> [45] "You can indent code chunks so they can nest within other environments such as lists."
#> [46] ""
#> [47] "1. the area of a circle with radius x"
#> [48] " ```{r}"
#> [49] " #| foo"
#> [50] " pi * x^2"
#> [51] " ```"
#> [52] "2. OK, that is great"
#> [53] ""
#> [54] "To compile me, use"
#> [55] ""
#> [56] "```{r}"
#> [57] "#| compile, eval=FALSE"
#> [58] "library(knitr)"
#> [59] "knit('knitr-minimal.Rmd')"
#> [60] "```"
#> [61] ""
#> [62] "## Conclusion"
#> [63] ""
#> [64] "Markdown is super easy to write. Go to **knitr** [homepage](https://yihui.org/knitr/) for details."
if (FALSE) { # \dontrun{
# Explicitly name the output
convert_chunk_header("test.Rmd", output = "test2.Rmd")
# Overwrite the input
convert_chunk_header("test.Rmd", output = identity)
# Use a custom function to name the output
convert_chunk_header("test.Rmd", output = \(f) sprintf("%s-new.%s",
xfun::sans_ext(f), xfun::file_ext(f)))
} # }