Search code examples
rshinydashboardr-box

add a scrollbar to a box in shiny


I have implemented a box on R shiny dashboard page using box() function, it loads the table and represents a column full of population numbers. However the numbers overshoot the table and it looks very bad. I wish to add a scrollbar to the box such that the data remains within the box and I can scroll down easily.

box( title = "Btest", status = "primary",height = 355, solidHeader = T, 
                              tableOutput("table1"))

Solution

  • you can use library(DT).It provides an R interface to the JavaScript library DataTables. R data objects (matrices or data frames) can be displayed as tables on HTML pages, and DataTables provides filtering, pagination, sorting, and many other features in the tables.

    your code will be looks like:

    ui.r:

    box(
    title = "View Data", 
    width = NULL,
    status = "primary", 
    solidHeader = TRUE,
    collapsible = TRUE,
    div(style = 'overflow-x: scroll', DT::dataTableOutput('view_data'))
         )
    

    In server.r

    view_data_fun<-eventreactive/reactive/observe/observeevent({
    
    #your table generation code
    
      })
    
    output$view_data<-DT::renderDataTable({
        DT::datatable(view_data_fun(),rownames = FALSE)%>%formatStyle(columns=colnames(view_selected_data_fun()),background = 'white',color='black')
      })
    

    you can change the option in %>%formatstyle. for more read DT information