A very simple table generator, and it is simple by design. It is not intended to replace any other R packages for making tables. The kable() function returns a single table for a single data object, and returns a table that contains multiple tables if the input object is a list of data objects. The kables() function is similar to kable(x) when x is a list of data objects, but kables() accepts a list of kable() values directly instead of data objects (see examples below).

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,
  ...
)

kables(x, format, caption = NULL, label = NULL)

Arguments

x

For kable(), x is an R object, which is typically a matrix or data frame. For kables(), a list with each element being a returned value from kable().

format

A character string. Possible values are latex, html, pipe (Pandoc's pipe tables), simple (Pandoc's simple tables), rst, jira, and org (Emacs Org-mode). The value of this argument will be automatically determined if the function is called within a knitr document. The format value can also be set in the global option knitr.table.format. If format is a function, it must return a character string.

digits

Maximum number of digits for numeric columns, passed to round(). This can also be a vector of length ncol(x), to set the number of digits for individual columns.

row.names

Logical: whether to include row names. By default, row names are included if rownames(x) is neither NULL nor identical to 1:nrow(x).

col.names

A character vector of column names to be used in the table.

align

Column alignment: a character vector consisting of 'l' (left), 'c' (center) and/or 'r' (right). By default or if align = NULL, numeric columns are right-aligned, and other columns are left-aligned. If length(align) == 1L, the string will be expanded to a vector of individual letters, e.g. 'clc' becomes c('c', 'l', 'c'), unless the output format is LaTeX.

caption

The table caption. By default, it is retrieved from the chunk option tab.cap.

label

The table reference label. By default, the label is obtained from knitr::opts_current$get('label') (i.e., the current chunk label). To disable the label, use label = NA.

format.args

A list of arguments to be passed to format() to format table values, e.g. list(big.mark = ',').

escape

Boolean; whether to escape special characters when producing HTML or LaTeX tables. When escape = FALSE, you have to make sure that special characters will not trigger syntax errors in LaTeX or HTML.

...

Other arguments (see Examples and References).

Value

A character vector of the table source code.

Details

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.

You can set the option knitr.kable.max_rows to limit the number of rows to show in the table, e.g., options(knitr.kable.max_rows = 30).

Note

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(...)).

References

See https://bookdown.org/yihui/rmarkdown-cookbook/kable.html for some examples about this function, including specific arguments according to the format selected.

See also

Other R packages such as huxtable, xtable, kableExtra, gt and tables for HTML and LaTeX tables, and ascii and pander for different flavors of markdown output and some advanced features and table styles. For more on other packages for creating tables, see https://bookdown.org/yihui/rmarkdown-cookbook/table-other.html.

Examples

d1 = head(iris)
d2 = head(mtcars)
# pipe tables by default
kable(d1)
#> Warning: 'xfun::attr()' is deprecated.
#> Use 'xfun::attr2()' instead.
#> See help("Deprecated")
#> Warning: 'xfun::attr()' is deprecated.
#> Use 'xfun::attr2()' instead.
#> See help("Deprecated")
#> 
#> 
#> | 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  |
kable(d2[, 1:5])
#> Warning: 'xfun::attr()' is deprecated.
#> Use 'xfun::attr2()' instead.
#> See help("Deprecated")
#> Warning: 'xfun::attr()' is deprecated.
#> Use 'xfun::attr2()' instead.
#> See help("Deprecated")
#> 
#> 
#> |                  |  mpg| cyl| disp|  hp| drat|
#> |:-----------------|----:|---:|----:|---:|----:|
#> |Mazda RX4         | 21.0|   6|  160| 110| 3.90|
#> |Mazda RX4 Wag     | 21.0|   6|  160| 110| 3.90|
#> |Datsun 710        | 22.8|   4|  108|  93| 3.85|
#> |Hornet 4 Drive    | 21.4|   6|  258| 110| 3.08|
#> |Hornet Sportabout | 18.7|   8|  360| 175| 3.15|
#> |Valiant           | 18.1|   6|  225| 105| 2.76|
# no inner padding
kable(d2, format = "pipe", padding = 0)
#> Warning: 'xfun::attr()' is deprecated.
#> Use 'xfun::attr2()' instead.
#> See help("Deprecated")
#> Warning: 'xfun::attr()' is deprecated.
#> Use 'xfun::attr2()' instead.
#> See help("Deprecated")
#> 
#> 
#> |                 | mpg|cyl|disp| hp|drat|   wt| qsec| vs| am|gear|carb|
#> |:----------------|---:|--:|---:|--:|---:|----:|----:|--:|--:|---:|---:|
#> |Mazda RX4        |21.0|  6| 160|110|3.90|2.620|16.46|  0|  1|   4|   4|
#> |Mazda RX4 Wag    |21.0|  6| 160|110|3.90|2.875|17.02|  0|  1|   4|   4|
#> |Datsun 710       |22.8|  4| 108| 93|3.85|2.320|18.61|  1|  1|   4|   1|
#> |Hornet 4 Drive   |21.4|  6| 258|110|3.08|3.215|19.44|  1|  0|   3|   1|
#> |Hornet Sportabout|18.7|  8| 360|175|3.15|3.440|17.02|  0|  0|   3|   2|
#> |Valiant          |18.1|  6| 225|105|2.76|3.460|20.22|  1|  0|   3|   1|
# more padding
kable(d2, format = "pipe", padding = 2)
#> Warning: 'xfun::attr()' is deprecated.
#> Use 'xfun::attr2()' instead.
#> See help("Deprecated")
#> Warning: 'xfun::attr()' is deprecated.
#> Use 'xfun::attr2()' instead.
#> See help("Deprecated")
#> 
#> 
#> |                   |   mpg|  cyl|  disp|   hp|  drat|     wt|   qsec|  vs|  am|  gear|  carb|
#> |:------------------|-----:|----:|-----:|----:|-----:|------:|------:|---:|---:|-----:|-----:|
#> |Mazda RX4          |  21.0|    6|   160|  110|  3.90|  2.620|  16.46|   0|   1|     4|     4|
#> |Mazda RX4 Wag      |  21.0|    6|   160|  110|  3.90|  2.875|  17.02|   0|   1|     4|     4|
#> |Datsun 710         |  22.8|    4|   108|   93|  3.85|  2.320|  18.61|   1|   1|     4|     1|
#> |Hornet 4 Drive     |  21.4|    6|   258|  110|  3.08|  3.215|  19.44|   1|   0|     3|     1|
#> |Hornet Sportabout  |  18.7|    8|   360|  175|  3.15|  3.440|  17.02|   0|   0|     3|     2|
#> |Valiant            |  18.1|    6|   225|  105|  2.76|  3.460|  20.22|   1|   0|     3|     1|
kable(d1, format = "latex")
#> Warning: 'xfun::attr()' is deprecated.
#> Use 'xfun::attr2()' instead.
#> See help("Deprecated")
#> Warning: 'xfun::attr()' is deprecated.
#> Use 'xfun::attr2()' instead.
#> See help("Deprecated")
#> 
#> \begin{tabular}{r|r|r|r|l}
#> \hline
#> Sepal.Length & Sepal.Width & Petal.Length & Petal.Width & Species\\
#> \hline
#> 5.1 & 3.5 & 1.4 & 0.2 & setosa\\
#> \hline
#> 4.9 & 3.0 & 1.4 & 0.2 & setosa\\
#> \hline
#> 4.7 & 3.2 & 1.3 & 0.2 & setosa\\
#> \hline
#> 4.6 & 3.1 & 1.5 & 0.2 & setosa\\
#> \hline
#> 5.0 & 3.6 & 1.4 & 0.2 & setosa\\
#> \hline
#> 5.4 & 3.9 & 1.7 & 0.4 & setosa\\
#> \hline
#> \end{tabular}
kable(d1, format = "html")
#> Warning: 'xfun::attr()' is deprecated.
#> Use 'xfun::attr2()' instead.
#> See help("Deprecated")
#> Warning: 'xfun::attr()' is deprecated.
#> Use 'xfun::attr2()' instead.
#> See help("Deprecated")
#> <table>
#>  <thead>
#>   <tr>
#>    <th style="text-align:right;"> Sepal.Length </th>
#>    <th style="text-align:right;"> Sepal.Width </th>
#>    <th style="text-align:right;"> Petal.Length </th>
#>    <th style="text-align:right;"> Petal.Width </th>
#>    <th style="text-align:left;"> Species </th>
#>   </tr>
#>  </thead>
#> <tbody>
#>   <tr>
#>    <td style="text-align:right;"> 5.1 </td>
#>    <td style="text-align:right;"> 3.5 </td>
#>    <td style="text-align:right;"> 1.4 </td>
#>    <td style="text-align:right;"> 0.2 </td>
#>    <td style="text-align:left;"> setosa </td>
#>   </tr>
#>   <tr>
#>    <td style="text-align:right;"> 4.9 </td>
#>    <td style="text-align:right;"> 3.0 </td>
#>    <td style="text-align:right;"> 1.4 </td>
#>    <td style="text-align:right;"> 0.2 </td>
#>    <td style="text-align:left;"> setosa </td>
#>   </tr>
#>   <tr>
#>    <td style="text-align:right;"> 4.7 </td>
#>    <td style="text-align:right;"> 3.2 </td>
#>    <td style="text-align:right;"> 1.3 </td>
#>    <td style="text-align:right;"> 0.2 </td>
#>    <td style="text-align:left;"> setosa </td>
#>   </tr>
#>   <tr>
#>    <td style="text-align:right;"> 4.6 </td>
#>    <td style="text-align:right;"> 3.1 </td>
#>    <td style="text-align:right;"> 1.5 </td>
#>    <td style="text-align:right;"> 0.2 </td>
#>    <td style="text-align:left;"> setosa </td>
#>   </tr>
#>   <tr>
#>    <td style="text-align:right;"> 5.0 </td>
#>    <td style="text-align:right;"> 3.6 </td>
#>    <td style="text-align:right;"> 1.4 </td>
#>    <td style="text-align:right;"> 0.2 </td>
#>    <td style="text-align:left;"> setosa </td>
#>   </tr>
#>   <tr>
#>    <td style="text-align:right;"> 5.4 </td>
#>    <td style="text-align:right;"> 3.9 </td>
#>    <td style="text-align:right;"> 1.7 </td>
#>    <td style="text-align:right;"> 0.4 </td>
#>    <td style="text-align:left;"> setosa </td>
#>   </tr>
#> </tbody>
#> </table>
kable(d1, format = "latex", caption = "Title of the table")
#> Warning: 'xfun::attr()' is deprecated.
#> Use 'xfun::attr2()' instead.
#> See help("Deprecated")
#> Warning: 'xfun::attr()' is deprecated.
#> Use 'xfun::attr2()' instead.
#> See help("Deprecated")
#> \begin{table}
#> 
#> \caption{Title of the table}
#> \centering
#> \begin{tabular}[t]{r|r|r|r|l}
#> \hline
#> Sepal.Length & Sepal.Width & Petal.Length & Petal.Width & Species\\
#> \hline
#> 5.1 & 3.5 & 1.4 & 0.2 & setosa\\
#> \hline
#> 4.9 & 3.0 & 1.4 & 0.2 & setosa\\
#> \hline
#> 4.7 & 3.2 & 1.3 & 0.2 & setosa\\
#> \hline
#> 4.6 & 3.1 & 1.5 & 0.2 & setosa\\
#> \hline
#> 5.0 & 3.6 & 1.4 & 0.2 & setosa\\
#> \hline
#> 5.4 & 3.9 & 1.7 & 0.4 & setosa\\
#> \hline
#> \end{tabular}
#> \end{table}
kable(d1, format = "html", caption = "Title of the table")
#> Warning: 'xfun::attr()' is deprecated.
#> Use 'xfun::attr2()' instead.
#> See help("Deprecated")
#> Warning: 'xfun::attr()' is deprecated.
#> Use 'xfun::attr2()' instead.
#> See help("Deprecated")
#> <table>
#> <caption>Title of the table</caption>
#>  <thead>
#>   <tr>
#>    <th style="text-align:right;"> Sepal.Length </th>
#>    <th style="text-align:right;"> Sepal.Width </th>
#>    <th style="text-align:right;"> Petal.Length </th>
#>    <th style="text-align:right;"> Petal.Width </th>
#>    <th style="text-align:left;"> Species </th>
#>   </tr>
#>  </thead>
#> <tbody>
#>   <tr>
#>    <td style="text-align:right;"> 5.1 </td>
#>    <td style="text-align:right;"> 3.5 </td>
#>    <td style="text-align:right;"> 1.4 </td>
#>    <td style="text-align:right;"> 0.2 </td>
#>    <td style="text-align:left;"> setosa </td>
#>   </tr>
#>   <tr>
#>    <td style="text-align:right;"> 4.9 </td>
#>    <td style="text-align:right;"> 3.0 </td>
#>    <td style="text-align:right;"> 1.4 </td>
#>    <td style="text-align:right;"> 0.2 </td>
#>    <td style="text-align:left;"> setosa </td>
#>   </tr>
#>   <tr>
#>    <td style="text-align:right;"> 4.7 </td>
#>    <td style="text-align:right;"> 3.2 </td>
#>    <td style="text-align:right;"> 1.3 </td>
#>    <td style="text-align:right;"> 0.2 </td>
#>    <td style="text-align:left;"> setosa </td>
#>   </tr>
#>   <tr>
#>    <td style="text-align:right;"> 4.6 </td>
#>    <td style="text-align:right;"> 3.1 </td>
#>    <td style="text-align:right;"> 1.5 </td>
#>    <td style="text-align:right;"> 0.2 </td>
#>    <td style="text-align:left;"> setosa </td>
#>   </tr>
#>   <tr>
#>    <td style="text-align:right;"> 5.0 </td>
#>    <td style="text-align:right;"> 3.6 </td>
#>    <td style="text-align:right;"> 1.4 </td>
#>    <td style="text-align:right;"> 0.2 </td>
#>    <td style="text-align:left;"> setosa </td>
#>   </tr>
#>   <tr>
#>    <td style="text-align:right;"> 5.4 </td>
#>    <td style="text-align:right;"> 3.9 </td>
#>    <td style="text-align:right;"> 1.7 </td>
#>    <td style="text-align:right;"> 0.4 </td>
#>    <td style="text-align:left;"> setosa </td>
#>   </tr>
#> </tbody>
#> </table>
# use the booktabs package
kable(mtcars, format = "latex", booktabs = TRUE)
#> Warning: 'xfun::attr()' is deprecated.
#> Use 'xfun::attr2()' instead.
#> See help("Deprecated")
#> Warning: 'xfun::attr()' is deprecated.
#> Use 'xfun::attr2()' instead.
#> See help("Deprecated")
#> 
#> \begin{tabular}{lrrrrrrrrrrr}
#> \toprule
#>   & mpg & cyl & disp & hp & drat & wt & qsec & vs & am & gear & carb\\
#> \midrule
#> Mazda RX4 & 21.0 & 6 & 160.0 & 110 & 3.90 & 2.620 & 16.46 & 0 & 1 & 4 & 4\\
#> Mazda RX4 Wag & 21.0 & 6 & 160.0 & 110 & 3.90 & 2.875 & 17.02 & 0 & 1 & 4 & 4\\
#> Datsun 710 & 22.8 & 4 & 108.0 & 93 & 3.85 & 2.320 & 18.61 & 1 & 1 & 4 & 1\\
#> Hornet 4 Drive & 21.4 & 6 & 258.0 & 110 & 3.08 & 3.215 & 19.44 & 1 & 0 & 3 & 1\\
#> Hornet Sportabout & 18.7 & 8 & 360.0 & 175 & 3.15 & 3.440 & 17.02 & 0 & 0 & 3 & 2\\
#> \addlinespace
#> Valiant & 18.1 & 6 & 225.0 & 105 & 2.76 & 3.460 & 20.22 & 1 & 0 & 3 & 1\\
#> Duster 360 & 14.3 & 8 & 360.0 & 245 & 3.21 & 3.570 & 15.84 & 0 & 0 & 3 & 4\\
#> Merc 240D & 24.4 & 4 & 146.7 & 62 & 3.69 & 3.190 & 20.00 & 1 & 0 & 4 & 2\\
#> Merc 230 & 22.8 & 4 & 140.8 & 95 & 3.92 & 3.150 & 22.90 & 1 & 0 & 4 & 2\\
#> Merc 280 & 19.2 & 6 & 167.6 & 123 & 3.92 & 3.440 & 18.30 & 1 & 0 & 4 & 4\\
#> \addlinespace
#> Merc 280C & 17.8 & 6 & 167.6 & 123 & 3.92 & 3.440 & 18.90 & 1 & 0 & 4 & 4\\
#> Merc 450SE & 16.4 & 8 & 275.8 & 180 & 3.07 & 4.070 & 17.40 & 0 & 0 & 3 & 3\\
#> Merc 450SL & 17.3 & 8 & 275.8 & 180 & 3.07 & 3.730 & 17.60 & 0 & 0 & 3 & 3\\
#> Merc 450SLC & 15.2 & 8 & 275.8 & 180 & 3.07 & 3.780 & 18.00 & 0 & 0 & 3 & 3\\
#> Cadillac Fleetwood & 10.4 & 8 & 472.0 & 205 & 2.93 & 5.250 & 17.98 & 0 & 0 & 3 & 4\\
#> \addlinespace
#> Lincoln Continental & 10.4 & 8 & 460.0 & 215 & 3.00 & 5.424 & 17.82 & 0 & 0 & 3 & 4\\
#> Chrysler Imperial & 14.7 & 8 & 440.0 & 230 & 3.23 & 5.345 & 17.42 & 0 & 0 & 3 & 4\\
#> Fiat 128 & 32.4 & 4 & 78.7 & 66 & 4.08 & 2.200 & 19.47 & 1 & 1 & 4 & 1\\
#> Honda Civic & 30.4 & 4 & 75.7 & 52 & 4.93 & 1.615 & 18.52 & 1 & 1 & 4 & 2\\
#> Toyota Corolla & 33.9 & 4 & 71.1 & 65 & 4.22 & 1.835 & 19.90 & 1 & 1 & 4 & 1\\
#> \addlinespace
#> Toyota Corona & 21.5 & 4 & 120.1 & 97 & 3.70 & 2.465 & 20.01 & 1 & 0 & 3 & 1\\
#> Dodge Challenger & 15.5 & 8 & 318.0 & 150 & 2.76 & 3.520 & 16.87 & 0 & 0 & 3 & 2\\
#> AMC Javelin & 15.2 & 8 & 304.0 & 150 & 3.15 & 3.435 & 17.30 & 0 & 0 & 3 & 2\\
#> Camaro Z28 & 13.3 & 8 & 350.0 & 245 & 3.73 & 3.840 & 15.41 & 0 & 0 & 3 & 4\\
#> Pontiac Firebird & 19.2 & 8 & 400.0 & 175 & 3.08 & 3.845 & 17.05 & 0 & 0 & 3 & 2\\
#> \addlinespace
#> Fiat X1-9 & 27.3 & 4 & 79.0 & 66 & 4.08 & 1.935 & 18.90 & 1 & 1 & 4 & 1\\
#> Porsche 914-2 & 26.0 & 4 & 120.3 & 91 & 4.43 & 2.140 & 16.70 & 0 & 1 & 5 & 2\\
#> Lotus Europa & 30.4 & 4 & 95.1 & 113 & 3.77 & 1.513 & 16.90 & 1 & 1 & 5 & 2\\
#> Ford Pantera L & 15.8 & 8 & 351.0 & 264 & 4.22 & 3.170 & 14.50 & 0 & 1 & 5 & 4\\
#> Ferrari Dino & 19.7 & 6 & 145.0 & 175 & 3.62 & 2.770 & 15.50 & 0 & 1 & 5 & 6\\
#> \addlinespace
#> Maserati Bora & 15.0 & 8 & 301.0 & 335 & 3.54 & 3.570 & 14.60 & 0 & 1 & 5 & 8\\
#> Volvo 142E & 21.4 & 4 & 121.0 & 109 & 4.11 & 2.780 & 18.60 & 1 & 1 & 4 & 2\\
#> \bottomrule
#> \end{tabular}
# use the longtable package
kable(matrix(1000, ncol = 5), format = "latex", digits = 2, longtable = TRUE)
#> Warning: 'xfun::attr()' is deprecated.
#> Use 'xfun::attr2()' instead.
#> See help("Deprecated")
#> Warning: 'xfun::attr()' is deprecated.
#> Use 'xfun::attr2()' instead.
#> See help("Deprecated")
#> 
#> \begin{longtable}{r|r|r|r|r}
#> \hline
#> 1000 & 1000 & 1000 & 1000 & 1000\\
#> \hline
#> \end{longtable}
# change LaTeX default table environment
kable(d1, format = "latex", caption = "My table", table.envir = "table*")
#> Warning: 'xfun::attr()' is deprecated.
#> Use 'xfun::attr2()' instead.
#> See help("Deprecated")
#> Warning: 'xfun::attr()' is deprecated.
#> Use 'xfun::attr2()' instead.
#> See help("Deprecated")
#> \begin{table*}
#> 
#> \caption{My table}
#> \centering
#> \begin{tabular}[t]{r|r|r|r|l}
#> \hline
#> Sepal.Length & Sepal.Width & Petal.Length & Petal.Width & Species\\
#> \hline
#> 5.1 & 3.5 & 1.4 & 0.2 & setosa\\
#> \hline
#> 4.9 & 3.0 & 1.4 & 0.2 & setosa\\
#> \hline
#> 4.7 & 3.2 & 1.3 & 0.2 & setosa\\
#> \hline
#> 4.6 & 3.1 & 1.5 & 0.2 & setosa\\
#> \hline
#> 5.0 & 3.6 & 1.4 & 0.2 & setosa\\
#> \hline
#> 5.4 & 3.9 & 1.7 & 0.4 & setosa\\
#> \hline
#> \end{tabular}
#> \end{table*}
# add some table attributes
kable(d1, format = "html", table.attr = "id=\"mytable\"")
#> Warning: 'xfun::attr()' is deprecated.
#> Use 'xfun::attr2()' instead.
#> See help("Deprecated")
#> Warning: 'xfun::attr()' is deprecated.
#> Use 'xfun::attr2()' instead.
#> See help("Deprecated")
#> <table id="mytable">
#>  <thead>
#>   <tr>
#>    <th style="text-align:right;"> Sepal.Length </th>
#>    <th style="text-align:right;"> Sepal.Width </th>
#>    <th style="text-align:right;"> Petal.Length </th>
#>    <th style="text-align:right;"> Petal.Width </th>
#>    <th style="text-align:left;"> Species </th>
#>   </tr>
#>  </thead>
#> <tbody>
#>   <tr>
#>    <td style="text-align:right;"> 5.1 </td>
#>    <td style="text-align:right;"> 3.5 </td>
#>    <td style="text-align:right;"> 1.4 </td>
#>    <td style="text-align:right;"> 0.2 </td>
#>    <td style="text-align:left;"> setosa </td>
#>   </tr>
#>   <tr>
#>    <td style="text-align:right;"> 4.9 </td>
#>    <td style="text-align:right;"> 3.0 </td>
#>    <td style="text-align:right;"> 1.4 </td>
#>    <td style="text-align:right;"> 0.2 </td>
#>    <td style="text-align:left;"> setosa </td>
#>   </tr>
#>   <tr>
#>    <td style="text-align:right;"> 4.7 </td>
#>    <td style="text-align:right;"> 3.2 </td>
#>    <td style="text-align:right;"> 1.3 </td>
#>    <td style="text-align:right;"> 0.2 </td>
#>    <td style="text-align:left;"> setosa </td>
#>   </tr>
#>   <tr>
#>    <td style="text-align:right;"> 4.6 </td>
#>    <td style="text-align:right;"> 3.1 </td>
#>    <td style="text-align:right;"> 1.5 </td>
#>    <td style="text-align:right;"> 0.2 </td>
#>    <td style="text-align:left;"> setosa </td>
#>   </tr>
#>   <tr>
#>    <td style="text-align:right;"> 5.0 </td>
#>    <td style="text-align:right;"> 3.6 </td>
#>    <td style="text-align:right;"> 1.4 </td>
#>    <td style="text-align:right;"> 0.2 </td>
#>    <td style="text-align:left;"> setosa </td>
#>   </tr>
#>   <tr>
#>    <td style="text-align:right;"> 5.4 </td>
#>    <td style="text-align:right;"> 3.9 </td>
#>    <td style="text-align:right;"> 1.7 </td>
#>    <td style="text-align:right;"> 0.4 </td>
#>    <td style="text-align:left;"> setosa </td>
#>   </tr>
#> </tbody>
#> </table>
# reST output
kable(d2, format = "rst")
#> Warning: 'xfun::attr()' is deprecated.
#> Use 'xfun::attr2()' instead.
#> See help("Deprecated")
#> Warning: 'xfun::attr()' is deprecated.
#> Use 'xfun::attr2()' instead.
#> See help("Deprecated")
#> 
#> 
#> =================  ====  ===  ====  ===  ====  =====  =====  ===  ===  ====  ====
#> \                   mpg  cyl  disp   hp  drat     wt   qsec   vs   am  gear  carb
#> =================  ====  ===  ====  ===  ====  =====  =====  ===  ===  ====  ====
#> Mazda RX4          21.0    6   160  110  3.90  2.620  16.46    0    1     4     4
#> Mazda RX4 Wag      21.0    6   160  110  3.90  2.875  17.02    0    1     4     4
#> Datsun 710         22.8    4   108   93  3.85  2.320  18.61    1    1     4     1
#> Hornet 4 Drive     21.4    6   258  110  3.08  3.215  19.44    1    0     3     1
#> Hornet Sportabout  18.7    8   360  175  3.15  3.440  17.02    0    0     3     2
#> Valiant            18.1    6   225  105  2.76  3.460  20.22    1    0     3     1
#> =================  ====  ===  ====  ===  ====  =====  =====  ===  ===  ====  ====
# no row names
kable(d2, format = "rst", row.names = FALSE)
#> Warning: 'xfun::attr()' is deprecated.
#> Use 'xfun::attr2()' instead.
#> See help("Deprecated")
#> Warning: 'xfun::attr()' is deprecated.
#> Use 'xfun::attr2()' instead.
#> See help("Deprecated")
#> 
#> 
#> ====  ===  ====  ===  ====  =====  =====  ===  ===  ====  ====
#>  mpg  cyl  disp   hp  drat     wt   qsec   vs   am  gear  carb
#> ====  ===  ====  ===  ====  =====  =====  ===  ===  ====  ====
#> 21.0    6   160  110  3.90  2.620  16.46    0    1     4     4
#> 21.0    6   160  110  3.90  2.875  17.02    0    1     4     4
#> 22.8    4   108   93  3.85  2.320  18.61    1    1     4     1
#> 21.4    6   258  110  3.08  3.215  19.44    1    0     3     1
#> 18.7    8   360  175  3.15  3.440  17.02    0    0     3     2
#> 18.1    6   225  105  2.76  3.460  20.22    1    0     3     1
#> ====  ===  ====  ===  ====  =====  =====  ===  ===  ====  ====
# Pandoc simple tables
kable(d2, format = "simple", caption = "Title of the table")
#> Warning: 'xfun::attr()' is deprecated.
#> Use 'xfun::attr2()' instead.
#> See help("Deprecated")
#> Warning: 'xfun::attr()' is deprecated.
#> Use 'xfun::attr2()' instead.
#> See help("Deprecated")
#> 
#> 
#> Table: Title of the table
#> 
#>                       mpg   cyl   disp    hp   drat      wt    qsec   vs   am   gear   carb
#> ------------------  -----  ----  -----  ----  -----  ------  ------  ---  ---  -----  -----
#> Mazda RX4            21.0     6    160   110   3.90   2.620   16.46    0    1      4      4
#> Mazda RX4 Wag        21.0     6    160   110   3.90   2.875   17.02    0    1      4      4
#> Datsun 710           22.8     4    108    93   3.85   2.320   18.61    1    1      4      1
#> Hornet 4 Drive       21.4     6    258   110   3.08   3.215   19.44    1    0      3      1
#> Hornet Sportabout    18.7     8    360   175   3.15   3.440   17.02    0    0      3      2
#> Valiant              18.1     6    225   105   2.76   3.460   20.22    1    0      3      1
# 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 = "'"))
#> Warning: 'xfun::attr()' is deprecated.
#> Use 'xfun::attr2()' instead.
#> See help("Deprecated")
#> Warning: 'xfun::attr()' is deprecated.
#> Use 'xfun::attr2()' instead.
#> See help("Deprecated")
#> 
#> 
#> |          V1|          V2|          V3|          V4|          V5|          V6|
#> |-----------:|-----------:|-----------:|-----------:|-----------:|-----------:|
#> |   996'127,9|   997'911,2| 1'020'393,7|   988'538,0|   998'373,2| 1'004'291,5|
#> |   992'145,7|   986'005,9| 1'004'494,5| 1'008'461,8|   991'726,9| 1'001'221,0|
#> |   989'432,6| 1'002'585,4| 1'013'918,1| 1'000'817,2| 1'018'765,1|   988'619,9|
#> |   992'044,6|   995'582,0| 1'004'265,7|   986'948,8| 1'007'664,4|   994'419,8|
#> |   982'437,2| 1'005'686,0| 1'001'075,8|   990'550,9| 1'009'799,6| 1'010'525,4|
#> |   993'094,6| 1'021'268,5| 1'000'222,9| 1'004'543,4| 1'013'217,8| 1'006'776,8|
#> |   994'414,6| 1'004'248,6| 1'006'036,1|   991'448,0|   988'802,9| 1'000'385,0|
#> |   994'633,4|   983'157,2|   997'373,5|   997'131,0| 1'005'146,0|   996'436,2|
#> | 1'002'271,3| 1'002'494,0|   994'717,4| 1'008'949,6|   984'909,0| 1'007'828,4|
#> | 1'009'784,5| 1'010'728,4| 1'001'921,5| 1'000'673,0| 1'015'327,4| 1'008'044,1|
# save the value
x = kable(d2, format = "html")
#> Warning: 'xfun::attr()' is deprecated.
#> Use 'xfun::attr2()' instead.
#> See help("Deprecated")
cat(x, sep = "\n")
#> <table>
#>  <thead>
#>   <tr>
#>    <th style="text-align:left;">   </th>
#>    <th style="text-align:right;"> mpg </th>
#>    <th style="text-align:right;"> cyl </th>
#>    <th style="text-align:right;"> disp </th>
#>    <th style="text-align:right;"> hp </th>
#>    <th style="text-align:right;"> drat </th>
#>    <th style="text-align:right;"> wt </th>
#>    <th style="text-align:right;"> qsec </th>
#>    <th style="text-align:right;"> vs </th>
#>    <th style="text-align:right;"> am </th>
#>    <th style="text-align:right;"> gear </th>
#>    <th style="text-align:right;"> carb </th>
#>   </tr>
#>  </thead>
#> <tbody>
#>   <tr>
#>    <td style="text-align:left;"> Mazda RX4 </td>
#>    <td style="text-align:right;"> 21.0 </td>
#>    <td style="text-align:right;"> 6 </td>
#>    <td style="text-align:right;"> 160 </td>
#>    <td style="text-align:right;"> 110 </td>
#>    <td style="text-align:right;"> 3.90 </td>
#>    <td style="text-align:right;"> 2.620 </td>
#>    <td style="text-align:right;"> 16.46 </td>
#>    <td style="text-align:right;"> 0 </td>
#>    <td style="text-align:right;"> 1 </td>
#>    <td style="text-align:right;"> 4 </td>
#>    <td style="text-align:right;"> 4 </td>
#>   </tr>
#>   <tr>
#>    <td style="text-align:left;"> Mazda RX4 Wag </td>
#>    <td style="text-align:right;"> 21.0 </td>
#>    <td style="text-align:right;"> 6 </td>
#>    <td style="text-align:right;"> 160 </td>
#>    <td style="text-align:right;"> 110 </td>
#>    <td style="text-align:right;"> 3.90 </td>
#>    <td style="text-align:right;"> 2.875 </td>
#>    <td style="text-align:right;"> 17.02 </td>
#>    <td style="text-align:right;"> 0 </td>
#>    <td style="text-align:right;"> 1 </td>
#>    <td style="text-align:right;"> 4 </td>
#>    <td style="text-align:right;"> 4 </td>
#>   </tr>
#>   <tr>
#>    <td style="text-align:left;"> Datsun 710 </td>
#>    <td style="text-align:right;"> 22.8 </td>
#>    <td style="text-align:right;"> 4 </td>
#>    <td style="text-align:right;"> 108 </td>
#>    <td style="text-align:right;"> 93 </td>
#>    <td style="text-align:right;"> 3.85 </td>
#>    <td style="text-align:right;"> 2.320 </td>
#>    <td style="text-align:right;"> 18.61 </td>
#>    <td style="text-align:right;"> 1 </td>
#>    <td style="text-align:right;"> 1 </td>
#>    <td style="text-align:right;"> 4 </td>
#>    <td style="text-align:right;"> 1 </td>
#>   </tr>
#>   <tr>
#>    <td style="text-align:left;"> Hornet 4 Drive </td>
#>    <td style="text-align:right;"> 21.4 </td>
#>    <td style="text-align:right;"> 6 </td>
#>    <td style="text-align:right;"> 258 </td>
#>    <td style="text-align:right;"> 110 </td>
#>    <td style="text-align:right;"> 3.08 </td>
#>    <td style="text-align:right;"> 3.215 </td>
#>    <td style="text-align:right;"> 19.44 </td>
#>    <td style="text-align:right;"> 1 </td>
#>    <td style="text-align:right;"> 0 </td>
#>    <td style="text-align:right;"> 3 </td>
#>    <td style="text-align:right;"> 1 </td>
#>   </tr>
#>   <tr>
#>    <td style="text-align:left;"> Hornet Sportabout </td>
#>    <td style="text-align:right;"> 18.7 </td>
#>    <td style="text-align:right;"> 8 </td>
#>    <td style="text-align:right;"> 360 </td>
#>    <td style="text-align:right;"> 175 </td>
#>    <td style="text-align:right;"> 3.15 </td>
#>    <td style="text-align:right;"> 3.440 </td>
#>    <td style="text-align:right;"> 17.02 </td>
#>    <td style="text-align:right;"> 0 </td>
#>    <td style="text-align:right;"> 0 </td>
#>    <td style="text-align:right;"> 3 </td>
#>    <td style="text-align:right;"> 2 </td>
#>   </tr>
#>   <tr>
#>    <td style="text-align:left;"> Valiant </td>
#>    <td style="text-align:right;"> 18.1 </td>
#>    <td style="text-align:right;"> 6 </td>
#>    <td style="text-align:right;"> 225 </td>
#>    <td style="text-align:right;"> 105 </td>
#>    <td style="text-align:right;"> 2.76 </td>
#>    <td style="text-align:right;"> 3.460 </td>
#>    <td style="text-align:right;"> 20.22 </td>
#>    <td style="text-align:right;"> 1 </td>
#>    <td style="text-align:right;"> 0 </td>
#>    <td style="text-align:right;"> 3 </td>
#>    <td style="text-align:right;"> 1 </td>
#>   </tr>
#> </tbody>
#> </table>
# can also set options(knitr.table.format = 'html') so that the output is HTML

# multiple tables via either kable(list(x1, x2)) or kables(list(kable(x1),
# kable(x2)))
kable(list(d1, d2), caption = "A tale of two tables")
#> Warning: 'xfun::attr()' is deprecated.
#> Use 'xfun::attr2()' instead.
#> See help("Deprecated")
#> Warning: 'xfun::attr()' is deprecated.
#> Use 'xfun::attr2()' instead.
#> See help("Deprecated")
#> Warning: 'xfun::attr()' is deprecated.
#> Use 'xfun::attr2()' instead.
#> See help("Deprecated")
#> 
#> 
#> Table: A tale of two tables
#> 
#> | 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  |
#> 
#> |                  |  mpg| cyl| disp|  hp| drat|    wt|  qsec| vs| am| gear| carb|
#> |:-----------------|----:|---:|----:|---:|----:|-----:|-----:|--:|--:|----:|----:|
#> |Mazda RX4         | 21.0|   6|  160| 110| 3.90| 2.620| 16.46|  0|  1|    4|    4|
#> |Mazda RX4 Wag     | 21.0|   6|  160| 110| 3.90| 2.875| 17.02|  0|  1|    4|    4|
#> |Datsun 710        | 22.8|   4|  108|  93| 3.85| 2.320| 18.61|  1|  1|    4|    1|
#> |Hornet 4 Drive    | 21.4|   6|  258| 110| 3.08| 3.215| 19.44|  1|  0|    3|    1|
#> |Hornet Sportabout | 18.7|   8|  360| 175| 3.15| 3.440| 17.02|  0|  0|    3|    2|
#> |Valiant           | 18.1|   6|  225| 105| 2.76| 3.460| 20.22|  1|  0|    3|    1|
kables(list(kable(d1, align = "l"), kable(d2)), caption = "A tale of two tables")
#> Warning: 'xfun::attr()' is deprecated.
#> Use 'xfun::attr2()' instead.
#> See help("Deprecated")
#> Warning: 'xfun::attr()' is deprecated.
#> Use 'xfun::attr2()' instead.
#> See help("Deprecated")
#> Warning: 'xfun::attr()' is deprecated.
#> Use 'xfun::attr2()' instead.
#> See help("Deprecated")
#> 
#> 
#> Table: A tale of two tables
#> 
#> |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  |
#> 
#> |                  |  mpg| cyl| disp|  hp| drat|    wt|  qsec| vs| am| gear| carb|
#> |:-----------------|----:|---:|----:|---:|----:|-----:|-----:|--:|--:|----:|----:|
#> |Mazda RX4         | 21.0|   6|  160| 110| 3.90| 2.620| 16.46|  0|  1|    4|    4|
#> |Mazda RX4 Wag     | 21.0|   6|  160| 110| 3.90| 2.875| 17.02|  0|  1|    4|    4|
#> |Datsun 710        | 22.8|   4|  108|  93| 3.85| 2.320| 18.61|  1|  1|    4|    1|
#> |Hornet 4 Drive    | 21.4|   6|  258| 110| 3.08| 3.215| 19.44|  1|  0|    3|    1|
#> |Hornet Sportabout | 18.7|   8|  360| 175| 3.15| 3.440| 17.02|  0|  0|    3|    2|
#> |Valiant           | 18.1|   6|  225| 105| 2.76| 3.460| 20.22|  1|  0|    3|    1|