Skip to content

Consider different behavior in Quarto context for HTML tables #2598

@cderv

Description

@cderv
---
format: 
  html: default
  docx: default
---

```{r}
data.frame(apples = 1, bananas = 2, carrots = 3) |>
  knitr::kable() |>
  kableExtra::add_header_above(c("fruits" = 2, " " = 1))
```

This will throw an error in docx because knitr output will have some HTML dependencies passed by kableExtra and rmarkdown is being too protective by throwing an error.

Quarto has this special feature of knowing how to parse HTML table (https://quarto.org/docs/authoring/tables.html#html-tables) so it should be possible to support kableExtra output for non HTML format in quarto.

Maybe this is something to do directly in kableExtra knit_print method when in Quarto 🤔

This works

---
format: 
  docx: default
keep-md: true
---

```{r}
tab <- data.frame(apples = 1, bananas = 2, carrots = 3) |>
  knitr::kable() |>
  kableExtra::add_header_above(c("fruits" = 2, " " = 1))
```

```{r}
#| output: asis
#| echo: false
if (knitr::pandoc_to("docx")) {
  as.character(tab) |> knitr::raw_html()
}
```

And gt does not have this issue, possibly because it does not add HTML deps in the knitted output
so no error triggered

---
format: 
  docx: default
keep-md: true
---

```{r}
library(gt)
towny |>
  dplyr::slice_max(population_2021, n = 5) |>
  dplyr::select(
    name, latitude, longitude,
    ends_with("2016"), ends_with("2021")
  ) |>
  gt() |>
  tab_spanner(
    label = "Population",
    columns = starts_with("pop")
  ) |>
  tab_spanner(
    label = "Density",
    columns = starts_with("den")
  ) |>
  tab_spanner(
    label = md("*Location*"),
    columns = ends_with("itude"),
    id = "loc"
  )
```

rmarkdown/R/render.R

Lines 796 to 801 in 688b1cf

# if this isn't html and there are html dependencies then flag an error
if (!(is_pandoc_to_html(output_format$pandoc) ||
identical(tolower(xfun::file_ext(output_file)), "html"))) {
if (has_html_dependencies(knit_meta)) {
if (!isTRUE(front_matter$always_allow_html)) {
stop2("Functions that produce HTML output found in document targeting ",

Metadata

Metadata

Assignees

No one assigned

    Labels

    nextto consider for next release

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions