合并DT :: datatable中的列 [英] Merge columns in DT::datatable

查看:177
本文介绍了合并DT :: datatable中的列的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要合并闪亮的DT :: datatable中跨列的单元格。最好的方法似乎是利用Javascript DataTables扩展



为此



解决方案

使用 htmltools

 库(闪亮)
库(DT)
库(htmltools)

tbl<-data.frame( A = c(1L, question),
B = c(2L, answer))

container_dt = withTags(table(
class ='display',
thead(
tr(
th(class ='dt-center',colspan = 2,'AB')),
tr(
lapply((c('foo','bar')),th) ))))


ui<-fluidPage(
dataTableOutput( table)


服务器<-函数(输入,输出){

output $ table<-renderDT({
datatable(tbl,container = container_dt,rownames = F,class =,
options = list(autoWidth = T,
columnDefs = list(list(className = dt-center,target = _all),
list(width = 40px,target = _all)) ))
})
}

ShinyApp(ui = ui,服务器=服务器)


I need to merge cells across columns in a DT::datatable in shiny. The best way seems to make use of the Javascript DataTables extension RowGroup. But I don't know which steps to take from viewing the page in above link, to having merged cells in my shiny app (there's a reason I'm working in shiny ;).

There's a partial answer in the accepted answer to this stackoverflow question but 1) that was about merging rows (i.e. vertically in stead of horizontally), and 2) the mechanics behind the interaction of R and Javascript seem to be assumed as prior knowledge, leaving me with questions like "which files do I need to download from where" and "do I need to adapt the Javascript code in them?"

Here is a simplified example of my app:

library(shiny)
library(DT)

tbl <- data.frame("A"=c("foo", 1L, "question"),
                  "B"=c("bar", 2L, "answer"))

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

server <- function(input, output) {

  output$table <- renderDT({
    datatable(tbl, rownames=F, class="",
              options = list(autoWidth=T,
                             columnDefs = list(list(className="dt-center", targets="_all"),
                                               list(width="40px", target="_all"))))
  })
}

shinyApp(ui = ui, server = server)

In which I'd like to go from this

to this

解决方案

This may work for you, using htmltools

library(shiny)
library(DT)
library(htmltools)

tbl <- data.frame("A" = c( 1L, "question"),
                  "B" = c( 2L, "answer"))

container_dt= withTags(table(
  class = 'display',
  thead(
    tr(
      th(class = 'dt-center',colspan = 2, 'AB')),
      tr(
      lapply((c('foo', 'bar')), th)))))


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

server <- function(input, output) {

    output$table <- renderDT({
        datatable(tbl, container = container_dt, rownames = F, class = "",
              options = list(autoWidth = T,
                             columnDefs = list(list(className = "dt-center", targets = "_all"),
                                               list(width = "40px", target = "_all"))))
    })
}

shinyApp(ui = ui, server = server)

这篇关于合并DT :: datatable中的列的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆