Prevent column name wrap in shiny DataTable
Asked Answered
F

2

8

I have a shiny DataTable (package "DT") with quite long column names (+ whitespace) that I want to be rendered without name wrapping - i.e. 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.

Feodore answered 8/7, 2015 at 13:0 Comment(0)
J
8

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")
Jaimeejaimes answered 14/6, 2019 at 12:56 Comment(0)
S
7

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(.....
Spitter answered 8/7, 2015 at 14:51 Comment(1)
good answer - thanks! in my case, there was tags$head defined already, so I needed just to provide: "tags$style(HTML('#current_data{white-space:nowrap}'))," - dont forget about '#'!Chaco

© 2022 - 2024 — McMap. All rights reserved.