How to freeze the first column with `modelsummary`?

Viewed 17

Suppose I have an .rmd document that I want to knit into an html with a large number of models:

---
title: "Example Document"
subtitle: ""
output:
  rmarkdown::html_document:
    smart: true
    theme: spacelab
    number_sections: no
    toc: true
    toc_float:
      collapsed: true
    toc_depth: 4
---

```{r echo=FALSE}
library(modelsummary)
library(purrr)
dv <- 
mtcars %>% 
  colnames() %>% 
  .[-1]

models <- 
map(.x = dv,
    ~formula(paste0(.x, "~ mpg"))) %>% 
  map(~lm(.x, data = mtcars))


modelsummary(c(models, models, models))

Is it possible to "freeze" the leftmost "variable name" column (or in other words keep it fixed) so that when I scroll to the right, the row names are always visible?

As far as I know, this feature is not available yet in kableExtra but it is perhaps possible using DT.

1 Answers

Are you okay with using DT? If so, you can output the modelsummary() to data.frame, and pass to DT::datatable(), using extensions="FixedColumns":

DT::datatable(
  modelsummary(c(models,models,models), output = "data.frame") %>% 
    select(-c(1,3)),
  extensions = "FixedColumns",
  options=list(scrollX = TRUE, fixedColumns = list(leftColumns = 2))
)
Related