Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Avoid Wrapping Text only in first column of R datatable

Tags:

r

datatable

shiny

I'm struggling with keeping text in one column of a datatable from wrapping.

A screenshot of the table

I'd like to avoid wrapping text in the first column (as it's the only part that makes the row size bigger), but keep the option in the headers (to avoid having to scroll).

I've tried adjusting the width of the first column, but the text keeps wrapping no matter what size I use.

DT::datatable(chartfilter,
    rownames = FALSE,
    options=list(iDisplayLength=7,                    
                 bPaginate=FALSE,                  
                 bLengthChange=FALSE,                       
                 bFilter=FALSE,                                    
                 bInfo=FALSE,
                 rowid = FALSE,
                 autoWidth = FALSE,
                 ordering = FALSE,
                 scrollX = TRUE,
                 columnDefs = list(list(width='500px', targets = list(1)))

I've also found a solution that turns text wrapping off in the entire table - but I don't want that for my column labels. Adding this in the UI in front of the tableoutput:

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

Is this possible, or do I just have to accept wrapping text in the first column? Appreciate any help I can get, and let me know if more info is needed.

like image 630
reefersadness Avatar asked Mar 04 '18 16:03

reefersadness


1 Answers

Use the formatStyle() function to apply a specific style to a column:

datatable() %>% formatStyle("Region","white-space"="nowrap")

This function comes from the same library (DT). More info on formatStyle() can be found here: https://rstudio.github.io/DT/010-style.html

like image 92
Dale Kube Avatar answered Oct 18 '22 03:10

Dale Kube