Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

the spanner column label below the column label #1869

Open
soilkoya opened this issue Aug 31, 2024 · 5 comments
Open

the spanner column label below the column label #1869

soilkoya opened this issue Aug 31, 2024 · 5 comments
Assignees

Comments

@soilkoya
Copy link

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.

@obsaditelnost
Copy link
Contributor

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

@soilkoya
Copy link
Author

soilkoya commented Sep 3, 2024

Is it possible to put together columns with the same unit like this?
image

@obsaditelnost
Copy link
Contributor

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)

@soilkoya
Copy link
Author

soilkoya commented Sep 5, 2024

Thanks for the great code. Soil scientists will be very happy!

@obsaditelnost
Copy link
Contributor

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants