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

991 views Asked by At

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

1

There are 1 answers

0
RSA On

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.