gpt4 book ai didi

css - Shiny 数据表中的标题方向

转载 作者:行者123 更新时间:2023-12-02 03:53:25 24 4
gpt4 key购买 nike

我在我的 Shiny 应用程序中使用 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)

最佳答案

这是将标题旋转 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

关于css - Shiny 数据表中的标题方向,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44762015/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com