Debian Codegraph @

man/convert_chunk_header.Rd

raw
1 % Generated by roxygen2: do not edit by hand
2 % Please edit documentation in R/parser.R
3 \name{convert_chunk_header}
4 \alias{convert_chunk_header}
5 \title{Convert the in-header chunk option syntax to the in-body syntax}
6 \usage{
7 convert_chunk_header(
8 input,
9 output = NULL,
10 type = c("multiline", "wrap", "yaml"),
11 width = 0.9 * getOption("width")
12 )
13 }
14 \arguments{
15 \item{input}{File path to the document with code chunks to convert.}
16
17 \item{output}{The default \code{NULL} will output to console. Other values
18 can be a file path to write the converted content into or a function which
19 takes \code{input} as argument and returns a file path to write into (e.g.,
20 \code{output = identity} to overwrite the input file).}
21
22 \item{type}{This determines how the in-body options will be formatted.
23 \code{"mutiline"} (the default, except for \file{qmd} documents, for which
24 the default is \code{"yaml"}) will write each chunk option on a separate
25 line. Long chunk option values will be wrapped onto several lines, and you
26 can use \code{width = 0} to keep one line per option only. \code{"wrap"}
27 will wrap all chunk options together using
28 \code{\link[base:strwrap]{base::strwrap}()}. \code{"yaml"} will convert
29 chunk options to YAML.}
30
31 \item{width}{An integer passed to \code{base::strwrap()} for \code{type =
32 "wrap"} and \code{type = "multiline"}. If set to \code{0}, deactivate the
33 wrapping (for \code{type = "multiline"} only).}
34 }
35 \value{
36 A character vector of converted \code{input} when \code{output =
37 NULL}. The output file path with converted content otherwise.
38 }
39 \description{
40 This is a helper function for moving chunk options from the chunk header to
41 the chunk body using the new syntax.
42 }
43 \note{
44 Learn more about the new chunk option syntax in
45 \url{https://yihui.org/en/2022/01/knitr-news/}
46 }
47 \section{About \pkg{knitr} option syntax}{
48
49
50 Historical chunk option syntax have chunk option in the chunk header using
51 valid R syntax. This is an example for \verb{.Rmd} document
52 \preformatted{
53 ```\{r, echo = FALSE, fig.width: 10\}
54 ```
55 }
56
57 New syntax allows to pass option inside the chunk using several variants
58 \itemize{
59 \item Passing options one per line using valid R syntax. This corresponds to \code{convert_chunk_header(type = "multiline")}.
60 \preformatted{
61 ```\{r\}
62 #| echo = FALSE,
63 #| fig.width = 10
64 ```
65 }
66
67 \item Passing option part from header in-chunk with several line if wrapping is
68 needed. This corresponds to \code{convert_chunk_header(type = "wrap")}
69 \preformatted{
70 ```\{r\}
71 #| echo = FALSE, fig.width = 10
72 ```
73 }
74 \item Passing options key value pairs in-chunk using YAML syntax. Values are no
75 more R expression but valid YAML syntax. This corresponds to
76 \code{convert_chunk_header(type = "yaml")} (not implement yet).
77 \preformatted{```\{r\}
78 #| echo: false,
79 #| fig.width: 10
80 ```
81 }
82 }
83 }
84
85 \examples{
86 knitr_example = function(...) system.file("examples", ..., package = "knitr")
87 # Convert a document for multiline type
88 convert_chunk_header(knitr_example("knitr-minimal.Rmd"))
89 # Convert a document for wrap type
90 convert_chunk_header(knitr_example("knitr-minimal.Rmd"), type = "wrap")
91 # Reduce default wrapping width
92 convert_chunk_header(knitr_example("knitr-minimal.Rmd"), type = "wrap", width = 0.6 *
93 getOption("width"))
94 \dontrun{
95 # Explicitly name the output
96 convert_chunk_header("test.Rmd", output = "test2.Rmd")
97 # Overwrite the input
98 convert_chunk_header("test.Rmd", output = identity)
99 # Use a custom function to name the output
100 convert_chunk_header("test.Rmd", output = \(f) sprintf("\%s-new.\%s",
101 xfun::sans_ext(f), xfun::file_ext(f)))
102 }
103 }