reefersadness
reefersadness

Reputation: 101

Avoid Wrapping Text only in first column of R datatable

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.

Upvotes: 9

Views: 6050

Answers (1)

Dale Kube
Dale Kube

Reputation: 1460

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

Upvotes: 17

Related Questions