haozhu233 / kableExtra

Construct Complex Table with knitr::kable() + pipe.
https://haozhu233.github.io/kableExtra/
Other
692 stars 148 forks source link

How to set fluid column widths? #690

Open mtennekes opened 2 years ago

mtennekes commented 2 years ago

I'd like to have fluid columns, with a certain maximum width (say 6em) and a minimum width (say 1em), irrespective of the content.

How can I achieve this with kableExtra?

My attempt so far:

library(kableExtra)

df = data.frame(A = c("Fixed width"), B = "Fixed width", C = "Fluid (min width 1em)", D = "Fluid (min width 1em)",
E = "Fluid (min width 1em)", F = "Fluid (min width 1em)", G = "Fluid (min width 1em)", H = "Fluid (min width 1em)")

kableExtra::kbl(df, table.attr = "table-layout: fixed") %>% 
    column_spec(1, width = "5em") %>%
    column_spec(2, width = "5em") %>% 
    column_spec(3, bold = TRUE, width_max = "20m", width_min = "1em", 
        extra_css = "white-space: nowrap; overflow: hidden; text-overflow: ellipsis;") %>% 
    column_spec(4, bold = TRUE, width_max = "20m", width_min = "1em")
mtennekes commented 2 years ago

Gentle reminder:-) Also related: https://github.com/mtennekes/cols4all/issues/7

dmurdoch commented 11 months ago

I don't understand what you are asking for. What's wrong with width_min and width_max? (Assuming it's not just the typo in the max values in your sample code.)