12

嗨,我用 制作了这张很棒的桌子kableExtra,但我唯一的问题是行的高度并不总是相等。有谁知道解决这个问题的方法?

我的桌子:

在此处输入图像描述

例如,如您所见,项目编号 22(第 6 行)的行具有比其他行更大的高度(间距)。

我的代码:

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()
4

2 回答 2

15

行高不总是相等的原因是默认情况下每 5 行kable插入一个。\addlinespace要摆脱它,请linesep = ""放入kable(). 有关详细信息,请参阅摆脱 kable 中的 \addlinespace

于 2017-12-05T16:59:54.937 回答
2

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.

于 2017-12-05T13:55:05.770 回答