简体   繁体   中英

Prevent column name wrap in shiny DataTable

I have a shiny DataTable (package "DT") with quite long column names (+ whitespace) that I want to be rendered without name wrapping - ie colnames wrapped over 2-3 lines. I have enabled horizontal scrolling to try and facilitate this:

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

but by default the whitespace is collapsed to new lines.

I think this answers my question: https://www.datatables.net/forums/discussion/8923/how-do-you-stop-the-header-from-wrapping-into-multiple-rows but I'm not sure how to translate this to the R function.

In addition, all DataTable options are listed here: https://www.datatables.net/reference/option/

Thanks in advance.

In ui.R add the following line before the line where you render the table:

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

Replace table1 with xxxxx from your output statement in server.R file

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

You can simply use the nowrap class:

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")

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM