Dynamic chunks in Quarto

I have a tibble with images ids, captions and labels to create a dynamic Quarto document from a repeated template. After reading the post s below, I still only know what not to do in Quarto (compared to rmarkdown). I learned that the old method in my code using knit_expand and knit_child is sort of "if you cannot avoid it", but what is the recommended way?


---
title: "Dynamic Images Blocks"
execute: 
  echo: false
---


```{r}
  library(dplyr)
  library(knitr)
  library(glue)
  imgs = tribble (
    ~id,  ~title,           ~caption,         ~label,
     1,   "First Title",   "First caption",   "first",       
     2,   "Second Title",  "Second caption",  "second",       
     3,   "Third Title",   "Third caption",   "third",       
  )
```


## `r imgs$title[1]`

::: {#fig-`r imgs$label[1]`}
`r include_graphics(glue("https://picsum.photos/id/{imgs$id[1]}/200.jpg"))`
`r imgs$caption[1]`
:::

@fig-`r imgs$label[1]` has label  fig-`r imgs$label[1]`. 

The above block was generated manually  with the `<div>` syntax. 
I would like it to be repeated for all images.

```{r}
child_text = "
## {{img$title[1]}}

![{{img$caption[1]}}](https://picsum.photos/id/{{img$id[1]}}/200.jpg){#fig-{{img$label[1]}}}

@fig-{{img$label[1]}} has label fig-{{img$label[1]}}. 
"
```


```{r}
#| output: asis
expand_knit = function(img) {
 expanded = knit_expand(text = child_text, img = img)
 cat(knit_child(text = expanded, quiet = TRUE), sep = "\n")
}

imgs  |> 
  rowwise() |> 
  group_walk(~expand_knit(.x)) 
```

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.