I came across this question and basically I want to do the same thing, but set the cell padding only for certain columns.
R - gt package - Ability to control cell padding within table header and column labels?
This is my dataframe:
gt(data = mtcars) %>%
tab_header(
title = "mtcars dataset") %>%
tab_options(data_row.padding = px(10),
column_labels.padding = px(3),
heading.padding = px(5),
data_row.padding.horizontal = px(5))
And I want to apply the "data_row.padding.horizontal
" only to the last 4 columns for example. In my real case I have a vector full of column numbers, where I want to reduce the data_row.padding to 0. My desired output would be a data_row.padding for the whole table of 10, but in the last 4 columns I want padding 0. The helper function "locations" is not working for me and I dont really know how to work with a loop in this case.
One method you could use is setting the column width conditionally. I've included 2 examples of how to do this.
vs
.mtcars
so that all columns are not numeric, then assigned the width to numeric columns. Lastly, I centered no numeric columns so the padding variation would be more obviousHere are two examples of setting the column width conditionally.
Just the vs
column:
gt(data = mtcars) %>%
tab_header(
title = "mtcars dataset") %>%
cols_width(vs ~ pct(10))
All columns that are numeric
:
mtcars %>%
mutate(cyl = factor(cyl),
across(vs:carb, factor)) %>% # change some fields to factor
gt() %>%
tab_header(
title = "mtcars dataset") %>%
cols_width(where(is.numeric) ~ pct(15)) %>% # widths of numeric columns
cols_align(align = c("center"), # center non-numeric
columns = where(is.factor))