gt table: freeze first column after tbl_merge

55 Views Asked by At

I would like to freeze the first column (the 'label' one) in a gt table. I tried the code here but it doesn't work as expected.

library(gt)

example <- 
  data.frame(
    sex = rep(c("Female","Male"),6),
    income = rep(c("[0,100[","Not applicable","[200,300[","[200,300[","I rather not to answer","[300,400["),2)
  )

tbl_merge(list(tbl_summary(example, by="sex"),tbl_summary(example, by="sex"))) %>%
  as_gt(id = "one") %>%
  tab_options(
    container.height = "400px",
    container.width = "200px"
  ) %>%
  opt_css(
    css = "
    #one .gt_stub {
    position: sticky;
    left: 0;
    }
    
    #one .gt_col_heading {
    position: sticky;
    top: 0;
    }

    #one .gt_row {
    position: sticky;
    left: 0;
    }
    
    thead th:first-child {
    left: 0;
    z-index: 2;
    }
    ")

With the inclusion of #one .gt_row I managed to freeze the first column (not the footnote although), but the other columns (including in the header) override it when scrolling.

Any suggestion on how to fix it?

0

There are 0 best solutions below