【问题标题】:How can I align datatable column headers to the left in Shiny?如何在 Shiny 中将数据表列标题向左对齐?
【发布时间】:2018-06-19 21:25:11
【问题描述】:

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

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)

自己看

【问题讨论】:

标签: r shiny dt leftalign


【解决方案1】:

应该这样做。

使用#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)

【讨论】:

  • 太好了!有没有办法将 css 提取到文件中,而不是放在 ui 中?
  • 是的,当然,您可以编写一个 css 文件,将其存储在 www 文件夹中,并将此 includeCSS("*****.css") 包含在您的 ui 中。看看这个page
  • 将其添加到 www 不起作用(可能是主题 =),而只是将其添加到应用程序旁边。R 做到了!
猜你喜欢
  • 2017-12-08
  • 1970-01-01
  • 2017-01-01
  • 2017-11-26
  • 1970-01-01
  • 2018-01-22
  • 1970-01-01
  • 1970-01-01
  • 2016-10-29
相关资源
最近更新 更多