How to conditonally include child .qmd file in Quarto?

I have a quarto file metav1.qmd. It needs to contain many child files such as child1.qmd, child2.qmd,..., child100.qmd. All child files need to be condionally included in metav1.qmd.

For example, uncoditonally including child1.qmd (this is included in quarto document):
{{< include child1.qmd >}}

How to make it conditional? I tried the following:

temp_file <- "include_temp1.qmd"
child1<-max(nrow(data1),nrow(data2))
if (child1) {
  writeLines("{{< include child1.qmd >}}", con = temp_file)
} else {writeLines("", con = temp_file)}

{{< include include_temp1.qmd >}}

unlink("include_temp1.qmd")

I ran above code to let Quarto conditionally include the child file child1.qmd, however, Quarto does not allow to create a temporary file inlcude_temp.qmd. This made above code practically not feasible as we need to run the code again and again but it won't let us to update the temporary file after unlink(). But abvoe code can create include_temp1.qmd when run it in R console.

I alos run the following
{{ h#if child1}}
{{< include include_temp1.qmd >}}
{{/if}

It does not run the condition head {{h#if child1}} and condition end {{/if} but only run {{< include include_temp1.qmd >}}. Here, "h" in front of hash sign# should be removed when run this code and I added it here for showing the hash sign# purpose

I tried

{r childchunk, if(child1){"child1.qmd"}}

It does not work since {r childchunk, if(child1){"child1.qmd"}} is an R Markdown command but RMarkdown does not compartible with Quarto since R Markdown is earlier than Quarto.

In summary, how to let Quarto to conditonally include child file? how to let Quarto to create .qmd file in the main .qmd file?

Hi @Chandler,

What condition(s) do you want to use to determine whether a child is included?

Could you give sample parent and child .rmd files where conditional inclusion works as you describe? They can be very simple files, but will be helpful to folks here who would like to help.

Solution: With knitr Engine, We Can Condtionally Include Child Files

For python engine, I do not have a solution yet. Thank you David and John.

The parent file meta1.qmd with knitr as engine:

---
title: "Conditionally Include and Run Child File with engine of knitr"
format: pdf
editor: visual
---
```{r} 
cv96 = T
cv97 = F
```

```{r , child=if(cv96){'child1.qmd'}}
```
```{r , child=if(c796){'child2.qmd'}}
```

The 1st child file child1.qmd:

---
title: "Child One"
format: pdf
editor: visual
---
Analyze data by Poisson mixed model

The 2nd child file child2.qmd:

 ---
title: "Child Two"
format: pdf
editor: visual
---
Analyze data by normal control chart if data is normally distributed

Above works in Quartio when we select knitr as engine and all files are in the same directory.

1 Like

Hi @Chandler, would you mind editing your last post so that the .rmd code is in code blocks? As it is now, it's hard to know what the files were or what the code was that you verified worked. In other words, could you post .qmd (or .rmd) files here in markdown code blocks so that folks reading your post can simply copy and paste what you share so that it runs on their machines, too, without triggering any errors?

@Chandler

To put your code in code blocks just copy your code and paste it here between
```

``

Thank you, John. If put the three apostrophs in front of {r, child=...}, format changed again. So, that part, I did not add but R code writers understand there should be there.

Those are not apostrophes. They are what I think may be called a "back tick" in English. In French it is an "accent grave" . An example would be E accent grave–È.

On an English language QUETY keyboard it is the leftmost key on the top row just below the Esc, It looks like you are using it here

 `{r, child=...},  format changed again.`

Even using this the code probably will look a bit rough but it should be more readable.

Thanks. I updated the code. Now we can see which is a block of code and which are comments

The code you shared above mixes r and python — have you tried replacing all the references to 'r' by 'python' in the code cells?

No, that should do not matter. In quarto, when the block is in R, use {r} and if it is in Python, use {python}.

---
title: "Conditionally Include and Run Child File"
format: pdf
editor: visual
jupyter: python3
---

## Child file does not work

```{python}
cv96 = True
cv97 = False
```

```{python, child=if(cv96){'child1.qmd'}}
```
```{python, child=if(cv97){'child2.qmd'}}
```

Above does not work for the child file even I added python engine in child files.

---
title: "Conditionally Include and Run Child File"
format: pdf
editor: visual
jupyter: python3
---

## Child file does not work

```{python}
cv96 = True
cv97 = False

if cv96:
    with open('child1.qmd') as f:
        child1_content = f.read()
    print(child1_content)

if cv97:
    with open('child2.qmd') as f:
        child2_content = f.read()
    print(child2_content)
```

This also does not work
I will use knitr first and when I compltely changed to Python, I will think about it again. Thank you.

Since I don't know exactly what use-case you have in mind, this may not help, but another possibility is to use code blocks to create child .qmd files, where just the header is created if the condition isn't met. That way, you could use the {{ <include child.qmd> }}, and if it's empty, no content is included.

If nothing else the code blocks work well. It is much easier to read your code. Thanks.

Yes, this type condition needs to create a temparary .qmd file when run the main .qmd file. And then I found when we run .qmd file, we cannot create .qmd file as I wrote at the first block.

This is a good idea and I realized if I created the temporary file in R console.

Thank you, David.

Yes, one block of above code works well when we use knitr as engine. It can conditionally include child file and works perfectly. Here we provided solution to conditionally include child file which enriches quarto topics at Quarto – Includes)

child option inside a cell is a knitr only feature. Quarto inherits it from knitr itself.

include shortcode is a Quarto feature, though there is no easy to way to conditionnaly include it based on computation results.

We are trying to thing about this problem for a language agnostic solution, but that is not easy.

For now, one could "hack" a solution using when-meta conditional coupled with computation cell output metadata block. This would allow to ignore the include in the output. Though it would not prevent the computation inside the included document to run.

Anyhow, for any knitr user, the child feature is a good one !

This topic was automatically closed 90 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.