Beeba
Beeba

Reputation: 642

Shiny merge cells in DT::table on two or more columns

Similar to this question: Shiny: Merge cells in DT::datatable

I was wondering if there was a way to merge on two or more columns. In the example below only duplicate rows in column 1 are merged, what if I wanted to also merge the duplicate rows in column 5 in each of the sections. For example, in the image of the table I want the 0.2's in Petal.Width to be merged, as well as the 1.5's in Petal.Width. Is that possible?

library(shiny)
library(DT)

dat <- iris[c(1,2,3,51,52,53,101,102,103), c(5,1,2,3,4)]

ui <- fluidPage(
  DTOutput("table")
)

server <- function(input, output){
  output[["table"]] <- renderDT({
    dtable <- datatable(dat, rownames = FALSE, 
                        options = list(
                          rowsGroup = list(0) # merge cells of column 1
                        ))
    path <- "U:/Data/shiny/DT/www" # folder containing dataTables.rowsGroup.js
    dep <- htmltools::htmlDependency(
      "RowsGroup", "2.0.0", 
      path, script = "dataTables.rowsGroup.js")
    dtable$dependencies <- c(dtable$dependencies, list(dep))
    dtable
  })
}

shinyApp(ui, server)

enter image description here

Upvotes: 1

Views: 1520

Answers (1)

heds1
heds1

Reputation: 3448

All you need to do is use the answer you linked (with the datatables-rowsgroup plugin) and specify extra columns to merge on duplicate results:

dtable <- datatable(
    dat,
    rownames = FALSE, 
    options = list(
       rowsGroup = list(0,4))) # merge cells of column 1 and 5

enter image description here

Upvotes: 3

Related Questions