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.
add_header_above(
kable_input,
header = NULL,
bold = FALSE,
italic = FALSE,
monospace = FALSE,
underline = FALSE,
strikeout = FALSE,
align = "c",
color = NULL,
background = NULL,
font_size = NULL,
angle = NULL,
escape = TRUE,
line = TRUE,
line_sep = 3,
extra_css = NULL,
include_empty = FALSE,
border_left = FALSE,
border_right = FALSE
)
Output of knitr::kable()
with format
specified
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" spanning 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)
.
Alternatively, a data frame with two columns can be provided: The first
column should contain the header names (character vector) and the second
column should contain the colspan (numeric vector). This input can be used
if there are problems with Unicode characters in the headers.
A T/F value to control whether the text should be bolded.
A T/F value to control whether the text should to be emphasized.
A T/F value to control whether the text of the selected column need to be monospaced (verbatim)
A T/F value to control whether the text of the selected row need to be underlined
A T/F value to control whether the text of the selected row need to be struck out.
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
.
A character string/vector for text color. Here please pay attention to the differences in color codes between HTML and LaTeX.
A character string/vector for background color. Here please pay attention to the differences in color codes between HTML and LaTeX. Also note that in HTML, background defined in cell_spec won't cover the whole cell.
A numeric input/vector for font size. For HTML, you can also use
options including xx-small
, x-small
, small
, medium
, large
,
x-large
, xx-large
, smaller
, larger
, initial
and inherit
.
0-360, degree that the text will rotate.
A T/F value showing whether special characters should be escaped.
A T/F value to control whether a line will appear underneath the header
A numeric value indicating how much the midlines should be separated by space. Default is 3.
An HTML only option. CSS defined here will be send to the td cell.
Whether empty cells in HTML should also be styled. Default is FALSE.
T/F option for border on the left side in latex.
T/F option for border on the right side in latex.
if (FALSE) {
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))
}
Run the code above in your browser using DataLab