KableExtra in Rmarkdown

Load needed packages

pacman::p_load(tidyverse,kableExtra,toolkit)

KableExtra

Basic Tips

For Latex PDF

  • PIN table in the exact position:
    • kable_styling(latex_options = "hold_position") uses [!h]
    • kable_styling(latex_options = "HOLD_position") uses [H] and the float package.

For Html

  • Fixed Table Header Row: kable_styling(fixed_head = T)

Use print_kable function

dat<-mtcars[1:10, 1:4] 
dat%>%print_kable(rowname_label = 'car', num_col = 2)
car mpg cyl disp hp
Mazda RX4 21.0 6 160 110
Mazda RX4 Wag 21.0 6 160 110
Datsun 710 22.8 4 108 93
Hornet 4 Drive 21.4 6 258 110
Hornet Sportabout 18.7 8 360 175
car mpg cyl disp hp
Valiant 18.1 6 225.0 105
Duster 360 14.3 8 360.0 245
Merc 240D 24.4 4 146.7 62
Merc 230 22.8 4 140.8 95
Merc 280 19.2 6 167.6 123

Column/Row Specification

mtcars[1:8, 1:8] %>%
  print_kable()%>%
  column_spec(2, color = spec_color(mtcars$mpg[1:8])) %>%
  column_spec(6, color = "white",background = spec_color(mtcars$drat[1:8], end = 0.7))%>%
  row_spec(0, color='white',background = 'green')%>%
  row_spec(3:5, bold = T)
mpg cyl disp hp drat wt qsec vs
21.0 6 160.0 110 3.90 2.62 16.46 0
21.0 6 160.0 110 3.90 2.88 17.02 0
22.8 4 108.0 93 3.85 2.32 18.61 1
21.4 6 258.0 110 3.08 3.21 19.44 1
18.7 8 360.0 175 3.15 3.44 17.02 0
18.1 6 225.0 105 2.76 3.46 20.22 1
14.3 8 360.0 245 3.21 3.57 15.84 0
24.4 4 146.7 62 3.69 3.19 20.00 1

Cell Specification

  • cell_spec generates raw HTML or LaTeX code, make sure you remember to put escape = FALSE in kable.
  • At the same time, you have to escape special symbols including % manually by yourself
  • cell_spec needs a way to know whether you want html or latex. You can specify it via the options(knitr.table.format = doc.type)
dat%>%
  mutate(mpg=cell_spec(mpg, color = ifelse(mpg > 20, "red", "blue")))%>%
  print_kable(escape=FALSE)
mpg cyl disp hp
21 6 160.0 110
21 6 160.0 110
22.8 4 108.0 93
21.4 6 258.0 110
18.7 8 360.0 175
18.1 6 225.0 105
14.3 8 360.0 245
24.4 4 146.7 62
22.8 4 140.8 95
19.2 6 167.6 123

Add header rows to group columns

mtcars[1:5,1:6]%>%
  print_kable(rowname_label = 'car')%>%
  add_header_above(c(" " = 1, "Group 1" = 2, "Group 2" = 2, "Group 3" = 2))
Group 1
Group 2
Group 3
car mpg cyl disp hp drat wt
Mazda RX4 21.0 6 160 110 3.90 2.62
Mazda RX4 Wag 21.0 6 160 110 3.90 2.88
Datsun 710 22.8 4 108 93 3.85 2.32
Hornet 4 Drive 21.4 6 258 110 3.08 3.21
Hornet Sportabout 18.7 8 360 175 3.15 3.44

Group rows via labeling

mtcars[1:5, 1:6] %>%
  print_kable()%>%
  pack_rows("Group 1", 1, 2) %>%
  pack_rows("Group 2", 3, 5)
mpg cyl disp hp drat wt
Group 1
21.0 6 160 110 3.90 2.62
21.0 6 160 110 3.90 2.88
Group 2
22.8 4 108 93 3.85 2.32
21.4 6 258 110 3.08 3.21
18.7 8 360 175 3.15 3.44

Group rows via multi-row cell

dt <- data.frame(C1=rep(LETTERS[1:2],each=4),C2=rep(LETTERS[c(3:4,3:4)],each=2), C3 = 1:8)
dt%>%
  print_kable()%>%
  collapse_rows(columns = 1:2, valign = "middle")
C1 C2 C3
A C 1
2
D 3
4
B C 5
6
D 7
8
Associate Professor

My research focuses on the intersection of machine learning and Statistics, methods and algorithms for analyzing high-dimensional data.