Open eeholmes opened 2 years ago
Currently chapters do indeed need to be in different qmd files. The best way to share data between chapters is to use the save()
and load()
functions: https://jjallaire.github.io/hopr/a4-data.html#saving-r-files
The main issue is not actually sharing data between chapters. Rather it is arising when we don't know how many chapters we will need. This is common when using R Markdown (and now) Quarto for government reports. We are dynamically creating the content (meaning the content in the qmd).
Here is a repo where I am working on a template for government reports with dynamic content. https://github.com/RVerse-Tutorials/QmdReport/blob/main/README.md
The problem is most acute for appendices. This is the area where we are most likely to not know the number of chapters (appendices). Fortunately, Word and PDF output seems to figure out how to deal with the multiple h1 levels in the one appendix qmd. But the HTML is totally messed up. Look just at the appendices.
HTML: https://rverse-tutorials.github.io/QmdReport/ PDF: https://rverse-tutorials.github.io/QmdReport/A-Quarto-Template-Repo-to-Create-Big-Reports-and-Very-Long-Title-Because-Long-Titles-are-Common.pdf
Okay, got it! Dynamic number of HTML chapters we indeed do not handle well right now (mostly b/c we build a static index of them before rendering so that we can build the sidebar for each chapter as we go -- kind of a chicken and egg problem unless we move the generation of the sidebar entirely outside of pandoc rendering). Need to think more carefully about it but I am hopeful this is something we can ultimate resolve. Will target this for work in our v1.2 release (later this fall).
Got it. The Quarto default HTML looks so similar to bookdown::bs4_book that I assumed it was working under the hood in the same way.
This is generated with bookdown at this RStudio Cloud project: https://rstudio.cloud/content/4350137
I am pretty sure it is using Knit then Merge since new_session: true
. The two appendices are generated in 08-appendices.Rmd
with two #
levels in one file.
Anyhow I'll look forward to Quarto v1.2 in the fall!
Here is a related minimal example. Our real life example: a report with a standardised chapter for every species (almost 100 species and increasing). Currently we have the report as a bookdown::gitbook()
. The standardised chapter is defined as a child document and we loop over the species to generate the report. The example below works, but it adds all child chapters to a single index.html file. This becomes very large file. Whereas bookdown::gitbook()
allows to split the content over multiple files by chapter. Making the html files much smaller.
_quarto.yml
project:
type: book
book:
chapters:
- index.qmd
format:
html: default
index.qmd
# Main chapter
```{r}
#| label: setup
#| include: false
library(knitr)
opts_chunk$set(echo = FALSE)
library(tidyverse)
```{r}
#| label: child
#| results: asis
sample(LETTERS, 3) |>
sapply(
function(id) {
knit_expand("_child.qmd", id = id)
}
) %>%
paste(collapse = "\n") -> qmd
knit(text = qmd, quiet = TRUE) |>
cat()
```
`_child.qmd`
# {{id}}
```{r}
#| label: {{id}}-plot
ggplot(iris, aes(x = Sepal.Length, y = Petal.Length, colour = Species)) +
geom_point() +
ggtitle("{{id}}")
```
@ThierryO side note regarding your example :
```{r} #| label: child #| results: asis sample(LETTERS, 3) |> sapply( function(id) { knit_expand("_child.qmd", id = id) } ) %>% paste(collapse = "\n") -> qmd knit(text = qmd, quiet = TRUE) |> cat() ```
I am surprised you don't have issue with this. Usual pattern to do this is to use knitr::knit_child()
and not knit()
directly. knit_child()
is designed to knit content within a knitting process.
See knit_expand()
example in the R Markdown cookbook.
Quarto version: I tried a few version. Started with 1.0.37 then I updated to the latest dev version (says 99.9.9) using this
Then went back to v1.0.38 by re-installing. RStudio: 2022.02.2 OS: Mac Big Sur 11.6.2
This is specific to a Quarto book project.
If have 2 h1 headers in a chapter qmd, the first appears in TOC and the others appear with the right sequential chapter numbers but don't appear in the TOC and later chapters don't "know" about the new chapters.
Example
_quarto.yml
chap1and2.qmd
This is the output. Notice the TOC.
quarto check
Outputquarto tools check
OutputUse case, why is this needed?
In report writing situation, you often don't know how many chapters (appendices) you will need. You are using
knit_expand()
with an child.Rmd that is creating all your chapters (appendices) based on the data (which vary). Thatknit_expand()
is a for loop (or map function) and is creating the markdown dynamically. This works fine in Quarto...except that it doesn't recognize the later h1 levels as proper chapters.Checklist
quarto check
in the "Quarto Check Output" text area?quarto tools check
in the "Quarto Tools Check Output" text area?