如何使R DataTables列宽度取决于单元素内容宽度?

发布于 2025-02-06 17:52:55 字数 1485 浏览 1 评论 0原文

我知道您可以使用选项手工调整宽度。沿线的内容:

DT::datatable(mtcars,
              options = list(
                  autoWidth = TRUE,
                  columnDefs = list(list(width = '70px', 
                                         targets = which(colnames(mtcars) %in% c("gear"))),
                                    list(width = '100px', 
                                         targets = which(colnames(mtcars) %in% c("disp", "hp")))
                  )
              )
)

我认为没有方法可以调整列宽度以拟合内容,而没有硬编码值。我找到了一个dataTables函数 columns.adjustss.adjust()从理论上重新调整列宽度以输入,但是如何在R / R中使用它?

MRE:

library(shiny)
library(DT)

ui <- fluidPage(
  DT::dataTableOutput(outputId = "table")
)

server <- function(input, output) {

  output$table <- DT::renderDataTable({
    DT::datatable(mtcars,
                options = list(
                    autoWidth = TRUE, # here we set fixed width. how to set depending on input?
                    columnDefs = list(list(width = '70px', 
                                           targets = which(colnames(mtcars) %in% c("gear"))),
                                      list(width = '100px', 
                                         targets = which(colnames(mtcars) %in% c("disp", "hp")))
                    )
                )
  )
  })
}

shinyApp(ui, server)

I know you can use options to adjust width by hand. Something along the lines:

DT::datatable(mtcars,
              options = list(
                  autoWidth = TRUE,
                  columnDefs = list(list(width = '70px', 
                                         targets = which(colnames(mtcars) %in% c("gear"))),
                                    list(width = '100px', 
                                         targets = which(colnames(mtcars) %in% c("disp", "hp")))
                  )
              )
)

I see no way to adjust the column width to fit content, without hardcoded values. I found a datatables function columns.adjust() which theoretically re-adjusts column width to input, but how to use it in R / R shiny?

MRE:

library(shiny)
library(DT)

ui <- fluidPage(
  DT::dataTableOutput(outputId = "table")
)

server <- function(input, output) {

  output$table <- DT::renderDataTable({
    DT::datatable(mtcars,
                options = list(
                    autoWidth = TRUE, # here we set fixed width. how to set depending on input?
                    columnDefs = list(list(width = '70px', 
                                           targets = which(colnames(mtcars) %in% c("gear"))),
                                      list(width = '100px', 
                                         targets = which(colnames(mtcars) %in% c("disp", "hp")))
                    )
                )
  )
  })
}

shinyApp(ui, server)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文