Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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 - 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.

like image 683
Jordan Mackie Avatar asked Jul 08 '15 13:07

Jordan Mackie


2 Answers

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(.....
like image 133
Sri Avatar answered Oct 15 '22 03:10

Sri


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")
like image 39
Stéphane Laurent Avatar answered Oct 15 '22 03:10

Stéphane Laurent