convert_chunk_header: Convert the in-header chunk option syntax to the in-body...

View source: R/parser.R

convert_chunk_headerR Documentation

Convert the in-header chunk option syntax to the in-body syntax

Description

This is a helper function for moving chunk options from the chunk header to the chunk body using the new syntax.

Usage

convert_chunk_header(
  input,
  output = NULL,
  type = c("multiline", "wrap", "yaml"),
  width = 0.9 * getOption("width")
)

Arguments

input

File path to the document with code chunks to convert.

output

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).

type

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.

width

An integer passed to base::strwrap() for type = "wrap" and type = "multiline". If set to 0, deactivate the wrapping (for type = "multiline" only).

Value

A character vector of converted input when output = NULL. The output file path with converted content otherwise.

About knitr option syntax

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
    ```
    

Note

Learn more about the new chunk option syntax in https://yihui.org/en/2022/01/knitr-news/

Examples

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)

knitr documentation built on Sept. 11, 2024, 6:18 p.m.