Search code examples
cssrshinybootstrap-4dt

Reducing bootstrap table cell padding when using `bs_theme()`


# Header
## Load packages
library(shiny)
library(tidyverse)
library(DT)
library(bslib)

ui <- navbarPage("Test",
                 inverse = T,
                 collapsible = T,
                 theme = bs_theme(
                   version = 4,
                   bootswatch = "lux",
                   "font-size-base" = "1rem",
                   "table-cell-padding" = ".4rem"
                 ),
                 tabPanel(
                   title = "Data Tables", 
                   id = "data",
                   icon = icon("flask"),
                   fluidRow(
                     dataTableOutput("DT1")
                   )
                   
                 )
                 
)

server <- function(input, output, session) {
  output$DT1 <- renderDataTable({
    datatable(mtcars, 
              style = "bootstrap4",
              options = list(info = F,
                             searching = T,
                             paging = T,
                             autoWidth = T,
                             scrollX = T),
              filter = list(position = 'top', clear = FALSE),
              class = 'cell-border stripe compact',
              rownames = F)
  })
  
}

##
shinyApp(ui, server)

enter image description here

I am trying to create a table using the bs_theme() function from the bslib package, specifically using the theme lux. The tables are very large and the cells have massive padding. Adding the "table-cell-padding" argument in bs_theme() doesn't change the cell sizes. How do I make the table compact and the cells tight around the numbers/column names?


Solution

  • Just add tags$style('#DT1 td {padding: 0}'), how simple is that.

    # Header
    ## Load packages
    library(shiny)
    library(tidyverse)
    library(DT)
    library(bslib)
    
    ui <- navbarPage("Test",
                     inverse = T,
                     collapsible = T,
                     theme = bs_theme(
                         version = 4,
                         bootswatch = "lux",
                         "font-size-base" = "1rem",
                         "table-cell-padding" = ".4rem"
                     ),
                     tabPanel(
                         title = "Data Tables", 
                         id = "data",
                         icon = icon("flask"),
                         fluidRow(
                             tags$style('#DT1 td {padding: 0}'),
                             # style = "width: 50%",
                             dataTableOutput("DT1")
                         )
                         
                     )
                     
    )
    
    server <- function(input, output, session) {
        output$DT1 <- renderDataTable({
            datatable(mtcars, 
                      style = "bootstrap4",
                      options = list(info = F,
                                     searching = T,
                                     paging = T,
                                     autoWidth = T,
                                     scrollX = T),
                      filter = list(position = 'top', clear = FALSE),
                      class = 'cell-border stripe compact',
                      rownames = F)
        })
        
    }
    
    ##
    shinyApp(ui, server)
    
    

    Change the padding: 0 to any valid css unit you want, here I changed to 0, no padding. It will seem that it doesn't work on left and right, that's because you have autoWidth, it will always use the full width of the parent node. So, if you want the width to be smaller, uncomment the style line above, you should see is't only half of the screen.

    enter image description here