Adding a vertical and horizontal scroll bar to the DT table in R shiny
Asked Answered
M

2

17

Please check the data table "Case Analyses Details" on the right. I want to fit the data table within the box, such that it aligns from right and bottom border in the box, such that we add a horizontal and vertical scroll bar to the DT which can be used to span the rows that overshoot the box.

## app.R ##
library(shiny)
library(shinydashboard)
library(DT)
ui <- dashboardPage(
dashboardHeader(title = "My Chart"),
dashboardSidebar(
width = 0
),
dashboardBody(
box(title = "Data Path", status = "primary",height = "595" ,solidHeader = T,
    plotOutput("trace_plot")),
box( title = "Case Analyses Details", status = "primary", height = 
"595",width = "6",solidHeader = T, 
     div(DT::dataTableOutput("trace_table",width = 220)))
))
server <- function(input, output) 
{ 
#Plot for Trace Explorer
output$trace_plot <- renderPlot({
plot(iris$Sepal.Length,iris$Sepal.Width)
})
output$trace_table <- renderDataTable({
mtcars
})
}
shinyApp(ui, server)

MTCARS CAPTURE

Manganite answered 27/11, 2017 at 7:37 Comment(0)
S
29

Something like this do?

rm(list = ls())
## app.R ##
library(shiny)
library(shinydashboard)
library(DT)
ui <- dashboardPage(
  dashboardHeader(title = "My Chart"),
  dashboardSidebar(
    width = 0
  ),
  dashboardBody(
    box(title = "Data Path", status = "primary",height = "595" ,solidHeader = T,
        plotOutput("trace_plot")),
    box( title = "Case Analyses Details", status = "primary", height = 
           "595",width = "6",solidHeader = T, 
         column(width = 12,
                DT::dataTableOutput("trace_table"),style = "height:500px; overflow-y: scroll;overflow-x: scroll;"
         )
    )))
server <- function(input, output) { 
  #Plot for Trace Explorer
  output$trace_plot <- renderPlot({
    plot(iris$Sepal.Length,iris$Sepal.Width)
  })
  output$trace_table <- renderDataTable({

    datatable(cbind(mtcars,mtcars), options = list(paging = FALSE))

  })
}
shinyApp(ui, server)

enter image description here

Savina answered 27/11, 2017 at 8:22 Comment(9)
Hi @Pork Chop...For horizontal scrolling this can be added: div(style = 'overflow-x: scroll',DT::dataTableOutput("trace_table",width = "100%"))Lathrope
@Subhasish1315, Thanks for your suggestion too.Manganite
@PorkChop, I have one issue here, when I slide the table horizontally, I cannot see the last three columns sliding along. Please help.Manganite
Looking into itSavina
@AshminKaul see aboveSavina
@PorkChop, nice but the mtcars data appears twice now. Please helpManganite
Oh yes I know that, I was testing and need to see how it will work with a dataset with many columnsSavina
@PorkChop, please help me with this DT with slider issue here, #47714221Manganite
Hi @Ashmin, did the last tree columns not able to see issue been solved? I am working on the scrolling bar in R shiny and has the same issueDiscordant
M
3

This is an old question, but we can also use the dedicated options scrollX and scrollY to add scrollbars to the datatable:

library(shiny)
library(shinydashboard)
library(DT)

ui <- dashboardPage(
  dashboardHeader(title = "My Chart"),
  dashboardSidebar(
    width = 0
  ),
  dashboardBody(
    box(title = "Data Path", height = 450,
        plotOutput("trace_plot")),
    
    box(title = "Case Analyses Details", height = 450,
        DTOutput("trace_table")
    ))
)

server <- function(input, output) { 
  output$trace_plot <- renderPlot({
    plot(iris$Sepal.Length,iris$Sepal.Width)
  })
  output$trace_table <- renderDataTable({
    datatable(
      cbind(mtcars, mtcars),
      options = list(
        scrollX = TRUE,
        scrollY = "250px"
      )        
    )
  })
}
shinyApp(ui, server)

enter image description here

Microspore answered 3/8, 2022 at 12:8 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.