commit | 69c8bd57db5fbfb11dc677bb20ba3a7a0ac449b6 | [log] [tgz] |
---|---|---|
author | jokorn <jonatankornholt@outlook.com> | Sat Jun 29 11:11:11 2019 +0200 |
committer | GitHub <noreply@github.com> | Sat Jun 29 11:11:11 2019 +0200 |
tree | c6202506ba6b4c4ea51a6cf127185ce3752cb0d5 | |
parent | 7e49c74670171de790ba81b3333ffd26912676f7 [diff] |
Update group_header_rows attribute with new position when adding multiple group_rows to HTML table There is an issue when adding multiple group_rows to a kable HTML table. When adding the second group_rows statement the positions are wrong because the positions of the first group_header_rows are not updated after adding the new group_header_rows. This line should fix this. Example .Rmd file: --- output: html_document: default --- ```{r include=FALSE} # Load libraries library(tidyverse) library(kableExtra) # Create test data # Nested groups. "a" only contains "1" and "2", # "b" only contains "3" and "4", "c" only "5" and "d" only "6" test_data <- tribble( ~var1, ~var2, ~var3, "a", "1", "x", "b", "4", "y", "b", "3", "x", "a", "2", "z", "c", "5", "y", "d", "6", "x", "d", "6", "z", "a", "2", "y", "d", "7", "b", "d", "7", "b") # Sort data on var1 and var2 so we're able to group rows based on these test_data_sorted <- test_data %>% arrange(var1, var2) ``` ```{r kable_one_group} # Create kable table and group rows based on var1 first kable_one_group <- test_data_sorted %>% kable() %>% kable_styling(full_width = FALSE) %>% group_rows(index = test_data_sorted$var1 %>% auto_index(), group_label = test_data_sorted$var1) # This works fine kable_one_group ``` ```{r kable_one_group_other_var} # Create kable table and group rows based on var2 second kable_one_group_other_var <- test_data_sorted %>% kable() %>% kable_styling(full_width = FALSE) %>% group_rows(index = test_data_sorted$var2 %>% auto_index(), group_label = test_data_sorted$var2) #This works fine kable_one_group_other_var ``` ```{r kable_two_groups_both_vars} # Then add another group of rows based on var2 to this table kable_two_groups <- kable_one_group %>% group_rows(index = test_data_sorted$var2 %>% auto_index(), group_label = test_data_sorted$var2 %>% auto_index, indent = FALSE) #This doesn't work as intended kable_two_groups ```
When we are talking about table generators in R, knitr's kable()
function is usually a popular choice because of 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
kable()
(Or a good alternative for markdown tables is pander::pander()
) for all simple tableskable()
with kableExtra
to generate 90 % of complex/advanced/self-customized/beautiful tables in either HTML or LaTeXkableExtra
cannot solve the problemThis package can load required LaTeX package automatically in vanilla rmarkdown. For customized rmarkdown templates, it is recommended to load related LaTeX packages manually.
kableExtra
is NOT a table generating package. It is a package that can "add features" to a kable()
output using a syntax that every useR loves - the pipes %>%
. 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.
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(...) %>% kable_styling(position = "left")
will work in both HTML and PDF.
install.packages("kableExtra") # For dev version devtools::install_github("haozhu233/kableExtra")
library(knitr) library(kableExtra) dt <- mtcars[1:5, 1:4] # HTML table kable(dt, format = "html", caption = "Demo Table") %>% kable_styling(bootstrap_options = "striped", full_width = F) %>% add_header_above(c(" ", "Group 1" = 2, "Group 2[note]" = 2)) %>% add_footnote(c("table footnote")) # LaTeX Table kable(dt, format = "latex", booktabs = T, caption = "Demo Table") %>% kable_styling(latex_options = c("striped", "hold_position"), full_width = F) %>% add_header_above(c(" ", "Group 1" = 2, "Group 2[note]" = 2)) %>% add_footnote(c("table footnote"))
For more information, please check the package vignette.
knitr::kable()
and kableExtra (中文)knitr::kable()
and kableExtraI would like to thank colleagues at Hebrew SeniorLife Institute for Aging Research and the Boston Pepper Center for their input. I also would like to appreciate the mentorship from Tom Travison (@tgt75) and all the efforts from the open source community, which help this package keep getting better.