R Shiny making DataTable columns manuallly resizable

461 Views Asked by At

I would like to ask if it is possible to let the user manually adjust the column widths by clicking on the column and dragging it. If so, how?

2

There are 2 best solutions below

6
Stéphane Laurent On BEST ANSWER

You can do that with the colResize plugin. Download the js file and the css file. In the code below, path/to/colResize is the path to the folder containing these two files. If you use an absolute path, you don't need normalizePath.

library(DT)
library(htmltools)

dep <- htmlDependency(
  name = "colResize", 
  version = "1.6.1", 
  src = normalizePath("path/to/colResize"),
  script = "jquery.dataTables.colResize.js",
  stylesheet = "jquery.dataTables.colResize.css",
  all_files = FALSE
)

dat <- iris

dtable <- datatable(
  dat,
  options = list(
    colResize = list()
  )
) 

deps <- dtable$dependencies
deps <- c(deps, list(dep))
dtable$dependencies <- deps

dtable

enter image description here

0
Ulrik Lyngs On

tl;dr: Perhaps because of an issue with the latest version of datatables, you need to adjust jquery.dataTables.colResize to

table.dataTable thead th.dt-colresizable-hover {
  cursor: col-resize !important;

Otherwise the resizing cursor won't show up.