htmlcssrr-markdownr-flextable

Is it possible to fix the head for a long html flextable?


I am trying to have sticky headers like tables produced with kableExtra which is useful when inspecting long tables and was wondering if anyone has an idea how this may be doable with flextable.

Here's an example Rmd to get an idea of what I mean. I'd like the flextable header to be fixed at the top of the page when scrolling down just like in the kable.

---
output: bookdown::html_document2
---

```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = FALSE)

library(flextable)
```

## My Kable

```{r mykable}
iris |>
  kableExtra::kbl(caption = "a caption") |>
  kableExtra::kable_styling(fixed_thead = TRUE)
```

## My Flextable

```{r myflextable}
flextable(iris) |>
  set_caption("a caption")
```

Solution

  • Similar to your other question, we can add some custom CSS:

    flextable::set_table_properties(opts_html = list(
        extra_css = "
          thead th {
            position: sticky !important;
            top: 0 !important;
            background-color: white !important;
            z-index: 1000 !important;
          }
        "
      ))