htmlrr-markdownkablehuxtable

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


This works for tables produced with kableExtra which is useful when inspecting long tables. I was wondering if anyone has an idea how this may be doable with huxtable.

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

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

```{r lib}
library(magrittr)
```

```{r kable}
iris %>%
  head(20) %>%
  kableExtra::kbl(caption = "a caption") %>%
  kableExtra::kable_styling(fixed_thead = TRUE)
```

```{r huxtable}
iris %>%
  head(33) %>%
  huxtable::hux() %>%
  huxtable::set_caption("a caption")
```

Related to: Is it possible to make a huxtable output hoverable?


Solution

  • Simply set position: sticky for each th element of huxtable tables.

    ---
    output: bookdown::html_document2
    ---
    
    ```{r lib}
    library(magrittr)
    ```
    
    ```{r huxtable}
    iris %>%
      head(33) %>%
      huxtable::hux() %>%
      huxtable::set_caption("a caption")
    ```
    
    
    ```{=html}
    <style>
    
    table.huxtable th {
      position: sticky;
      top: 0;
      background: white;
    }
    
    </style>
    
    ```