-
DescriptionHey! I want to create code chunks programmatically, and started with the following example from stackoverflow ---
title: "Programmatically Generating Code Chunks"
format: html
echo: false
output: asis
warning: false
---
## Summary list
Code to make a bunch of example models.
```{r}
#| echo: false
# A list of summaries:
library(palmerpenguins)
summary_list <- penguins |>
split(penguins$species) |>
lapply(function(dat){
lm(flipper_length_mm ~ body_mass_g, data = dat)
})
headings <- names(summary_list)
```
## Summary tabs
:::{.panel-tabset}
```{r, results='asis'}
#| warning: false
for (i in seq_along(summary_list)) {
cat("# ",headings[i],"\n")
cat("```\n")
print(summary(summary_list[[i]]))
cat("\n```\n")
cat("\n\n")
}
```
::: This works as expected. At first. The more I dug into it, the more I got lost. ## tabsets make the echo disapear?
:::{.panel-tabset}
```{r}
#| warning: false
#| echo: true
for (i in seq_along(headings)) {
cat("### ", headings[i],"\n")
cat("```\n")
print(headings[i])
cat("\n```\n")
cat("\n\n")
}
```
::: I also don't understand why not everything is ## why `print(headings[i])` instead of `cat("print(headings[i])")`
```{r}
#| warning: false
for (i in seq_along(headings)) {
cat("### ",headings[i],"\n")
cat("```\n")
cat("print(headings[i])")
cat("\n```\n")
cat("\n\n")
}
``` When I wanted to investigate, I first got rid of the loop...and discovered that the example, which I pulled, uses ## lose the loop
```{r}
#| warning: false
cat("```\n")
print(headings[1])
cat("\n```\n")
cat("\n\n")
``` versus ## lose the loop with "```{r}```"
```{r}
cat("```{r}\n")
print(headings[1])
cat("\n```\n")
cat("\n\n")
``` Now, where are getting to the problem that started this all: including execute options. ## include "include = false"
```{r}
cat("```{r}\n")
cat("#| include: false\n")
print(headings[1])
cat("\n```\n")
cat("\n\n")
```
As per the suggestion of a stafckoverflow user I tried the following ## include "include = false" differently (inconsistent interpretations of the steps in the loop?)
```{r}
for (i in seq_along(headings)) {
cat("```{r, include=FALSE}\n")
print(headings[i])
cat("\n```\n")
cat("\n\n")
}
``` When I wanted to change some things around, I copy pasted that chunk, and just knitted the doc without actually changing something. Apparently, this breaks the styling. ## same chunk as above, but this time the header styling is broken
```{r}
for (i in seq_along(headings)) {
cat("```{r, include=FALSE}\n")
print(headings[i])
cat("\n```\n")
cat("\n\n")
}
```
## styling is back Lastly, since ## styling is back
```{r}
for (i in seq_along(headings)) {
cat("```{r}\n")
#| include: false
print(headings[i])
cat("\n```\n")
cat("\n\n")
}
``` I suspect that this is either an issue of timing in terms of the knitr pipeline, or, as the second to last chunks seem to suggest a problem with fencing the chunks? To wrap this up: I want to create code chunks programmatically, but lack understanding of...well, it's of everthing. My specs: Here are the chunks from above in a single doc: ---
title: "Programmatically Generating Code Chunks"
format: html
echo: false
output: asis
warning: false
---
## Summary list
Code to make a bunch of example models.
```{r}
#| echo: false
# A list of summaries:
library(palmerpenguins)
summary_list <- penguins |>
split(penguins$species) |>
lapply(function(dat){
lm(flipper_length_mm ~ body_mass_g, data = dat)
})
headings <- names(summary_list)
```
## Summary tabs
:::{.panel-tabset}
```{r, results='asis'}
#| warning: false
for (i in seq_along(summary_list)) {
cat("# ",headings[i],"\n")
cat("```\n")
print(summary(summary_list[[i]]))
cat("\n```\n")
cat("\n\n")
}
```
:::
## tabsets make the echo disapear?
:::{.panel-tabset}
```{r}
#| warning: false
#| echo: true
for (i in seq_along(headings)) {
cat("### ", headings[i],"\n")
cat("```\n")
print(headings[i])
cat("\n```\n")
cat("\n\n")
}
```
:::
## why `print(headings[i])` instead of `cat("print(headings[i])")`
```{r}
#| warning: false
for (i in seq_along(headings)) {
cat("### ",headings[i],"\n")
cat("```\n")
cat("print(headings[i])")
cat("\n```\n")
cat("\n\n")
}
```
## lose the loop
```{r}
#| warning: false
cat("```\n")
print(headings[1])
cat("\n```\n")
cat("\n\n")
```
## lose the loop with "```{r}```"
```{r}
cat("```{r}\n")
print(headings[1])
cat("\n```\n")
cat("\n\n")
```
## include "include = false"
```{r}
cat("```{r}\n")
cat("#| include: false\n")
print(headings[1])
cat("\n```\n")
cat("\n\n")
```
## include "include = false" differently (inconsistent interpretations of the steps in the loop?)
```{r}
for (i in seq_along(headings)) {
cat("```{r, include=FALSE}\n")
print(headings[i])
cat("\n```\n")
cat("\n\n")
}
```
## same chunk as above, but this time the header styling is broken
```{r}
for (i in seq_along(headings)) {
cat("```{r, include=FALSE}\n")
print(headings[i])
cat("\n```\n")
cat("\n\n")
}
```
## styling is back
```{r}
for (i in seq_along(headings)) {
cat("```{r}\n")
#| include: false
print(headings[i])
cat("\n```\n")
cat("\n\n")
} |
Beta Was this translation helpful? Give feedback.
Replies: 2 comments 2 replies
-
(I did not read all) You need to use Related discussions: https://github.com/quarto-dev/quarto-cli/discussions?discussions_q=knit_child On a side note: ```{r, results='asis'}
#| warning: false
for (i in seq_along(summary_list)) {
cat("# ",headings[i],"\n")
cat("```\n")
print(summary(summary_list[[i]]))
cat("\n```\n")
cat("\n\n")
}
``` Use ```{r}
#| warning: false
#| output: asis
for (i in seq_along(summary_list)) {
cat("# ",headings[i],"\n")
cat("```\n")
print(summary(summary_list[[i]]))
cat("\n```\n")
cat("\n\n")
}
``` |
Beta Was this translation helpful? Give feedback.
-
Well, this certainly works, thank you so much for your help, as well as your side note! Quick follow-up question, if I may: PS: since this is also an open question on stackoverflow, if you answer there, I'd accept it. Otherwise, I answer it myself. |
Beta Was this translation helpful? Give feedback.
(I did not read all)
You need to use
knitr::knit_child
(and eventuallyknitr::knit_expand
), see https://bookdown.org/yihui/rmarkdown-cookbook/child-document.html.That's how
knitr
works.Related discussions: https://github.com/quarto-dev/quarto-cli/discussions?discussions_q=knit_child
On a side note:
Don't mix code cell options syntaxes, inline R syntax and YAML syntax:
Use