我正在尝试设置以 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/