闪亮数据表

时间:2017-06-26 14:26:54

标签: css r dt

我一直在闪亮的应用程序中使用DT一段时间。我想知道是否有任何选项(简单的方法)在文本很长时更改表头方向(比如将所有的同名旋转45度等),当表中有很多列时,这是一个问题。 谢谢, 这是一个简短的例子:

library(shiny)
library(DT)
ui <- fluidPage(
  mainPanel(
    tabsetPanel(
      tabPanel("Table", br(),
               dataTableOutput("myTable"))
    ), width = 9
  )
)

server <- function(input, output) {
  output$myTable <- renderDataTable({
  test <- data.frame(1:20, letters[1:20], stringsAsFactors = FALSE)
  colnames(test) <- c("This is a long name", "This column name is much more longer!")
  datatable(test, rownames = FALSE, options = list(autoWidth = TRUE, searching = TRUE, lengthMenu = list(c(5, 10, 25, 50, -1), c('5', '10', '25', '50', 'All')), pageLength = 10)) # %>% formatStyle(names(test))
  })
}

shinyApp(ui, server)

1 个答案:

答案 0 :(得分:0)

这是将页眉旋转90度的一种方法。

library(DT)
library(shiny)

headerCallback <- c(
  "function(thead, data, start, end, display){",
  "  var $ths = $(thead).find('th');",
  "  $ths.css({'vertical-align': 'bottom', 'white-space': 'nowrap'});",
  "  var betterCells = [];",
  "  $ths.each(function(){",
  "    var cell = $(this);",
  "    var newDiv = $('<div>', {height: 'auto', width: cell.height()});",
  "    var newInnerDiv = $('<div>', {text: cell.text()});",
  "    newDiv.css({margin: 'auto'});",
  "    newInnerDiv.css({",
  "      transform: 'rotate(180deg)',",
  "      'writing-mode': 'tb-rl',",
  "      'white-space': 'nowrap'",
  "    });",
  "    newDiv.append(newInnerDiv);",
  "    betterCells.push(newDiv);",
  "  });",
  "  $ths.each(function(i){",
  "    $(this).html(betterCells[i]);",
  "  });",
  "}"
)

ui <- fluidPage(
  DTOutput("table")
)

server <- function(input, output){
  output[["table"]] <- renderDT({
    datatable(iris,  
              options = list(
                headerCallback = JS(headerCallback),
                columnDefs = list(
                  list(targets = "_all", className = "dt-center")
                )
              ))
  })
}

shinyApp(ui, server)

enter image description here