gt table - freeze columns (or make them more readable)
Asked Answered
B

1

9

I am creating a table using the gt package in R Studio, and I can't figure out a graceful way to make the columns more readable for tables with many rows.

Take the following code:

gt::sp500 %>% slice(1:100) %>%
  mutate(month = lubridate::month(date)) %>%
  group_by(month) %>%
  gt::gt(rowname_col = "date")

As you see below, the columns are visible through the first ~35 rows... enter image description here

But once you move beyond that, I find myself having to scroll up to remember which column is high and which is low, and whether adjusted close is in the 6th column or the 4th. Is there a way to either freeze those columns in place as you scroll (using the gt package), or easily repeat the column headers after each group (without messing with the underlying data)?

enter image description here

Bestir answered 9/10, 2020 at 16:54 Comment(1)
I believe at the moment there is no function in {gt} that allows for repeating headers easily. This issue on the topic might be helpful: github.com/rstudio/gt/issues/194. The {kable} package does a better job of repeating headers if you are interested.Compressor
Q
1

The problem is related to overflow property set in the outer div and .cell-output-display. Just unset them and it works. Here is a working example:

gt::sp500 %>% slice(1:100) %>%
  mutate(month = lubridate::month(date)) %>%
  group_by(month) %>%
  gt::gt(id="two",rowname_col = "date") %>% opt_css(
    css = "
    .cell-output-display {
      overflow-x: unset !important;
    }
    div#two {
      overflow-x: unset !important;
      overflow-y: unset !important;
    }
    #two .gt_col_heading {
      position: sticky !important;
      top: 0 !important;
    }
    ")

All credits go to 1 and 2.

Quean answered 4/9, 2023 at 21:30 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.