r shiny problem with datatable to copy a table with table head (colspan)

359 Views Asked by At

I have a shiny app and would like to offer that the tables can be downloaded or copied to the clipboard. However, in the clipboard-Copy or the excel file the headers are missing.

Can someone tell me what my error is and how to solve the problem?

library(shiny)
library(DT)

ui <- basicPage(
  h2("Test the copy button"),
  DT::dataTableOutput("mytable")
)

server <- function(input, output) {
  output$mytable = DT::renderDataTable({
    
    sketch = htmltools::withTags(table(
      class = 'display',
      thead(
        tr(# this is missing in the clipboard copy and excel: 
          th(rowspan = 2, 'Species'),
          th(colspan = 2, 'Sepal'),
          th(colspan = 2, 'Petal')
        ),
        tr(
          lapply(rep(c('Length', 'Width'), 2), th)
        )
      )
    ))

    datatable(iris[1:5, c(5, 1:4)],
              container = sketch, 
              rownames = FALSE, 
              extensions = c('Buttons'),
              options = list(dom = 'B',
                             buttons = list(
                               list(extend = "copy", text = "copy"),
                               list(extend = "excel", filename = "Test_excel"))))
  })
}


shinyApp(ui, server)

My problem in a picture: my problem in a picture

0

There are 0 best solutions below