Kable
Kable
Kable
URL http://haozhu233.github.io/kableExtra/,
https://github.com/haozhu233/kableExtra
BugReports https://github.com/haozhu233/kableExtra/issues
Depends R (>= 3.1.0)
Imports knitr (>= 1.16), magrittr, stringr (>= 1.0), xml2 (>= 1.1.1),
rvest, rmarkdown (>= 1.6.0), readr, scales, viridisLite, stats,
grDevices, htmltools, rstudioapi
Suggests testthat, magick, formattable, dplyr
VignetteBuilder knitr
Encoding UTF-8
RoxygenNote 6.0.1
NeedsCompilation no
Author Hao Zhu [aut, cre] (<https://orcid.org/0000-0002-3386-6076>),
Thomas Travison [ctb],
Timothy Tsai [ctb],
Will Beasley [ctb],
Yihui Xie [ctb],
GuangChuang Yu [ctb],
Stéphane Laurent [ctb],
1
2 R topics documented:
R topics documented:
kableExtra-package . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3
add_footnote . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 4
add_header_above . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 4
add_indent . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 5
auto_index . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 6
cell_spec . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 6
collapse_rows . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 7
column_spec . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 8
footnote . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 10
footnote_marker_number . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 11
group_rows . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 12
html_dependency_bsTable . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 13
html_dependency_kePrint . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 13
kableExtra_latex_packages . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 13
kable_as_image . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 14
kable_styling . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 15
landscape . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 16
linebreak . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 17
magic_mirror . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 17
rmd_format . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 17
row_spec . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 18
save_kable . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 19
scroll_box . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 19
spec_angle . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 20
spec_color . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 20
spec_font_size . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 21
spec_popover . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 22
spec_tooltip . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 22
usepackage_latex . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 23
Index 24
kableExtra-package 3
kableExtra-package kableExtra
Description
When we are talking about table generators in R, knitr’s kable() function wins lots of flavor by its
ultimate simplicity. Unlike those powerful table rendering engines such as xtable, the philosophy
behind knitr::kable() is to make it easy for programmers to use. Just as it claimed in its function
description, "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. - Yihui".
However, the ultimate simplicity of kable() also brought troubles to some of us, especially for
new R users, who may not have a lot of experience on generating tables in R. It is not rare to see
people including experienced users asking questions like how to center/left-align a table on Stack
Overflow. Also, for me personally, I found myself repeatedly parsing CSS into kable() for some
very simple features like striped lines. For LaTeX, it’s even worse since I’m almost Stack Overflow
dependent for LaTeX... That’s why this package kableExtra was created.
I hope with kableExtra, you can
• Use default base kable() (Or a good alternative for markdown tables is pander::pander())
for all simple tables
• Use kable() with kableExtra to generate 90 tables in either HTML or LaTeX
• Only have to mess with raw HTML/LaTeX in the last 10 kableExtra cannot solve the problem
For a full package documentation, please visit the package documentation site for more information
Features
Pipable syntax: kableExtra is NOT a table generating package. It is a package that can "add fea-
tures" to a kable output using a syntax that every useR loves - the pipe. We see similar approaches
to deal with plots in packages like ggvis and plotly. There is no reason why we cannot use it with
tables.
Unified functions for both HTML and PDF: Most functionalities in kableExtra can work in both
HTML and PDF. In fact, as long as you specifies format in kable (which can be set globally through
option knitr.table.format), functions in this package will pick the right way to manipulate the
table be themselves. As a result, if users want to left align the table, kable_styling(kable(...), position = "left")
will work in both HTML and PDF.
Note
If you found a feature on the documentation site that is not available in the version of kableExtra
you are using, try to install the pre-release version from github. You can do so by running devtools::install_github("hao
Also, note that This package can load required LaTeX package automatically in vanilla rmarkdown.
For customized rmarkdown templates, it is recommended to load related LaTeX packages manually.
4 add_header_above
Description
Usage
Arguments
input The direct output of your kable function or your last kableExtra function.
label A vector of footnotes you want to add. You don’t need to add notations in your
notes.
notation You can select the format of your footnote notation from number, alphabet,
symbol and none.
threeparttable Boolean value indicating if a threeparttable scheme should be used.
escape Logical value controlling if the label needs to be escaped. Default is TRUE.
Examples
x <- knitr::kable(head(mtcars), "html")
add_footnote(x, c("footnote 1", "footnote 2"), notation = "symbol")
Description
Tables with multiple rows of header rows are extremely useful to demonstrate grouped data. This
function takes the output of a kable() function and adds an header row on top of it.
Usage
Arguments
kable_input Output of knitr::kable() with format specified
header A (named) character vector with colspan as values. For example, c(" " = 1, "title" = 2)
can be used to create a new header row for a 3-column table with "title" span-
ning across column 2 and 3. For convenience, when colspan equals to 1, users
can drop the = 1 part. As a result, c(" ", "title" = 2) is the same as
c(" " = 1, "title" = 2).
bold A T/F value to control whether the text should be bolded.
italic A T/F value to control whether the text should to be emphasized.
monospace A T/F value to control whether the text of the selected column need to be
monospaced (verbatim)
align A character string for cell alignment. For HTML, possible values could be l, c,
r plus left, center, right, justify, initial and inherit while for LaTeX,
you can only choose from l, c & r.
escape A T/F value showing whether special characters should be escaped.
line A T/F value to control whether a line will appear underneath the header
Examples
x <- knitr::kable(head(mtcars), "html")
# Add a row of header with 3 columns on the top of the table. The column
# span for the 2nd and 3rd one are 5 & 6.
add_header_above(x, c(" ", "Group 1" = 5, "Group 2" = 6))
Description
Add indentations to row headers
Usage
add_indent(kable_input, positions)
Arguments
kable_input Output of knitr::kable() with format specified
positions A vector of numeric row numbers for the rows that need to be indented.
Examples
x <- knitr::kable(head(mtcars), "html")
# Add indentations to the 2nd & 4th row
add_indent(x, c(2, 4))
6 cell_spec
Description
This helper function allows users to build the group_row index more quickly and use group_rows
in a way that is similar with collapse_rows.
Usage
auto_index(x)
Arguments
x The index column. A vector. For example ‘c("a", "a", "b", "b", "b")“
Description
Specify Cell format before it gets into kable
Usage
cell_spec(x, format, bold = FALSE, italic = FALSE, monospace = FALSE,
underline = FALSE, strikeout = FALSE, color = NULL, background = NULL,
align = NULL, font_size = NULL, angle = NULL, tooltip = NULL,
popover = NULL, link = NULL, extra_css = NULL, escape = TRUE,
background_as_tile = TRUE, latex_background_in_cell = TRUE)
Arguments
x Things to be formated. It could be a vector of numbers or strings.
format Either "html" or "latex". It can also be set through option(knitr.table.format),
same as knitr::kable().
bold T/F for font bold.
italic T/F for font italic.
collapse_rows 7
Description
Collapse same values in columns into multirow cells. This feature does similar things with group_rows.
However, unlike group_rows, it analyzes existing columns, finds out rows that can be grouped to-
gether, and make them multirow cells. Note that if you want to use column_spec to specify column
styles, you should use column_spec before collapse_rows.
8 column_spec
Usage
collapse_rows(kable_input, columns = NULL, valign = c("middle", "top",
"bottom"), latex_hline = c("full", "major", "none", "custom"),
row_group_label_position = c("identity", "stack"),
custom_latex_hline = NULL, row_group_label_fonts = NULL,
headers_to_remove = NULL)
Arguments
kable_input Output of knitr::kable() with format specified
columns Numeric column positions where rows need to be collapsed.
valign Select from "top", "middle"(default), "bottom". The reason why "top" is not
default is that the multirow package on CRAN win-builder is not up to date.
latex_hline Option controlling the behavior of adding hlines to table. Choose from full,
major, none, custom.
row_group_label_position
Option controlling positions of row group labels. Choose from identity, stack.
custom_latex_hline
Numeric column positions whose collapsed rows will be separated by hlines.
row_group_label_fonts
A list of arguments that can be supplied to group_rows function to format the
row group label when row_group_label_position is stack
headers_to_remove
Numeric column positions where headers should be removed when they are
stacked.
Examples
dt <- data.frame(a = c(1, 1, 2, 2), b = c("a", "a", "a", "b"))
x <- knitr::kable(dt, "html")
collapse_rows(x)
Description
This function allows users to select a column and then specify its look.
Usage
column_spec(kable_input, column, width = NULL, bold = FALSE,
italic = FALSE, monospace = FALSE, underline = FALSE,
strikeout = FALSE, color = NULL, background = NULL,
border_left = FALSE, border_right = FALSE, width_min = NULL,
width_max = NULL, extra_css = NULL, include_thead = FALSE)
column_spec 9
Arguments
Examples
x <- knitr::kable(head(mtcars), "html")
column_spec(x, 1:2, width = "20em", bold = TRUE, italic = TRUE)
10 footnote
Description
footnote provides a more flexible way to add footnote. You can add mutiple sets of footnote using
differeny notation system. It is also possible to specify footnote section header one by one and print
footnotes as a chunk of texts.
Usage
footnote(kable_input, general = NULL, number = NULL, alphabet = NULL,
symbol = NULL, footnote_order = c("general", "number", "alphabet",
"symbol"), footnote_as_chunk = FALSE, escape = TRUE,
threeparttable = FALSE, general_title = "Note: ", number_title = "",
alphabet_title = "", symbol_title = "", title_format = "italic")
Arguments
kable_input HTML or LaTeX table generated by knitr::kable
general Text for general footnote comments. Footnotes in this section won’t be labeled
with any notations
number A vector of footnote texts. Footnotes here will be numbered. There is no upper
cap for the number of footnotes here
alphabet A vector of footnote texts, Footnotes here will be labeled with abc. The vector
here should not have more than 26 elements.
symbol A vector of footnote texts, Footnotes here will be labeled with special symbols.
The vector here should not have more than 20 elements.
footnote_order The order of how to arrange general, number, alphabet and symbol.
footnote_as_chunk
T/F value. Default is FALSE. It controls whether the footnotes should be printed
in a chunk (without line break).
escape T/F value. It controls whether the contents and titles should be escaped against
HTML or LaTeX. Default is TRUE.
threeparttable T/F value for whether to use LaTeX package threeparttable. Threeparttable will
force the width of caption and footnotes be the width of the original table. It’s
useful when you have long paragraph of footnotes.
general_title Section header for general footnotes. Default is "Note: ".
number_title Section header for number footnotes. Default is "".
alphabet_title Section header for alphabet footnotes. Default is "".
symbol_title Section header for symbol footnotes. Default is "".
title_format Choose from "italic"(default), "bold" and "underline". Multiple options are pos-
sible.
footnote_marker_number 11
Examples
footnote_marker_number
Footnote marker
Description
Put footnote mark in superscription in table. Unless you are using it in the caption of kable, you
will need to put escape = F in kable (similar with cell_spec). Again, similar with cell_spec,
the format option here can read default value from global option knitr.table.format.
Usage
Arguments
Examples
Description
Group a few rows in a table together under a label.
Usage
group_rows(kable_input, group_label = NULL, start_row = NULL,
end_row = NULL, index = NULL,
label_row_css = "border-bottom: 1px solid;", latex_gap_space = "0.3em",
escape = TRUE, latex_align = "l", colnum = NULL, bold = T,
italic = F, hline_before = F, hline_after = F,
extra_latex_after = NULL)
Arguments
kable_input Output of knitr::kable() with format specified
group_label A character string for the name of the group
start_row A numeric value that tells the function in which row the group starts. Note that
the counting excludes header rows and other group labeling rows
end_row A numeric value that tells the function in which row the group ends.
index A named vector providing the index for robust row-grouping tasks. Basically,
you can use it in the same way as add_header_above().
label_row_css A character string for any customized css used for the labeling row. By default,
the labeling row will have a solid black line underneath. Only useful for HTML
documents.
latex_gap_space
A character value telling LaTeX how large the gap between the previous row
and the group labeling row. Only useful for LaTeX documents.
escape A T/F value showing whether special characters should be escaped.
latex_align Adjust justification of group_label in latex only. Value should be "c" for centered
on row, "r" for right justification, or "l" for left justification. Default Value is "l"
If using html, the alignment can be set by using the label_row_css parameter.
colnum A numeric that determines how many columns the text should span. The default
setting will have the text span the entire length.
bold A T/F value to control whether the text should be bolded.
italic A T/F value to control whether the text should to be emphasized.
hline_before A T/F value that addes a horizontal line before the group_row label. Default
value is False.
hline_after A replicate of hline.after in xtable. It addes a hline after the row
extra_latex_after
Extra LaTeX text to be added after the row.
html_dependency_bsTable 13
Examples
x <- knitr::kable(head(mtcars), "html")
# Put Row 2 to Row 5 into a Group and label it as "Group A"
group_rows(x, "Group A", 2, 5)
html_dependency_bsTable
HTML dependency for Twitter bootstrap (table only)
Description
HTML dependency for Twitter bootstrap (table only)
Usage
html_dependency_bsTable()
html_dependency_kePrint
HTML dependency for js script to enable bootstrap tooltip and popup
message
Description
HTML dependency for js script to enable bootstrap tooltip and popup message
Usage
html_dependency_kePrint()
kableExtra_latex_packages
LaTeX Packages
Description
This function shows all LaTeX packages that is supposed to be loaded for this package in a rmark-
down yaml format.
Usage
kableExtra_latex_packages()
14 kable_as_image
Description
This is a LaTeX-only function. This function will render the raw LaTeX code (could be codes
generated by other table packages like xtable) to generate a table, convert it to an image and put it
back to a rmarkdown environment. It is a "better than nothing" solution to print high quality tables
in rmarkdown Word document. By using this, you need to take the responsibility of explaining to
your collaborators why they can’t make edits to the tables in Word.
Also, if a filename is provided, user has the option to "save" the table to an image file like ggplot2::ggsave().
Note that, if you are using this function on a Windows computer, you need to install Ghostscript
before you can use this feature. It is essential for magick to read PDFs on Windows. Website for
Ghostscript: https://ghostscript.com/
The idea of this function was coming from this StackOverflow question. The approach was learned
and adopted from the texpreview package, which allows you to preview the results of TeX code in
the Viewer panel.
Usage
kable_as_image(kable_input, filename = NULL, file_format = "png",
latex_header_includes = NULL, keep_pdf = FALSE, density = 300,
keep_tex = FALSE)
Arguments
kable_input Raw LaTeX code to generate a table. It doesn’t have to came from kable or
kableExtra.
filename Character String. If specified, the image will be saved under the specified (path
&) name. You don’t need to put file format like ".png" here.
file_format Character String to specify image format, such as png, jpeg, gif, tiff, etc.
Default is png.
latex_header_includes
A character vector of extra LaTeX header stuff. Each element is a row. You can
have things like c("\\usepackage{threeparttable}", "\\usepackage{icons}")
keep_pdf A T/F option to control if the mid-way standalone pdf should be kept. Default
is FALSE.
density Resolution to read the PDF file. Default value is 300, which should be sufficient
in most cases.
keep_tex A T/F option to control if the latex file that is initially created should be kept.
Default is FALSE.
kable_styling 15
Description
This function provides a cleaner approach to modify the style of HTML tables other than using
the table.attr option in knitr::kable(). Note that those bootstrap options requires Twitter
bootstrap theme, which is not avaiable in some customized template being loaded.
Usage
Arguments
row_label_position
A character string determining the justification of the row labels in a table. Pos-
sible values inclued l for left, c for center, and r for right. The default value is
l for left justifcation.
... extra options for HTML or LaTeX. See details.
Details
For LaTeX, extra options includes:
• repeat_header_method can either be append(default) or replace
• repeat_header_text is just a text string you want to append on or replace the caption.
• stripe_color allows users to pick a different color for their strip lines.
• latex_table_env character string to define customized table environment such as tabu or
tabularx.You shouldn’t expect all features could be supported in self-defined environments.
Examples
x_html <- knitr::kable(head(mtcars), "html")
kable_styling(x_html, "striped", position = "left", font_size = 7)
Description
This function will put the table on an single landscape page. It’s useful for wide tables that cann’t
be printed on a portrait page.
Usage
landscape(kable_input, margin = NULL)
Arguments
kable_input Output of knitr::kable() with format specified
margin Customizable page margin for special needs. Values can be "1cm", "1in" or
similar.
Examples
landscape(knitr::kable(head(mtcars), "latex"))
linebreak 17
Description
This function generate LaTeX code of makecell so that users can have linebreaks in their table
Usage
linebreak(x, align = c("l", "c", "r"), double_escape = F)
Arguments
x A character vector
align Choose from "l", "c" or "r"
double_escape Whether special character should be double escaped. Default is FALSE.
Description
Mirror mirror tell me, how does this kable look like?
Usage
magic_mirror(kable_input)
Arguments
kable_input The output of kable
Examples
magic_mirror(knitr::kable(head(mtcars), "html"))
Description
If the export format of the Rmarkdown document exist,
Usage
rmd_format()
18 row_spec
Description
This function allows users to select a row and then specify its look. It can also specify the format of
the header row when row = 0.
Usage
row_spec(kable_input, row, bold = FALSE, italic = FALSE,
monospace = FALSE, underline = FALSE, strikeout = FALSE, color = NULL,
background = NULL, align = NULL, font_size = NULL, angle = NULL,
extra_css = NULL, hline_after = FALSE, extra_latex_after = NULL)
Arguments
kable_input Output of knitr::kable() with format specified
row A numeric value or vector indicating which row(s) to be selected. You don’t
need to count in header rows or group labeling rows.
bold A T/F value to control whether the text of the selected row need to be bolded.
italic A T/F value to control whether the text of the selected row need to be empha-
sized.
monospace A T/F value to control whether the text of the selected row need to be monospaced
(verbatim)
underline A T/F value to control whether the text of the selected row need to be underlined
strikeout A T/F value to control whether the text of the selected row need to be stricked
out.
color A character string for row text color. For example, "red" or "#BBBBBB".
background A character string for row background color. Here please pay attention to the
differences in color codes between HTML and LaTeX.
align A character string for cell alignment. For HTML, possible values could be l, c,
r plus left, center, right, justify, initial and inherit while for LaTeX,
you can only choose from l, c & r.
font_size A numeric input for font size. For HTML, you can also use options includ-
ing xx-small, x-small, small, medium, large, x-large, xx-large, smaller,
larger, initial and inherit.
angle 0-360, degree that the text will rotate.
extra_css Extra css text to be passed into the cells of the row. Note that it’s not for the
whole row.
hline_after T/F. A replicate of hline.after in xtable. It addes a hline after ther row
extra_latex_after
Extra LaTeX text to be added after the row. Similar with add.to.row in xtable
save_kable 19
Examples
x <- knitr::kable(head(mtcars), "html")
row_spec(x, 1:2, bold = TRUE, italic = TRUE)
Description
Save kable to files
Usage
save_kable(x, file, bs_theme = "simplex", self_contained = TRUE)
Arguments
x A piece of HTML code for tables, usually generated by kable and kableExtra
file save to files
bs_theme Which Bootstrap theme to use
self_contained Will the files be self-contained?
Description
This function will put a HTML kable object in a fixed-height, fixed-width or both box and make it
scrollable.
Usage
scroll_box(kable_input, height = NULL, width = NULL,
box_css = "border: 1px solid #ddd; padding: 5px; ", extra_css = NULL)
Arguments
kable_input A HTML kable object
height A character string indicating the height of the box, e.g. "50px"
width A character string indicating the width of the box, e.g. "100px"
box_css CSS text for the box
extra_css Extra CSS styles
20 spec_color
Examples
## Not run:
# Specify table size by pixels
kable(cbind(mtcars, mtcars), "html") %>%
kable_styling() %>%
scroll_box(width = "500px", height = "200px")
# Specify by percent
kable(cbind(mtcars, mtcars), "html") %>%
kable_styling() %>%
scroll_box(width = "100%", height = "200px")
## End(Not run)
Description
Generate rotation angle for continuous values
Usage
spec_angle(x, begin, end, scale_from = NULL)
Arguments
x continuous vectors of values
begin Smallest degree to rotate. Default is 0
end Largest degree to rotate. Default is 359.
scale_from input range (vector of length two). If not given, is calculated from the range of
x
Description
Generate viridis Color code for continuous values
Usage
spec_color(x, alpha = 1, begin = 0, end = 1, direction = 1,
option = "D", na_color = "#BBBBBB", scale_from = NULL)
spec_font_size 21
Arguments
Description
Usage
Arguments
Description
Usage
Arguments
Description
Usage
Arguments
Description
Load a LaTeX package using R code. Just like \usepackage{} in LaTeX
Usage
usepackage_latex(name, options = NULL)
Arguments
name The LaTeX package name
options The LaTeX options for the package
Examples
usepackage_latex("xcolor")
Index
cell_spec, 6 usepackage_latex, 23
collapse_rows, 7
column_spec, 8
footnote, 10
footnote_marker_alphabet
(footnote_marker_number), 11
footnote_marker_number, 11
footnote_marker_symbol
(footnote_marker_number), 11
group_rows, 12
hsv, 21
html_dependency_bsTable, 13
html_dependency_kePrint, 13
kable_as_image, 14
kable_styling, 15
kableExtra (kableExtra-package), 3
kableExtra-package, 3
kableExtra_latex_packages, 13
landscape, 16
linebreak, 17
magic_mirror, 17
rmd_format, 17
row_spec, 18
save_kable, 19
scroll_box, 19
24