Shiny: Merge cells in DT::datatable

血红的双手。 提交于 2019-11-30 04:43:57

问题


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:

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

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


回答1:


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)




回答2:


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 :)



来源:https://stackoverflow.com/questions/39484118/shiny-merge-cells-in-dtdatatable

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!