Search code examples
rr-markdownmarkdowndtquarto

How do I add captions to DT:datatable()?


I often use exchangeably knitr::kable and DT::datatable to show tables in my reports, depending on how many rows the data has. I need these tables to be numbers, so that they can be referenced to in the document

The problem is that the following code does not produce a table caption and reference for the first table and I have to use weird workarounds that do not work reliably or completely switch to manual numbering of tables.

---
title: "Example"
format: html
---


As you can see in @tbl-iris, this example does not work. However, it does
work for `knitr::kable`, as you can see in @tbl-iris2.


```{r}
#| label: tbl-iris
#| tbl-cap: Example DT table
DT::datatable(head(iris))
```

```{r}
#| label: tbl-iris2
#| tbl-cap: Example kable table
knitr::kable(head(iris))
```

This is the output of quarto render test.qmd:

example output


Solution

  • Just for completeness: then answer is "install a new version of Quarto". Recent versions of Quarto handle captions to data tables correctly.