R语言 如何在Shiny中将datatable列标题左对齐?

eagi6jfj  于 2023-01-22  发布在  其他
关注(0)|答案(2)|浏览(245)

由于某些原因,我无法使数据表的列标题正确地左对齐:

library(shiny)
library(DT)
library(ggplot2)

ui <- fluidPage(
  title = "Examples of DataTables",
  sidebarLayout(mainPanel = 
                  mainPanel(
                    tabsetPanel(
                      id = 'dataset',
                      tabPanel("diamonds", DT::dataTableOutput("mytable1"))
                    )
                  ),sidebarPanel = sidebarPanel()
  )
)

server <- function(input, output) {

  # choose columns to display
  diamonds2 = ggplot2::diamonds[sample(nrow(ggplot2::diamonds), 1000), ]
  output$mytable1 <- DT::renderDataTable({
    DT::datatable(diamonds2,
                  extensions = c('Buttons', 
                                 #'Autofill', 
                                 'ColReorder',
                                 'Responsive',
                                 'Scroller'),
                  style = 'bootstrap',
                  class = 'table-bordered stripe table-condensed',
                  filter = list(position = 'top', clear = FALSE),
                  rownames = FALSE,
                  options = list( dom = 'Bfrtip',
                                  buttons = list(list(extend = 'csv',
                                                      buttons = c('csv'),
                                                      exportOptions = list(modifiers = list(page = "current"))
                                                      )
                                                 ),
                                  search = list(regex = TRUE, caseInsensitive = FALSE),
                                  #autofill = TRUE,
                                  colReorder = TRUE,
                                  deferRender = TRUE,
                                  scrollY = 200,
                                  scroller = TRUE,
                                  columnDefs = list(list(className = 'dt-left', targets = '_all'))
                                  )
                  )
  })

}

shinyApp(ui, server)

自己看

d5vmydt9

d5vmydt91#

这个应该够了。
使用#mytable1 .table th可以访问表头。
使用#mytable1 .table td,我们可以访问表格单元格。
使用text-align: left;,我们将文本左对齐。

library(shiny)
library(shinyjs)
library(DT)
library(ggplot2)

ui <- fluidPage(
  title = "Examples of DataTables",

  ## CSS-Code ###############
  inlineCSS("
            #mytable1 .table th {
             text-align: left;
            }

            #mytable1 .table td {
             text-align: left;
            }
            "
  ),
   #####################
  sidebarLayout(mainPanel = 
                  mainPanel(
                    tabsetPanel(
                      id = 'dataset',
                      tabPanel("diamonds", DT::dataTableOutput("mytable1"))
                    )
                  ),sidebarPanel = sidebarPanel()
  )
)

server <- function(input, output) {

  # choose columns to display
  diamonds2 = ggplot2::diamonds[sample(nrow(ggplot2::diamonds), 1000), ]
  output$mytable1 <- DT::renderDataTable({
    DT::datatable(diamonds2,
                  extensions = c('Buttons', 
                                 #'Autofill', 
                                 'ColReorder',
                                 'Responsive',
                                 'Scroller'),
                  style = 'bootstrap',
                  class = 'table-bordered stripe table-condensed',
                  filter = list(position = 'top', clear = FALSE),
                  rownames = FALSE,
                  options = list( dom = 'Bfrtip',
                                  buttons = list(list(extend = 'csv',
                                                      buttons = c('csv'),
                                                      exportOptions = list(modifiers = list(page = "current"))
                                  )
                                  ),
                                  search = list(regex = TRUE, caseInsensitive = FALSE),
                                  #autofill = TRUE,
                                  colReorder = TRUE,
                                  deferRender = TRUE,
                                  scrollY = 200,
                                  scroller = TRUE,
                                  columnDefs = list(list(className = 'dt-left', targets = '_all'))
                  )
    )
  })

}

shinyApp(ui, server)
8nuwlpux

8nuwlpux2#

这对我很有效:

columnDefs = list(list(className = 'dt-head-center', targets = '_all'))

相关问题