Print a Stan file with syntax highlighting in Quarto and R Markdown
Source:R/file.R
print_stan_file.RdPrints the contents of a Stan file, optionally with syntax highlighting
when used in a Quarto or R Markdown document. When called inside a
knitr code chunk with the chunk option
output: asis (or results: asis in R Markdown), the output is a
fenced Stan code block that Quarto renders with syntax highlighting.
When called interactively or without output: asis, the code is
printed as plain text via writeLines().
Arguments
- file
(string) Path to a
.stanfile.- fold
(logical) Whether to wrap the output in an HTML
<details>block so that the code is collapsed (folded) by default. Only has an effect when rendering withoutput: asisand when outputting HTML. Defaults toFALSE.- summary
(string) The summary text shown in the fold toggle when
fold = TRUE. Defaults to"Stan model code".
Quarto usage
Use in a Quarto code chunk with output: asis to get syntax
highlighting:
To make the code block collapsible:
Examples
stan_file <- write_stan_file("
parameters {
real y;
}
model {
y ~ std_normal();
}
")
# Prints plain code at the console
print_stan_file(stan_file)
#>
#> parameters {
#> real y;
#> }
#> model {
#> y ~ std_normal();
#> }
#>