Search code examples
rr-markdownkablekableextra

Kable column width in html rmarkdown


I want to display a wide kable table in html rmarkdown to display the first column has long text without any line breaks. When I set the first column's width using column_spec, it doesn't seem to have any effect on the resulting html table. Here's my code. Is there a way to not have any line breaks for the first column? Thanks!

df <- data.frame(v1 = c('Long text without line breaks', 'Long text without line breaks', 'Long text without line breaks'),
                 v2 = c('text', 'text', 'text'),
                 v3 = c('text', 'text', 'text'),
                 v4 = c('text', 'text', 'text'),
                 v5 = c('text', 'text', 'text'),
                 v6 = c('text', 'text', 'text'),
                 v7 = c('text', 'text', 'text'),
                 v8 = c('text', 'text', 'text'),
                 v9 = c('text', 'text', 'text'),
                 v10 = c('text', 'text', 'text'),
                 v11 = c('text', 'text', 'text'),
                 v12 = c('text', 'text', 'text'),
                 v13 = c('text', 'text', 'text'),
                 v14 = c('text', 'text', 'text'),
                 v15 = c('text', 'text', 'text'),
                 v16 = c('text', 'text', 'text'),
                 v17 = c('text', 'text', 'text'),
                 v18 = c('text', 'text', 'text'),
                 v19 = c('text', 'text', 'text')
                 )

gctbl <- kable(df, col.names = colnames(df), escape = F) %>%
  kable_styling(full_width = T) %>%
  column_spec(1, width = '10in')
  
gctbl

enter image description here


Solution

  • You'll need to set width_min in column_spec. I think 3in is already enough for your column 1 in the example.

    If you really need 10in for some reason, you can add scroll_box() to enable x-axis scrolling in the table (not included in my answer).

    kable(df, col.names = colnames(df), escape = F) %>%
      kable_styling(full_width = T) %>%
      column_spec(1, width_min = '3in')
    

    column_spec_width_min