The kableExtra
package has a great function called add_header_above()
which creates an additional header row in the output table on top of the actual column names. This can be very useful for grouping data. When setting fixed_thead = TRUE
in kable_styling()
the actual column names are frozen when scrolling down but this additional header row is not.
Here is a minimal shiny
app that shows what I mean. Note that if you view the app in the RStudio viewer neither the normal column header nor the additional ones are sticky. Run it in a proper web browser instead.
library(shiny)
library(magrittr)
ui <- fluidPage(
tableOutput("table")
)
server <- function(input, output, session) {
output$table <- function() {
knitr::kable(mtcars) %>%
kableExtra::kable_styling(fixed_thead = TRUE) %>%
kableExtra::add_header_above(c(" " = 1, "Header 1" = 5, "Header 2" = 6))
}
}
shinyApp(ui, server)
How to make the additional header row created with add_header_above()
sticky? I guess I would need to incorporate some CSS or JavaScript in the app to do so.