Search code examples
rdatatabledt

Widen R datatable column


I have created a datatable in R as you can see in the image. I have 2 things I would like to change:

  1. I would like column 1 to be wider so that the pay bands are shows all in one line e.g £15,001- £20,000 - how would I do that?

  2. Can I remove the search bar? enter image description here

This is my current code:

tab1a <- datatable(tab1a,
                   fillContainer = FALSE,
                          rownames = FALSE,
                          extensions = 'Buttons', options = list(
                            columnDefs = list(list(className = "dt-body-right", targets = "_all"),
                                              list(className = "dt-center", targets = "_all")),
                            pageLength = 20,
                            lengthMenu = c(5, 10, 15, 20),
                            dom = 'Bfrtip',
                            buttons = list(
                              list(extend = "pdf", exportOptions = list(columns = ":visible")),
                              list(extend = "excel", exportOptions = list(columns = ":visible")),
                              list(extend = "csv", exportOptions = list(columns = ":visible")),
                              list(extend = "copy", exportOptions = list(columns = ":visible")),
                              list(extend = "print", exportOptions = list(columns = ":visible"))
                            )
                              ))

Solution

  • The index of the column starts from '0'. Also set option scrollX=TRUE and searching = F. The width of the first column is set to be 200 as an example.

    datatable(tab1a, 
              fillContainer = FALSE,
                        rownames = FALSE,
                        extensions = 'Buttons',
              options=list(columnDefs = list( list(className = "dt-body-right", targets = "_all"),
                                              list(className = "dt-center", targets = "_all"),
                                              list(width = 200, targets = c(0))
                                             ),
                           autoWidth = TRUE,
                           scrollX=TRUE,
                           searching = F,
                                       pageLength = 20,
                                       lengthMenu = c(5, 10, 15, 20),
                                       dom = 'Bfrtip',
                                       buttons = list(
                                         list(extend = "pdf", exportOptions = list(columns = ":visible")),
                                         list(extend = "excel", exportOptions = list(columns = ":visible")),
                                         list(extend = "csv", exportOptions = list(columns = ":visible")),
                                         list(extend = "copy", exportOptions = list(columns = ":visible")),
                                         list(extend = "print", exportOptions = list(columns = ":visible"))
                                       )
                           )
              )