防止Shiny DataTable中列名换行

7

我有一个闪亮的DataTable(包“DT”),其中列名很长(带空格),我希望不换行地呈现 - 即列名不会跨越2-3行。我已经启用了水平滚动来尝试实现这一点:

renderDataTable(dataframe_with_long_colnames, ..., options = list(scrollX = TRUE))

但默认情况下,空格会折叠成新行。
我认为这回答了我的问题: https://www.datatables.net/forums/discussion/8923/how-do-you-stop-the-header-from-wrapping-into-multiple-rows 但我不确定如何将其翻译为R函数。
此外,所有DataTable选项都在此处列出:https://www.datatables.net/reference/option/ 提前致谢。
2个回答

7

在ui.R中,在呈现表格的那一行之前添加以下行:

tags$head(tags$style("#table1  {white-space: nowrap;  }")),

server.R文件中,将输出语句中的table1替换为xxxxx

output$`xxxxx`<-renderDataTable(.....

好的答案 - 谢谢!在我的情况下,已经定义了tags$head,所以我只需要提供:"tags$style(HTML('#current_data{white-space:nowrap}'))," - 别忘了 '#'! - xhudik

7
你可以简单地使用 nowrap 类:
library(DT)

dat <- data.frame(
  "This is a looooooooooooooooonnnnnnnnnnnnggggggg column name" = c(1,2),
  "This is also a looooooooooooooooooonnnnnnnnnnnggggggg column name" = c(3,4),
  check.names = FALSE
)

datatable(dat, class = "display nowrap")

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接