rstudio / DT

R Interface to the jQuery Plug-in DataTables
https://rstudio.github.io/DT/
Other
598 stars 181 forks source link

RowGroup extension with rownames = TRUE #1109

Closed isthisthat closed 8 months ago

isthisthat commented 9 months ago

The RowGroup extension does not appear to work properly when rownames = TRUE. It repeats the row header for each row (even when the grouping column has the same value). I'm aware of the support issues with DT, just filing this FYI. Thank you


By filing an issue to this repo, I promise that

I understand that my issue may be closed if I don't fulfill my promises.

yihui commented 8 months ago

I'm not sure what you meant by "It repeats the row header for each row (even when the grouping column has the same value)". Here is my minimal reprex:

d = data.frame(
  Group = rep(xfun::n2w(1:3), length.out = 26),
  a = sample(26), b = letters, c = factor(rep(c('a', 'b'), 13)),
  d = Sys.Date() + 1:26, e = Sys.time() + 1000 * (1:26), row.names = LETTERS
)

DT::datatable(
  d, rownames = FALSE,
  options = list(rowGroup = list(dataSrc = 0), order = list(list(0, 'asc'))),
  extensions = 'RowGroup'
)

DT::datatable(
  d, rownames = TRUE,
  options = list(rowGroup = list(dataSrc = 1), order = list(list(1, 'asc'))),
  extensions = 'RowGroup'
)

rownames = FALSE

image

rownames = TRUE

image
isthisthat commented 8 months ago

Sorry that was poor triaging on my part. There is no issue with rownames. The actual issue is with colReorder and rowGroup it seems. Here is a reproducible example:

renderDT({
  datatable(setDT(data.frame(A=c(1,2,3,4),B=c(5,6,7,8),G=c("a","a","b","b"))), rownames=FALSE,
          options = list(
                         colReorder = TRUE,
                         rowGroup = list(dataSrc = 2)
          ),
          extensions = c("ColReorder", "RowGroup")
  )
}, server=TRUE)

Initially the table is row-grouped by G. When you move column A (or any column) to the place where column G was, and sort by that column, the row-grouping changes to that column, for example: image

yihui commented 8 months ago
d = data.frame(
  A = c(1, 2, 3, 4),
  B = c(5, 6, 7, 8),
  G = c("a", "a", "b", "b")
)

DT::datatable(
  d, rownames = FALSE,
  options = list(
    colReorder = TRUE,
    rowGroup = list(dataSrc = 2)
  ),
  extensions = c("ColReorder", "RowGroup")
)

Okay, I see the problem now. Technically it shouldn't be too difficult to fix. We need to reset the dataSrc attribute for the rowGroup extension in the column-reorder event:

I'm not sure if anyone wants to help. I don't have time to work on it at the moment, but can take another look in a couple of months if no one sends a PR.

yihui commented 8 months ago

Our great helper @mikmart has made it again! Now you can try the development version:

remotes::install_github('rstudio/DT')
isthisthat commented 8 months ago

I can confirm it works! Thank you so much @mikmart , you're on a roll!!

mikmart commented 8 months ago

Great, thanks for testing!