Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Shiny: Merge cells in DT::datatable

Tags:

r

dt

shiny

I would like to merge few rows in column in DT::datatable in shiny. Is it possible to do so?

Currently I am able to output which looks something like this:

enter image description here

But ideally I would like to merge the rows and want to output something like this:

enter image description here

Is it possible to merge rows like this in DT::datatable?

like image 448
SBista Avatar asked Sep 14 '16 06:09

SBista


People also ask

How do you merge cells in a table design?

Merge cellsIn the table, drag the pointer across the cells that you want to merge. Click the Layout tab. In the Merge group, click Merge Cells.

What is merging cells in a table?

You can split and merge cells in a table. Splitting cells is similar to adding a row or column, but it all takes place in one cell instead of a group of cells. Merging cells, however, is similar to deleting a cell and then adjoining it with a neighboring cell.


2 Answers

It is possible with the help of the datatables-rowsgroup library. Here is an example:

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

like image 117
Stéphane Laurent Avatar answered Nov 06 '22 01:11

Stéphane Laurent


hey as far as i know its not possible to do it in DT i have another way to make it happen.

 kable(c, align = "c") %>%
  kable_styling(bootstrap_options = "striped", full_width = F, position = "left",font_size = 12)%>%
  column_spec(1, bold = T) %>%
  collapse_rows(columns = 1, valign = "middle")

please try it and it works :)

like image 1
Uttam Gogineni Avatar answered Nov 06 '22 01:11

Uttam Gogineni