10.2 ThekableExtra package

ThekableExtra package(Zhu 2024) is designed to extend the basic functionality of tables produced usingknitr::kable() (see Section10.1). Sinceknitr::kable() is simple by design (please feel free to read this as “Yihui is lazy”), it definitely has a lot of missing features that are commonly seen in other packages, andkableExtra has filled the gap perfectly. The most amazing thing aboutkableExtra is that most of its table features work for both HTML and PDF formats (e.g., making striped tables like the one in Figure10.1).

This package can be installed from CRAN as usual, or you may try the development version on GitHub (https://github.com/haozhu233/kableExtra):

# install from CRANinstall.packages("kableExtra")# install the development versionremotes::install_github("haozhu233/kableExtra")

It has extensive documentation athttps://haozhu233.github.io/kableExtra/, which provides a lot of examples on how thekable() output can be customized for either HTML or LaTeX output. We recommend that you read its documentation by yourself, and will only present a handful of examples in this section.

ThekableExtra package features the pipe operator,%>%. You can pipe thekable() output to the styling functions ofkableExtra, e.g.,

library(knitr)library(kableExtra)kable(iris)%>%kable_styling(latex_options ="striped")

10.2.1 Set the font size

The functionkable_styling() inkableExtra allows you to style the whole table. For example, you can specify the alignment of the table on the page, the width, and the font size of the table. Below is an example of using a smaller font size:

kable(head(iris,5),booktabs =TRUE)%>%kable_styling(font_size =8)
Sepal.LengthSepal.WidthPetal.LengthPetal.WidthSpecies
5.13.51.40.2setosa
4.93.01.40.2setosa
4.73.21.30.2setosa
4.63.11.50.2setosa
5.03.61.40.2setosa

10.2.2 Style specific rows/columns

The functionsrow_spec() andcolumn_spec() can be used to style individual rows and columns, respectively. In the example below, we make the first row bold and italic, add a black background to the second and third rows while changing the font color to white, underline the fourth row and change its typeface, rotate the fifth row, and strike out the fifth column:

kable(head(iris,5),align ='c',booktabs =TRUE)%>%row_spec(1,bold =TRUE,italic =TRUE)%>%row_spec(2:3,color ='white',background ='black')%>%row_spec(4,underline =TRUE,monospace =TRUE)%>%row_spec(5,angle =45)%>%column_spec(5,strikeout =TRUE)
Sepal.LengthSepal.WidthPetal.LengthPetal.WidthSpecies
5.13.51.40.2setosa
4.93.01.40.2setosa
4.73.21.30.2setosa
4.63.11.50.2setosa
5.03.61.40.2setosa

Similarly, you can style individual cells with thecell_spec() function.

10.2.3 Group rows/columns

Rows and columns can be grouped via the functionspack_rows() andadd_header_above(), respectively. You can also collapse rows viacollapse_rows(), so one cell can span multiple rows. Below is an example that shows a custom table header with grouped columns:

iris2<- iris[1:5,c(1,3,2,4,5)]names(iris2)<-gsub('[.].+','',names(iris2))kable(iris2,booktabs =TRUE)%>%add_header_above(c("Length"=2,"Width"=2," "=1))%>%add_header_above(c("Measurements"=4,"More attributes"=1))
Measurements
More attributes
Length
Width
SepalPetalSepalPetalSpecies
5.11.43.50.2setosa
4.91.43.00.2setosa
4.71.33.20.2setosa
4.61.53.10.2setosa
5.01.43.60.2setosa

For the named vector inadd_header_above(), the names are the text to be shown in the table header, and the integer values of the vector indicate how many columns a name should span, e.g.,"Length" = 2 meansLength should span two columns.

Below is an example ofpack_rows(). The meaning of itsindex argument is similar to the argument ofadd_header_above() as we just explained before.

iris3<- iris[c(1:2,51:54,101:103), ]kable(iris3[,1:4],booktabs =TRUE)%>%pack_rows(index =c("setosa"=2,"versicolor"=4,"virginica"=3))
Sepal.LengthSepal.WidthPetal.LengthPetal.Width
setosa
15.13.51.40.2
24.93.01.40.2
versicolor
517.03.24.71.4
526.43.24.51.5
536.93.14.91.5
545.52.34.01.3
virginica
1016.33.36.02.5
1025.82.75.11.9
1037.13.05.92.1

10.2.4 Scaling down wide tables in LaTeX

There are a few features that are specific to the HTML or LaTeX output format. For example, landscape pages only make sense in LaTeX, so thelandscape() function inkableExtra only works for LaTeX output. Below we show an example to scale down a table to fit the page (otherwise it would be too wide):

tab<-kable(tail(mtcars,5),booktabs =TRUE)tab# original table (too wide)
mpgcyldisphpdratwtqsecvsamgearcarb
Lotus Europa30.4495.11133.771.51316.91152
Ford Pantera L15.88351.02644.223.17014.50154
Ferrari Dino19.76145.01753.622.77015.50156
Maserati Bora15.08301.03353.543.57014.60158
Volvo 142E21.44121.01094.112.78018.61142
tab%>%kable_styling(latex_options ="scale_down")
mpgcyldisphpdratwtqsecvsamgearcarb
Lotus Europa30.4495.11133.771.51316.91152
Ford Pantera L15.88351.02644.223.17014.50154
Ferrari Dino19.76145.01753.622.77015.50156
Maserati Bora15.08301.03353.543.57014.60158
Volvo 142E21.44121.01094.112.78018.61142

You will not see any differences in the above two tables if you are viewing the HTML version.

References

Zhu, Hao. 2024.kableExtra: Construct Complex Table with Kable and Pipe Syntax.http://haozhu233.github.io/kableExtra/.