Reputation: 3608
I want to add a row explaining the columns of my table. But the add_header_above
function is a bit confusing. How do you use it? This is the built-in example:
x = 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, header = c(" ", "Group 1" = 5, "Group 2" = 6))
Upvotes: 2
Views: 4970
Reputation: 3608
You have to create a named vector corresponding to the header row you want, e.g. c("Group 1" = 5)
Each header item consists of the text and its "span"
"span" is a single number specifying how many columns the item spans in the header row.
These spans cumulate and have to sum to the total number of column in the table.
So, if you have a 7 column table, and wanted a header spanning the whole table, you could say:
add_header_above(myKe, header = c("Big title" = 7))`
If you wanted an empty header over an initial column of rownames, followed by a header spanning columns 2:3, another spanning 3:4, and the last 2 cells blank in your 7-column table, you would say:
add_header_above(myKe, header = c(" " = 1, "header1" = 2, "header2" = 2, " " = 2))
note: A header can't be blank, so you need to use a white space " " in "blank" cells.
Important Caveat: This construction of c("my header" = 7)
won't work if your headers are constructed on the fly because tmp= "X"; c(tmp = 7)
is interpreted as c("tmp" = 7)
Instead, you would have to say something like:
header = 7
names(header) = "X"
myKe = add_header_above(myKe, header = header)
Upvotes: 2