10.1 The function knitr::kable()
The kable()
function in knitr is a very simple table generator, and is simple by design. It only generates tables for strictly rectangular data such as matrices and data frames. You cannot heavily format the table cells or merge cells. However, this function does have a large number of arguments for you to customize the appearance of tables:
kable(x, format, digits = getOption("digits"), row.names = NA,
col.names = NA, align, caption = opts_current$get("tab.cap"),
label = NULL, format.args = list(), escape = TRUE, ...)
10.1.1 Supported table formats
In most cases, knitr::kable(x)
may be enough if you only need a simple table for the data object x
. The format
argument is automatically set according to the knitr source document format. Its possible values are pipe
(tables with columns separated by pipes), simple
(Pandoc’s simple tables), latex
(LaTeX tables), html
(HTML tables), and rst
(reStructuredText tables). For R Markdown documents, kable()
uses the pipe
format for tables by default, which looks like this:
::kable(head(mtcars[, 1:4]), "pipe") knitr
| | 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| |Valiant | 18.1| 6| 225| 105|
You can also generate simple tables, or tables in HTML, LaTeX, and reStructuredText:
::kable(head(mtcars[, 1:4]), "simple") knitr
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 Valiant 18.1 6 225 105
::kable(mtcars[1:2, 1:2], "html") knitr
<table>
<thead>
<tr>
<th style="text-align:left;"> </th>
<th style="text-align:right;"> mpg </th>
<th style="text-align:right;"> cyl </th>
</tr>
</thead>
<tbody>
<tr>
<td style="text-align:left;"> Mazda RX4 </td>
<td style="text-align:right;"> 21 </td>
<td style="text-align:right;"> 6 </td>
</tr>
<tr>
<td style="text-align:left;"> Mazda RX4 Wag </td>
<td style="text-align:right;"> 21 </td>
<td style="text-align:right;"> 6 </td>
</tr>
</tbody>
</table>
::kable(head(mtcars[, 1:4]), "latex") knitr
\begin{tabular}{l|r|r|r|r}
\hline
& mpg & cyl & disp & hp\\
\hline
& 21.0 & 6 & 160 & 110\\
Mazda RX4 \hline
& 21.0 & 6 & 160 & 110\\
Mazda RX4 Wag \hline
& 22.8 & 4 & 108 & 93\\
Datsun 710 \hline
& 21.4 & 6 & 258 & 110\\
Hornet 4 Drive \hline
& 18.7 & 8 & 360 & 175\\
Hornet Sportabout \hline
& 18.1 & 6 & 225 & 105\\
Valiant \hline
\end{tabular}
::kable(head(mtcars[, 1:4]), "rst") knitr
================= ==== === ==== ===
\ 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
Valiant 18.1 6 225 105 ================= ==== === ==== ===
Please note that only the formats pipe
and simple
are portable, i.e., they work for any output document format. Other table formats only work for specific output formats, e.g., format = 'latex'
only works for LaTeX output documents. Using a specific table format will give you more control, at the price of sacrificing portability.
If you only need one table format that is not the default format for a document, you can set the global R option knitr.table.format
, e.g.,
options(knitr.table.format = "latex")
This option can also be a function that returns the format string or NULL
. In the case of NULL
, knitr will try to automatically decide the appropriate format. For example, we can use the latex
format only when the output format is LaTeX:
options(knitr.table.format = function() {
if (knitr::is_latex_output())
"latex" else "pipe"
})
10.1.2 Change column names
The names of columns in a data frame may not be the same as what we want to display to readers. In R, the column names of data often do not use spaces to separate words but dots or underscores instead. This may not feel natural when we read them in a table. We can use the col.names
argument to replace the column names with a vector of new names. For example, we substitute the dots with spaces in the column names of the iris
data:
<- head(iris)
iris2 ::kable(iris2, col.names = gsub("[.]", " ", names(iris))) knitr
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 |
5.4 | 3.9 | 1.7 | 0.4 | setosa |
The col.names
argument can take an arbitrary character vector (not necessarily the modified column names via functions like gsub()
), as long as the length of the vector is equal to the number of columns of the data object, e.g.,
::kable(
knitr
iris,col.names = c('We', 'Need', 'Five', 'Names', 'Here')
)
10.1.3 Specify column alignment
To change the alignment of the table columns, you can use either a vector of values consisting of characters l
(left), c
(center), and r
(right) or a single multi-character string for alignment, so kable(..., align = c('c', 'l'))
can be shortened to kable(..., align = 'cl')
. By default, numeric columns are right-aligned, and other columns are left-aligned. Here is an example:
# left, center, center, right, right
::kable(iris2, align = "lccrr") knitr
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 |
5.4 | 3.9 | 1.7 | 0.4 | setosa |
10.1.5 Format numeric columns
You can set the maximum number of decimal places via the digits
argument (which will be passed to the round()
function), and other formatting arguments via format.args
(to be passed to the format()
function in base R). First we show a few simple examples of round()
and format()
so you will understand how the arguments work later in kable()
:
round(1.234567, 0)
## [1] 1
round(1.234567, digits = 1)
## [1] 1.2
round(1.234567, digits = 3)
## [1] 1.235
format(1000, scientific = TRUE)
## [1] "1e+03"
format(10000.123, big.mark = ",")
## [1] "10,000"
Then we round and format numbers in a table:
<- cbind(X1 = runif(3), X2 = 10^c(3, 5, 7), X3 = rnorm(3,
d 0, 1000))
# at most 4 decimal places
::kable(d, digits = 4) knitr
X1 | X2 | X3 |
---|---|---|
0.0698 | 1e+03 | -571.9407 |
0.7653 | 1e+05 | -2161.6849 |
0.0049 | 1e+07 | -1705.9400 |
# round columns separately
::kable(d, digits = c(5, 0, 2)) knitr
X1 | X2 | X3 |
---|---|---|
0.06975 | 1e+03 | -571.94 |
0.76528 | 1e+05 | -2161.68 |
0.00491 | 1e+07 | -1705.94 |
# do not use the scientific notation
::kable(d, digits = 3, format.args = list(scientific = FALSE)) knitr
X1 | X2 | X3 |
---|---|---|
0.070 | 1000 | -571.941 |
0.765 | 100000 | -2161.685 |
0.005 | 10000000 | -1705.940 |
# add commas to big numbers
::kable(d, digits = 3, format.args = list(big.mark = ",",
knitrscientific = FALSE))
X1 | X2 | X3 |
---|---|---|
0.070 | 1,000 | -571.941 |
0.765 | 100,000 | -2,161.685 |
0.005 | 10,000,000 | -1,705.940 |
10.1.6 Display missing values
By default, missing values (i.e., NA
) are displayed as the character string NA
in the table. You can replace them with other values or choose not to display anything (i.e., leave the NA
cells empty) with the global R option knitr.kable.NA
, e.g., we make NA
cells empty in the second table and display **
in the third table below:
rbind(c(1, 1), c(2, 3), c(3, 2))] <- NA
d[::kable(d) # NA is displayed by default knitr
X1 | X2 | X3 |
---|---|---|
NA | 1e+03 | -571.9 |
0.7653 | 1e+05 | NA |
0.0049 | NA | -1705.9 |
# replace NA with empty strings
<- options(knitr.kable.NA = "")
opts ::kable(d) knitr
X1 | X2 | X3 |
---|---|---|
1e+03 | -571.9 | |
0.7653 | 1e+05 | |
0.0049 | -1705.9 |
options(knitr.kable.NA = "**")
::kable(d) knitr
X1 | X2 | X3 |
---|---|---|
** | 1e+03 | -571.9 |
0.7653 | 1e+05 | ** |
0.0049 | ** | -1705.9 |
options(opts) # restore global R options
10.1.7 Escape special characters
If you are familiar with HTML or LaTeX, you know that there are a few special characters in these languages. To generate safe output, kable()
will escape these special characters by default via the argument escape = TRUE
, which means all characters will be generated verbatim, and special characters lose their special meanings. For example, >
will be substituted with >
for HTML tables, and _
will be escaped as \_
for LaTeX tables. If you are an expert and know how to use special characters properly, you may disable this argument via escape = FALSE
. In the second table below, we include a few LaTeX math expressions that contain special characters $
, \
, and _
:
<- lm(dist ~ speed, data = cars)
m <- coef(summary(m))
d ::kable(d) knitr
Estimate | Std. Error | t value | Pr(>|t|) | |
---|---|---|---|---|
(Intercept) | -17.579 | 6.7584 | -2.601 | 0.0123 |
speed | 3.932 | 0.4155 | 9.464 | 0.0000 |
# add a few math expressions to row and column names
rownames(d) <- c("$\\beta_0$", "$\\beta_1$")
colnames(d)[4] <- "$P(T > |t|)$"
::kable(d, escape = FALSE) knitr
Estimate | Std. Error | t value | \(P(T > |t|)\) | |
---|---|---|---|---|
\(\beta_0\) | -17.579 | 6.7584 | -2.601 | 0.0123 |
\(\beta_1\) | 3.932 | 0.4155 | 9.464 | 0.0000 |
Without escape = FALSE
, special characters will either be escaped or substituted. For example, $
is escaped as \$
, _
is escaped as \_
, and \
is substituted with \textbackslash{}
:
::kable(d, format = "latex", escape = TRUE) knitr
\begin{tabular}{l|r|r|r|r}
\hline
& Estimate & Std. Error & t value & \$P(T > |t|)\$\\
\hline
\$\textbackslash{}beta\_0\$ & -17.579 & 6.7584 & -2.601 & 0.0123\\
\hline
\$\textbackslash{}beta\_1\$ & 3.932 & 0.4155 & 9.464 & 0.0000\\
\hline
\end{tabular}
Other common special LaTeX characters include #
, %
, &
, {
, and }
. Common special HTML characters include &
, <
, >
, and "
. You need to be cautious when generating tables with escape = FALSE
, and make sure you are using the special characters in the right way. It is a very common mistake to use escape = FALSE
and include %
or _
in column names or the caption of a LaTeX table without realizing that they are special.
If you are not sure how to properly escape special characters, there are two internal helper functions in knitr. Below are some examples:
:::escape_latex(c("100%", "# a comment", "column_name")) knitr
## [1] "100\\%" "\\# a comment" "column\\_name"
:::escape_html(c("<address>", "x = \"character\"",
knitr"a & b"))
## [1] "<address>"
## [2] "x = "character""
## [3] "a & b"
10.1.8 Multiple tables side by side
You can pass a list of data frames or matrices to kable()
to generate multiple tables side by side. For example, Table 10.2 contains two tables generated from the code below:
<- head(cars, 3)
d1 <- head(mtcars[, 1:3], 5)
d2 ::kable(
knitrlist(d1, d2),
caption = 'Two tables placed side by side.',
booktabs = TRUE, valign = 't'
)
|
|
Please note that this feature only works for HTML and PDF output.
If you want to be able to customize each table individually when placing them side by side, you may use the kables()
function (the plural form of kable()
), and pass a list of kable()
objects to it. For example, we change the column names in the left table and set the number of decimal places to zero in the right table in Table 10.3:
# data objects d1 and d2 are from the previous code chunk
::kables(
knitrlist(
# the first kable() to change column names
::kable(
knitrcol.names = c('SPEED', 'DISTANCE'), valign = 't'
d1,
),# the second kable() to set the digits option
::kable(d2, digits = 0, valign = 't')
knitr
),caption = 'Two tables created by knitr::kables().'
)
|
|
10.1.9 Generate multiple tables from a for
-loop (*)
One common confusion about kable()
is that it does not work inside for
-loops. This problem is not specific to kable()
but exists in many other packages, too. The reason is a little complicated. In case you are interested in the technicality, it is explained in the blog post “The Ghost Printer behind Top-level R Expressions.”
You may expect the following code chunk to generate three tables, but it will not:
```{r}
for (i in 1:3) {
knitr::kable(head(iris))
}
```
You have to explicitly print the kable()
results, and apply the chunk option results = 'asis'
, e.g.,
```{r, results='asis'}
for (i in 1:3) {
print(knitr::kable(head(iris)))
}
```
In general, when you generate output from a for
-loop, we recommend that you add a few line breaks (\n
) or an HTML comment (<!-- -->
) after each output element to clearly separate all output elements, e.g.,
```{r, results='asis'}
for (i in 1:3) {
print(knitr::kable(head(iris), caption = 'A caption.'))
cat('\n\n<!-- -->\n\n')
}
```
Without the separators, Pandoc may be fail to detect the individual elements. For example, when a plot is followed immediately by a table, the table will not be recognized:
![](logo.png)
mpg cyl disp hp
------------------ ----- ---- ----- ----
Mazda RX4 21.0 6 160 110 Mazda RX4 Wag 21.0 6 160 110
But it will be if there is a clear separation like this (note that we added an empty line below the image):
![](logo.png)
mpg cyl disp hp
------------------ ----- ---- ----- ----
Mazda RX4 21.0 6 160 110 Mazda RX4 Wag 21.0 6 160 110
or
![](logo.png)
<!-- -->
mpg cyl disp hp
------------------ ----- ---- ----- ----
Mazda RX4 21.0 6 160 110 Mazda RX4 Wag 21.0 6 160 110
10.1.10 Customize LaTeX tables (*)
If the only output format you need is LaTeX, there are a few extra options you can use in kable()
. Note that these options will be ignored in other types of output such as HTML. Unless you have set the table format option globally (see Section 10.1.1), you will have to use the format
argument of kable()
explicitly in the examples of this section, e.g.,
::kable(iris2, format = "latex", booktabs = TRUE) knitr
When you assign a caption to a table (see Section 10.1.4), kable()
will use the table
environment to include the table, i.e.,
\begin{table}
% the table body (usually the tabular environment)
\end{table}
You can change this environment via the table.envir
argument, e.g.,
::kable(cars[1:2, ], format = "latex", table.envir = "figure") knitr
\begin{figure}
\begin{tabular}{r|r}
\hline
& dist\\
speed \hline
& 2\\
4 \hline
& 10\\
4 \hline
\end{tabular}
\end{figure}
The floating position of the table is controlled by the argument position
. For example, we can try to force a table to float to the bottom of a page via position = "!b"
:
::kable(cars[1:2, ], format = "latex", table.envir = "table",
knitrposition = "!b")
\begin{table}[!b]
\begin{tabular}{r|r}
\hline
& dist\\
speed \hline
& 2\\
4 \hline
& 10\\
4 \hline
\end{tabular}
\end{table}
When a table has a caption, you can also assign a short caption to it via the caption.short
argument, e.g.,
::kable(iris2, caption = "A long long long caption!",
knitrcaption.short = "A short one.")
The short caption goes into the square brackets of the \caption[]{}
command in LaTeX, and is often used in the List of Tables of the PDF output document (if the short caption is not provided, the full caption is displayed there).
If you are familiar with the LaTeX package booktabs for publication-quality tables, you can set booktabs = TRUE
, e.g.,
<- head(iris, 10)
iris3 ::kable(iris3, format = "latex", booktabs = TRUE) knitr
\begin{tabular}{rrrrl}
\toprule
& Sepal.Width & Petal.Length & Petal.Width & Species\\
Sepal.Length \midrule
& 3.5 & 1.4 & 0.2 & setosa\\
5.1 & 3.0 & 1.4 & 0.2 & setosa\\
4.9 & 3.2 & 1.3 & 0.2 & setosa\\
4.7 & 3.1 & 1.5 & 0.2 & setosa\\
4.6 & 3.6 & 1.4 & 0.2 & setosa\\
5.0 \addlinespace
& 3.9 & 1.7 & 0.4 & setosa\\
5.4 & 3.4 & 1.4 & 0.3 & setosa\\
4.6 & 3.4 & 1.5 & 0.2 & setosa\\
5.0 & 2.9 & 1.4 & 0.2 & setosa\\
4.4 & 3.1 & 1.5 & 0.1 & setosa\\
4.9 \bottomrule
\end{tabular}
Please note that when you need additional LaTeX packages such as booktabs for an R Markdown document, you have to declare these packages in YAML (see Section 6.4 for how).
Depending on whether the argument booktabs
is TRUE
or FALSE
(default), the table appearance is different. For booktabs = FALSE
:
Table columns are separated by vertical lines. You can explicitly remove the vertical lines via the
vline
argument, e.g.,knitr::kable(iris, vline = "")
(the default isvline = "|"
). You can set this option as a global R option so you do not need to set it for every single table, e.g.,options(knitr.table.vline = "")
.The horizontal lines can be defined via arguments
toprule
,midrule
,linesep
, andbottomrule
. Their default values are all\hline
.
For booktabs = TRUE
:
There are no vertical lines in the table, but you can add these lines via the
vline
argument.The table only has horizontal lines for the table header and the bottom row. The default argument values are
toprule = "\\toprule"
,midrule = "\\midrule"
, andbottomrule = "\\bottomrule"
. A line space is added to every five rows by default. This is controlled by the argumentlinesep
, which defaults toc("", "", "", "", "\\addlinespace")
. If you want to add a space to every three rows, you can do this:::kable(iris3, format = "latex", linesep = c("", "", knitr"\\addlinespace"), booktabs = TRUE)
\begin{tabular}{rrrrl} \toprule & Sepal.Width & Petal.Length & Petal.Width & Species\\ Sepal.Length \midrule & 3.5 & 1.4 & 0.2 & setosa\\ 5.1 & 3.0 & 1.4 & 0.2 & setosa\\ 4.9 & 3.2 & 1.3 & 0.2 & setosa\\ 4.7 \addlinespace & 3.1 & 1.5 & 0.2 & setosa\\ 4.6 & 3.6 & 1.4 & 0.2 & setosa\\ 5.0 & 3.9 & 1.7 & 0.4 & setosa\\ 5.4 \addlinespace & 3.4 & 1.4 & 0.3 & setosa\\ 4.6 & 3.4 & 1.5 & 0.2 & setosa\\ 5.0 & 2.9 & 1.4 & 0.2 & setosa\\ 4.4 \addlinespace & 3.1 & 1.5 & 0.1 & setosa\\ 4.9 \bottomrule \end{tabular}
If you want to remove the line spaces altogether, you may use
linesep = ''
.
Sometimes your table may be longer than a page. In this case, you can use the argument longtable = TRUE
, which uses the LaTeX package longtable to span your table to multiple pages.
Tables are center-aligned by default when they are included in a table environment (i.e., when the table has a caption). If you do not want to center a table, use the argument centering = FALSE
.
10.1.11 Customize HTML tables (*)
If you want to customize tables generated via knitr::kable(format = "html")
, there is only one extra argument besides the common arguments mentioned in previous sections: table.attr
. This argument allows you to add arbitrary attributes to the <table>
tag. For example:
::kable(mtcars[1:2, 1:2], table.attr = "class=\"striped\"",
knitrformat = "html")
<table class="striped">
<thead>
<tr>
<th style="text-align:left;"> </th>
<th style="text-align:right;"> mpg </th>
<th style="text-align:right;"> cyl </th>
</tr>
</thead>
<tbody>
<tr>
<td style="text-align:left;"> Mazda RX4 </td>
<td style="text-align:right;"> 21 </td>
<td style="text-align:right;"> 6 </td>
</tr>
<tr>
<td style="text-align:left;"> Mazda RX4 Wag </td>
<td style="text-align:right;"> 21 </td>
<td style="text-align:right;"> 6 </td>
</tr>
</tbody>
</table>
We added a class striped
to the table. However, a class name is not enough to change the appearance of a table. You have to define CSS rules for the class. For example, to make a striped table that has different colors for odd and even rows, you can add a light gray background to even or odd rows:
.striped tr:nth-child(even) { background: #eee; }
The above CSS rule means all rows (i.e., the <tr>
tags) with even row numbers (:nth-child(even)
) that are children of an element with the striped
class will have a background color #eee
.
A little bit of CSS can make a plain HTML table look decent. Figure 10.1 is a screenshot of an HTML table to which the following CSS rules are applied:
table {margin: auto;
border-top: 1px solid #666;
border-bottom: 1px solid #666;
}border-bottom: 1px solid #ddd; }
table thead th { , td { padding: 5px; }
th, tfoot, tr:nth-child(even) { background: #eee; } thead