quarto

Is it possible to conditionally include a file in a quarto document?


This doc shows how to include a file:

{{< include _content.qmd >}}

And this doc shows how to conditionally include based on a metadata file:

::: {.content-hidden unless-meta="path.to.metadata"}

This content will be hidden unless there exists a metadata entry like such:

```yml
path:
  to:
    metadata: true
```

:::

However, I want to include a section if a data frame has some rows in it, i.e. an R boolean expression.

Is that possible?


Solution

  • How about this:

    ---
    format: html
    ---
    
    ## first section
    
    ```{r}
    nrow(mtcars)
    ```
    
    
    ```{r}
    #| results: asis
    #| echo: false
    if(nrow(mtcars) > 10) {
      paste(
        "",
      "{{< include _example_include.qmd >}}",
      "") |> cat(sep = "\n")
    }
    ```
    

    with _example_include.qmd:

    ## section cond
    
    Load the `sp500` from [`great_tables`](https://posit-dev.github.io/great-tables/).
    

    Result: enter image description here