kable {knitr} | R Documentation |
This is a very simple table generator. It is simple by design. It is not intended to replace any other R packages for making tables.
kable(x, format, digits = getOption("digits"), row.names = NA, col.names = NA, align, caption = NULL, format.args = list(), escape = TRUE, ...)
x |
an R object (typically a matrix or data frame) |
format |
a character string; possible values are |
digits |
the maximum number of digits for numeric columns (passed to
|
row.names |
a logical value indicating whether to include row names; by
default, row names are included if |
col.names |
a character vector of column names to be used in the table |
align |
the alignment of columns: a character vector consisting of
|
caption |
the table caption |
format.args |
a list of arguments to be passed to |
escape |
escape special characters when producing HTML or LaTeX tables |
... |
other arguments (see examples) |
Missing values (NA
) in the table are displayed as NA
by
default. If you want to display them with other characters, you can set the
option knitr.kable.NA
, e.g. options(knitr.kable.NA = '')
to
hide NA
values.
A character vector of the table source code.
The tables for format = 'markdown'
also work for Pandoc when the
pipe_tables
extension is enabled (this is the default behavior for
Pandoc >= 1.10).
When using kable()
as a top-level expression, you do not need
to explicitly print()
it due to R's automatic implicit printing.
When it is wrapped inside other expressions (such as a for
loop), you must explicitly print(kable(...))
.
See https://github.com/yihui/knitr-examples/blob/master/091-knitr-table.Rnw for some examples in LaTeX, but they also apply to other document formats.
Other R packages such as xtable and tables for HTML and LaTeX tables, and ascii and pander for different flavors of markdown output and some advanced features and table styles.
kable(head(iris), format = "latex") kable(head(iris), format = "html") kable(head(iris), format = "latex", caption = "Title of the table") kable(head(iris), format = "html", caption = "Title of the table") # use the booktabs package kable(mtcars, format = "latex", booktabs = TRUE) # use the longtable package kable(matrix(1000, ncol = 5), format = "latex", digits = 2, longtable = TRUE) # add some table attributes kable(head(iris), format = "html", table.attr = "id=\"mytable\"") # reST output kable(head(mtcars), format = "rst") # no row names kable(head(mtcars), format = "rst", row.names = FALSE) # R Markdown/Github Markdown tables kable(head(mtcars[, 1:5]), format = "markdown") # no inner padding kable(head(mtcars), format = "markdown", padding = 0) # more padding kable(head(mtcars), format = "markdown", padding = 2) # Pandoc tables kable(head(mtcars), format = "pandoc", caption = "Title of the table") # format numbers using , as decimal point, and ' as thousands separator x = as.data.frame(matrix(rnorm(60, 1e+06, 10000), 10)) kable(x, format.args = list(decimal.mark = ",", big.mark = "'")) # save the value x = kable(mtcars, format = "html") cat(x, sep = "\n") # can also set options(knitr.table.format = 'html') so that the output is HTML