R Shiny 设置DataTable列宽

标签 r shiny dt

我正在尝试设置以 Shiny 呈现的 DataTable 中的列宽度,但无法使用 aoColumnDefs 选项来实现它。以前有人尝试过这个吗?我的表格有 1 个文本,后跟 3 个数字列。数字列需要更窄,第一列(文本)需要更宽。

output$result <- renderDataTable({
z <- as(dataInput(), "data.frame")
setnames(z, c("Rules", "Support", "Confidence", "StatDep"))
z
}, options = list(aLengthMenu = c(5, 30, 50), iDisplayLength = 5, bSortClasses = TRUE,
              aoColumnDefs = list(sWidth = "50px", aTargets = list(1))))

谢谢

  • 拉吉。

** 更新 ** 这似乎有效,但可能还有其他选项可以做到这一点。

output$result <- renderDataTable({
z <- as(dataInput(), "data.frame")
setnames(z, c("Rules", "Support", "Confidence", "StatDep"))
z
}, options = list(aLengthMenu = c(5, 30, 50), iDisplayLength = 5, bSortClasses = TRUE,
              bAutoWidth = FALSE,
              aoColumn = list(list(sWidth = "150px", sWidth = "30px",
                                       sWidth = "30px", sWidth = "30px"))
                                  ))

最佳答案

试试这个

#OUTPUT - dtdata
output$table <- DT::renderDataTable({
  data.frame(a=c(1,2,3,4,5),b=c("A","B","C","D","E"))
},
options = list(
  autoWidth = TRUE,
  columnDefs = list(list(width = '200px', targets = "_all"))
))

将所有列的宽度设置为 200px。

要设置所选列的宽度,请将targets更改为数字或向量。

targets = c(1,3)

关于R Shiny 设置DataTable列宽,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25205410/

相关文章:

r - 让 Shiny (RMarkdown)使用完整的浏览器窗口

R Shiny,如何使数据表对数据表中的复选框使用react

readLines 与同时使用 Azure Data Lakes 和 R Server 时等效

r - 针对特定条件在 R 中格式化 X Axis 标签

r - 托管和设置自己的 Shiny 应用程序,无需 Shiny 服务器

R Shiny 在加载 UI 之前观察运行,这会导致 Null 参数

r - 如何在DT header中显示HTML?

r - 使用 R DataTable 包添加加载微调器

r - 为我的逻辑回归模型生成逻辑曲线

r - 如何为同一天出现的设备分配编号?