convert_chunk_header | R Documentation |
This is a helper function for moving chunk options from the chunk header to the chunk body using the new syntax.
convert_chunk_header(
input,
output = NULL,
type = c("multiline", "wrap", "yaml"),
width = 0.9 * getOption("width")
)
input |
File path to the document with code chunks to convert. |
output |
The default |
type |
This determines how the in-body options will be formatted.
|
width |
An integer passed to |
A character vector of converted input
when output =
NULL
. The output file path with converted content otherwise.
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 ```
Learn more about the new chunk option syntax in https://yihui.org/en/2022/01/knitr-news/
knitr_example = function(...) system.file("examples", ..., package = "knitr")
# Convert a document for multiline type
convert_chunk_header(knitr_example("knitr-minimal.Rmd"))
# Convert a document for wrap type
convert_chunk_header(knitr_example("knitr-minimal.Rmd"), type = "wrap")
# Reduce default wrapping width
convert_chunk_header(knitr_example("knitr-minimal.Rmd"), type = "wrap", width = 0.6 *
getOption("width"))
## Not run:
# 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)))
## End(Not run)
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.