55

I am trying to set the width of columns in a DataTable rendered in Shiny and am not able to implement it using the aoColumnDefs options. Has anyone tried this before ? My table has 1 text followed by 3 numeric columns. The numeric columns need to be narrower and the 1st column (text) wider.

output$result <- renderDataTable({
z <- as(dataInput(), "data.frame")
setnames(z, c("Rules", "Support", "Confidence", "StatDep"))
z
}, options = list(aLengthMenu = c(5, 30, 50), iDisplayLength = 5, bSortClasses = TRUE,
              aoColumnDefs = list(sWidth = "50px", aTargets = list(1))))

Thanks,

  • Raj.

** Update ** This seems to be working, but there might be other options to do this as well.

output$result <- renderDataTable({
z <- as(dataInput(), "data.frame")
setnames(z, c("Rules", "Support", "Confidence", "StatDep"))
z
}, options = list(aLengthMenu = c(5, 30, 50), iDisplayLength = 5, bSortClasses = TRUE,
              bAutoWidth = FALSE,
              aoColumn = list(list(sWidth = "150px", sWidth = "30px",
                                       sWidth = "30px", sWidth = "30px"))
                                  ))
John Paul
  • 12,196
  • 6
  • 55
  • 75
xbsd
  • 2,438
  • 4
  • 25
  • 35

2 Answers2

54

Try this

#OUTPUT - dtdata
output$table <- DT::renderDataTable({
  data.frame(a=c(1,2,3,4,5),b=c("A","B","C","D","E"))
},
options = list(
  autoWidth = TRUE,
  columnDefs = list(list(width = '200px', targets = "_all"))
))

Sets the width of all columns to 200px.

To set width of selected columns, change targetsto a number or vector.

targets = c(1,3)
mindlessgreen
  • 11,059
  • 16
  • 68
  • 113
  • 9
    To set different column widths for multiple columns you can use: columnDefs = (list(list(width = '200px', targets =c(0, 2)), list(width = '80px', targets =c(6)))) – Nivel Nov 30 '19 at 07:51
  • 1
    This doesn't seem to work. The actual solution is to set option `scrollX = TRUE` (https://github.com/rstudio/DT/issues/29#issuecomment-162093790) then specify column widths using CSS (https://stackoverflow.com/a/44110579/8605348). – ardaar Jul 12 '21 at 16:14
  • 1
    This does not work. – jzadra Feb 08 '22 at 01:23
  • It **DOES** work, [source](https://rstudio.github.io/DT/options.html) – vladli May 27 '22 at 19:50
15

By the way, in case you're like me and never used DataTables before version 1.10 came out -- The examples above confused me at first, because they use the notation that was used in version 1.9 but 1.10 introduces new notation: http://datatables.net/upgrade/1.10-convert

I've been using the new syntax, i.e.,

columnDefs instead of aoColumnDefs http://datatables.net/reference/option/columnDefs

width instead of sWidth http://datatables.net/reference/option/columns.width etc.

numbercruncher
  • 924
  • 6
  • 10
  • 2
    for those of us who are not familiar with javascript, translating the datatable documentation at their website e.g. [http://datatables.net/reference/option/columns.width] to an equivalent `R` code becomes difficult. Esp the level of nesting needed in a list is not clear by looking at the JSON syntax. Is there any tips around that? – Lazarus Thurston Feb 06 '20 at 04:15
  • Useful to know about the new notation, but this answer does not give or point to an up-to-date DT syntax to use. – geotheory Oct 08 '20 at 12:39
  • 1
    @geotheory [here you go](https://rstudio.github.io/DT/options.html) – vladli May 27 '22 at 19:51