Reputation: 2722
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.
Upvotes: 5
Views: 1547
Reputation: 701
Inspiration comes from @Stéphane Laurent's answer. Below is a more generic approach for applying the sticky property to any number of headers.
library(shiny)
library(magrittr)
JS <- "
$(document).ready(function() {
var myInterval = setInterval(function() {
// clear interval after the table's DOM is available
if ($('thead').length) {
clearInterval(myInterval);
}
// setting css
$('thead tr th').css('position', 'sticky').css('background', 'white');
var height = 0;
for (var i = 0, length = $('thead tr').length; i < length; i++) {
var header = $('thead tr:nth-child(' + i + ')');
height += header.length ? header.height() : 0;
$('thead tr:nth-child(' + (i + 1) + ') th').css('top', height);
}
}, 500);
});
"
ui <- fluidPage(
tags$head(
tags$script(HTML(JS))
),
tableOutput("table")
)
server <- function(input, output, session) {
output$table <- function() {
knitr::kable(mtcars) %>%
kableExtra::add_header_above(c(" " = 1, "Header 1" = 5, "Header 2" = 6)) %>%
kableExtra::add_header_above(c(" " = 1, "Header" = 11)) %>%
kableExtra::kable_styling()
}
}
shinyApp(ui, server)
If you don't want your main app.R
to have all that Javascript, you can move the code to a different file, see: Include a javascript file in Shiny app.
Upvotes: 4
Reputation: 84529
library(shiny)
library(magrittr)
CSS <- "
thead tr th {
position: sticky;
background-color: white;
}
thead tr:nth-child(1) th {
top: 0;
}
"
JS <- "
$(document).ready(function(){
setTimeout(function(){
var h = $('thead tr:nth-child(1)').height();
$('thead tr:nth-child(2) th').css('top', h);
}, 500);
});
"
ui <- fluidPage(
tags$head(
tags$style(HTML(CSS)),
tags$script(HTML(JS))
),
uiOutput("table")
)
server <- function(input, output, session) {
output$table <- renderUI({
tabl <- knitr::kable(mtcars) %>%
kableExtra::add_header_above(c(" " = 1, "Header 1" = 5, "Header 2" = 6)) %>%
kableExtra::kable_styling()
HTML(tabl)
})
}
shinyApp(ui, server)
Upvotes: 2