the spanner column label below the column label
soilkoya opened this issue · 5 comments
I'm an environmental scientist, specifically soil science, and I do a lot of table work in R. I put the units (e.g. g kg-1) below the COLUMN LABEL across multiple lines, but most table packages don't allow me to do that. I do this with other apps. It would be great if you could improve this. i hope that the spanner column label could be placed below the column label.
If you just want to put your units below the column labels, have a look at the last example for cols_label
Otherwise you may have to show examples for better understanding
Right now there is no native support for it, but you could work around by using multiple levels/layers of column spanners and hide the actual labels.
Here a quick and dirty approach (I use gt::extibble
and for "units" the datatypes):
data <- gt::exibble
table <- gt::gt(data)
# define datatypes as spanners
for (i in unique(sapply(data, class))) {
table <- table %>% gt::tab_spanner(
id = paste0("datatype", i),
label = paste0("<", i, ">"),
columns = !!names(sapply(data, class)[sapply(data, class) == i]),
level = 1
)
}
# define columns as spanners
for (i in 1:length(names(data))) {
table <- table %>% gt::tab_spanner(
id = paste0("colname", i),
label = names(data)[i],
columns = !!names(data)[i],
level = 2
)
}
table %>%
# hide actual columns
gt::tab_style(locations = gt::cells_column_labels(), style = gt::css(display = "none")) %>%
# prettier borders
gt::tab_style(locations = gt::cells_column_spanners(), style = cell_borders(
color = "#D3D3D3", sides = c("left", "right"), weight = px(1))) %>%
gt::tab_options(column_labels.border.bottom.width = 1, column_labels.border.top.width = 0)
Thanks for the great code. Soil scientists will be very happy!
gt
is a great package. However there will always be some use-cases that may never be implemented in a specific package. That's why there are so many packages out there.
As long as the function is not available or if there is a "not planned" answer from the maintainer, I'd suggest to to use an appropriate package with native support for a functionality that you are looking for... instead of finding an unstable workaround.
For one problem it might be okay to "hack" but if you are planning to be in your job for some more years, try to add more packages to your "standard toolset". flextable
or huxtable
are great alternatives to gt
with their own strengths and weaknesses