Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Controlling row height in kableExtra()

Hi i made this awesome table with kableExtra, but my only problem is that the height of the rows is not always equal. Does any one know a remedy for this?

my table:

enter image description here

for example, as you can see, the line for item number 22 (6th row) has a larger height (spacing) than other lines.

my code:

my_column_names = c("Item number", "Item", 
                "Emotion", "Social", 
                "At Home", "Body", "Emotion", 
                "Social 1", "Social 2", 
                "At Home", "Body") 

kable(df1,
      format = "latex", booktabs = TRUE,
      col.names = my_column_names,
      caption = "Factor loadings for the 4 and 5 Factor Model") %>%
      kable_styling(latex_options = c("striped", "hold_position"),
                    full_width = FALSE) %>% 
      add_header_above(c(" " = 2, 
                 "4 Factor Model " = 4, "5 Factor model" = 5)) %>%
      add_header_above(c(" " = 2, 
                 "Model" = 9)) %>%
      kableExtra::landscape()
like image 578
Benjamin Telkamp Avatar asked Dec 05 '17 13:12

Benjamin Telkamp


2 Answers

Usually, this is something that you can change via CSS in an HTML table. Not sure how to do this with kableExtra but you might want to consider tableHTML to do it. I am adding a small example below to demonstrate row height:

library(tableHTML)
tableHTML(mtcars[1:10, ], 
          border = 1,
          rownames = TRUE, 
          caption = 'This is a caption',
          footer = 'This is a footer',
          widths = c(140, rep(50, 11)),
          second_headers = list(c(2, 5, 6), c('', 'col2', 'col3')),
          theme = 'scientific') %>%
  add_css_row(list('height', '50px'), rows = 3:12)

enter image description here

You don't need to use the scientific theme if you don't want to. The package gives you flexibility to add any css you like (like striped rows, etc.). You can check a tutorial here if interested.

P.S. It currently only supports one extra header. Apart from that your whole table can be replicated.

like image 195
LyzandeR Avatar answered Nov 18 '22 12:11

LyzandeR


The reason why the row height is not always equal is that by default kable inserts a \addlinespace every 5th rows. To get rid of it, put linesep = "" in kable(). See Get rid of \addlinespace in kable for details.

like image 16
Hao Avatar answered Nov 18 '22 13:11

Hao