This package can be installed from CRAN as usual, or you may try the development version on GitHub ():
It has extensive documentation at https://haozhu233.github.io/kableExtra/, which provides a lot of examples on how the kable()
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.
The kableExtra package features the pipe operator, %>%
. You can pipe the kable()
output to the styling functions of kableExtra, e.g.,
library(knitr)
kable(iris) %>%
kable_styling(latex_options = "striped")
The functions row_spec()
and 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) %>%
column_spec(5, strikeout = TRUE)
Sepal.Length | Sepal.Width | Petal.Length | Petal.Width | Species |
---|---|---|---|---|
5.1 | 3.5 | 1.4 | 0.2 | setosa |
4.9 | 3.0 | 1.4 | 0.2 | setosa |
4.7 | 3.2 | 1.3 | 0.2 | setosa |
4.6 | 3.1 | 1.5 | 0.2 | setosa |
5.0 | 3.6 | 1.4 | 0.2 | setosa |
Similarly, you can style individual cells with the cell_spec()
function.
Rows and columns can be grouped via the functions pack_rows()
and add_header_above()
, respectively. You can also collapse rows via collapse_rows()
, so one cell can span multiple rows. Below is an example that shows a custom table header with grouped columns:
Below is an example of pack_rows()
. The meaning of its index
argument is similar to the argument of add_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.Length | Sepal.Width | Petal.Length | Petal.Width | |
---|---|---|---|---|
setosa | ||||
1 | 5.1 | 3.5 | 1.4 | 0.2 |
2 | 4.9 | 3.0 | 1.4 | 0.2 |
versicolor | ||||
51 | 7.0 | 3.2 | 4.7 | 1.4 |
52 | 6.4 | 3.2 | 4.5 | 1.5 |
53 | 6.9 | 3.1 | 4.9 | 1.5 |
54 | 5.5 | 2.3 | 4.0 | 1.3 |
virginica | ||||
101 | 6.3 | 3.3 | 6.0 | 2.5 |
102 | 5.8 | 2.7 | 5.1 | 1.9 |
103 | 7.1 | 3.0 | 5.9 | 2.1 |
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 the landscape()
function in kableExtra 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):
mpg | cyl | disp | hp | drat | wt | qsec | vs | am | gear | carb | |
---|---|---|---|---|---|---|---|---|---|---|---|
Lotus Europa | 30.4 | 4 | 95.1 | 113 | 3.77 | 1.513 | 16.9 | 1 | 1 | 5 | 2 |
Ford Pantera L | 15.8 | 8 | 351.0 | 264 | 4.22 | 3.170 | 14.5 | 0 | 1 | 5 | 4 |
Ferrari Dino | 19.7 | 6 | 145.0 | 175 | 3.62 | 2.770 | 15.5 | 0 | 1 | 5 | 6 |
Maserati Bora | 15.0 | 8 | 301.0 | 335 | 3.54 | 3.570 | 14.6 | 0 | 1 | 5 | 8 |
Volvo 142E | 21.4 | 4 | 121.0 | 109 | 4.11 | 2.780 | 18.6 | 1 | 1 | 4 | 2 |
You will not see any differences in the above two tables if you are viewing the HTML version.