please I have a R shiny application and one of the pages have a really big table. For this reason, I would need to have the horizontal scrollbar both at the top as well as the bottom of the table. Please, bear in mind I'm very little familiar with HTML, CSS and JS. Also, I already managed to move the horizontal scrollbar to the top of the table using solution: R DT Horizontal scroll bar at top of the table
I'm literally using the example explained there and it works perfectly. I would just need some help in adding the scrollbar at the bottom as well.
css <- HTML(
"#flipped > .dataTables_wrapper.no-footer > .dataTables_scroll > .dataTables_scrollBody {
transform:rotateX(180deg);
}
#flipped > .dataTables_wrapper.no-footer > .dataTables_scroll > .dataTables_scrollBody table{
transform:rotateX(180deg);
}"
)
ui <- fluidPage(
tags$head(tags$style(css)),
fluidRow(column(width = 6,
h4("Flipped Scrollbar"),
br(),
DT::dataTableOutput("flipped")
),
column(width = 6,
h4("Regular Scrollbar"),
br(),
DT::dataTableOutput("regular")
)
)
)
server <- function(input, output, session) {
output$flipped <- DT::renderDataTable({
DT::datatable(mtcars, rownames = FALSE,
options = list(
scrollX = TRUE
)
)
})
output$regular <- DT::renderDataTable({
DT::datatable(mtcars, rownames = FALSE,
options = list(
scrollX = TRUE
)
)
})
}
shinyApp(ui, server)
I managed to find a similar question (horizontal scrollbar on top and bottom of table) however, I can't understand how to apply that css and JS code to a Shiny application. Many thanks
Update (that still doesn't work) as a follow-up to Stéphane Laurent suggested solution. My current code now is:
library(shiny)
library(DT)
wideTable <- as.data.frame(matrix(rnorm(1000), nrow = 10, ncol = 100))
js <- "
$(document).ready(function(){
$('#dtable').on('shiny:value', function(e){
setTimeout(function(){
$('#dtable table').wrap('<div id=\"scrolldiv\"></div>');
$('#scrolldiv').doubleScroll({
contentElement: $('table'),
scrollCss: {
'overflow-x': 'scroll',
'overflow-y': 'hidden'
},
contentCss: {
'overflow-x': 'scroll',
'overflow-y': 'hidden'
},
resetOnWindowResize: true
});
setTimeout(function(){$(window).resize();}, 100);
}, 0);
});
});
"
CSS <- "
.doubleScroll-scroll-wrapper {
clear: both;
}
"
ui <- fluidPage(
tags$head(
tags$script(src = "jquery.doubleScroll.js"),
tags$script(HTML(js)),
tags$style(HTML(CSS))
),
br(),
dataTableOutput("dtable")
)
server <- function(input, output, session){
output$dtable <- DT::renderDataTable({
datatable(wideTable,
rownames = T,
filter = 'top',
caption = paste0("All columns of CSV report")
)
})
}
shinyApp(ui, server)