This function creates a code_chunk object that represents code to be displayed
in a report. It stores the code of any language (see lang argument) and any
specified chunk options (e.g., echo, eval).
Arguments
- code
(
character) The code to be displayed in the code chunk.- ...
Additional named parameters to be included as chunk options. These control how the chunk behaves when rendered (e.g.,
echo = TRUE,eval = FALSE,message = FALSE). Seeknitroptions for available options.- lang
(
character(1)) The language of the code chunk. Defaults to"R". Seeknitr::knit_enginesfor supported languages (e.g., "python", "bash").
Details
Important Notes:
The code is not evaluated; it is only stored as text with formatting attributes.
When converted to output,
code_chunkproduces markdown code block syntax (```{lang} ... ```) or HTML<pre><code>...</code></pre>blocks.The document is not rendered using
rmarkdown::render. Thecode_chunkis part of theteal_cardAPI for building reproducible documents that are produced as-is.
Typical Workflow:
Create a
code_chunkobject with your code and optionsAdd it to a
teal_cardusingteal_card()orc()The card produces the formatted code block in the final document output
See also
teal_card()for creating report cards that can containcode_chunkobjects
Examples
# Basic code chunk with options
code_chunk("x <- 1:10", echo = TRUE, message = FALSE)
#> [1] "x <- 1:10"
#> attr(,"params")
#> attr(,"params")$echo
#> [1] TRUE
#>
#> attr(,"params")$message
#> [1] FALSE
#>
#> attr(,"lang")
#> [1] "R"
#> attr(,"class")
#> [1] "code_chunk"
# Python code chunk
code_chunk("import pandas as pd", lang = "python", eval = FALSE)
#> [1] "import pandas as pd"
#> attr(,"params")
#> attr(,"params")$eval
#> [1] FALSE
#>
#> attr(,"lang")
#> [1] "python"
#> attr(,"class")
#> [1] "code_chunk"
# Code chunk with multiple knitr options
code_chunk(
"plot(mtcars$mpg, mtcars$hp)",
echo = TRUE,
eval = TRUE,
fig.width = 7,
fig.height = 5,
warning = FALSE
)
#> [1] "plot(mtcars$mpg, mtcars$hp)"
#> attr(,"params")
#> attr(,"params")$echo
#> [1] TRUE
#>
#> attr(,"params")$eval
#> [1] TRUE
#>
#> attr(,"params")$fig.width
#> [1] 7
#>
#> attr(,"params")$fig.height
#> [1] 5
#>
#> attr(,"params")$warning
#> [1] FALSE
#>
#> attr(,"lang")
#> [1] "R"
#> attr(,"class")
#> [1] "code_chunk"