12

I need to set the column width of a DataTabe in an R Shiny app. I went through the Data Table Options in the documentation. Also, I reviewed this and this questions in Stackoverflow.

Setting width works fine if the DataTable does not have too many columns. In cases, however, if multiple columns present the settings is overridden despite the absolute width unit (e.g. '600px').

In the below code snippet:

output$mytable <- DT::renderDataTable({
  num_of_cols <- 3
  cbind(iris,iris)[,1:num_of_cols]},
  options = list(autoWidth = TRUE,
  columnDefs = list(list(width = '500px', targets = 1))))

if I set variable num_of_cols = 3 it works fine. Increasing displayed columns (num_of_cols), however, leads to decrease in column width. In case of lots of displayed columns the width setting seemingly has no effect.

I tried option autoWidth = FALSE but it gives no different result. I also tried using JavaScript in options drawCallback as described in the answer section of this thread but it gives the same result.

How can I have DataTable display the desired column width setting?

Community
  • 1
  • 1
Szilard
  • 175
  • 1
  • 1
  • 10

2 Answers2

12

Wow. I can't believe this. Column width on a table object should be so clear and yet for data tables it was so confusing. I spent a lot of time researching this and coming up dry before finally discovering the answer in the tool tip displays in RStudio.

DT::renderDataTable({
      datatable(df) %>% formatStyle(columns = c(1,2), width='200px')
})

EDIT:

This might work, but it doesn't actually work any better than any other way. There is no straight forward way to do this that I could tell. Here's the code I am NOW using... I don't think I will need to use anything else. I had to build it out column by column and continually go back and adjust to get it to work. Seems like something that should be fixed.

I set the UI width value to 1200px to start and scaled it back when I was all done.

#UI
DT::dataTableOutput(outputId = "tableID", width = '830px')
#Server
options = list(autoWidth = TRUE,
              columnDefs = list(list(targets=c(0), visible=TRUE, width='90'),
                                list(targets=c(1), visible=TRUE, width='145'),
                                list(targets=c(2), visible=TRUE, width='105'),
                                list(targets=c(3), visible=TRUE, width='100'),
                                list(targets=c(4), visible=TRUE, width='100'),
                                list(targets=c(5), visible=TRUE, width='100'),
                                list(targets=c(6), visible=TRUE, width='100'),
                                list(targets=c(7), visible=TRUE, width='90'),
                                list(targets='_all', visible=FALSE)
DonkeyKong
  • 727
  • 8
  • 15
  • 2
    This answer has been getting attention so I wanted to share that I transitioned to Python. R was exciting when I first discovered it, but my experience was that many features are frustrating to use like the DataTable formatting. I use Python for everything. Python can do everything that R does, can do it as well if not better, can do a lot more, is better supported, has better tools, and the code is cleaner, more concise, and easier to follow. Take a look at Plotly Dash for Python, and I hope this will save you a lot of time and agony going forward. – DonkeyKong Jul 07 '19 at 16:25
  • 4
    Cool story. In reality Dash does not come even close to Shiny (so far) – DSGym Aug 02 '19 at 09:26
  • thank you very much for the answer. There's one more thing I'd like to add that could screw up those custom column widths. you need to make sure you have filter = 'none' otherwise the min column width with correspond to the width required to allow columns filtering. – Angelo Jun 24 '20 at 22:32
  • Thanks @DonkeyKong! For me it worked just making the change in the options of my DT:datatable similar as you mentioned: ```options = list(autoWidth = TRUE, columnDefs = list(list( list(targets='_all', visible=TRUE, width='90') ) ``` I just wanted the same width for all the columns. – Corina Roca Jan 11 '21 at 15:27
6

As suggested here, setting scrollX=T and keeping autoWidth = TRUE in the options should work.

Community
  • 1
  • 1
jbz
  • 91
  • 1
  • 5