0

I'm looking for a library in R that can generate the results as attached in the picture i.e. multiple columnnames and rownames. Currently i'm using rpivotTable and DT::Datatable library for the same purpose and building a ShinyApp using them.

But the Problem that i'm facing with them is in exportation of output to excel using ShinyApp, which either comes with wrong repetition of headers or skipping of headers. e.g when i download the output of Iris as given in codes below, i don't get top headers of Sepal and Petal.

I need this

X

library(shiny)
library(DT)

iris<-iris[,c(5,1:4)]

ui =basicPage(
tags$head(
tags$style(type = "text/css",
           HTML("th { text-align: center; }")
)
),

selectInput(inputId = "Species", 
          label = "Species:",
          choices = c("All",
                      unique(as.character(iris$Species)))),


h2('Iris Table'),
DT::dataTableOutput('mytable')
)


server = function(input, output) {
output$mytable = DT::renderDataTable({

  # a custom table container
  sketch = htmltools::withTags(table(
    class = 'display',
    thead(
      tr(
        th(rowspan = 2, 'Species'),
        th(colspan = 2, 'Sepal'),
        th(colspan = 2, 'Petal')
      ),
      tr(
        lapply(rep(c('Length', 'Width'), 2), th)
      )
    )
  ))

  DT::datatable(filter = "top",  rownames = FALSE, container = sketch,
                extensions = 'Buttons',
                escape = FALSE,
                options = list(dom = 'Bfrtip',
                               buttons = 
                                 list('colvis', list(
                                   extend = 'collection',
                                   buttons = list(list(extend='csv',
                                                       filename = 
'hitStats'),
                                                  list(extend='excel',
                                                       filename = 
'hitStats'),
                                                  list(extend='pdf',
                                                       filename= 
'hitStats'),
                                                  list(extend='copy',
                                                       filename = 
'hitStats'),
                                                  list(extend='print',
                                                       filename = 
'hitStats')),
                                   text = 'Download'
                                 )),
                               scrollX = TRUE,
                               pageLength = nrow(iris),
                               order=list(list(2,'desc'))),               


                 {

                   data<-iris

                   if(input$Species != 'All'){
                     data<-data[data$Species == input$Species,]
                   }



                   # data<-data[,c("Species",input$columns),drop=FALSE]
                   # 

                   data

                 })

})
}


shinyApp(ui = ui, server = server)
Doctor
  • 59
  • 1
  • 11
  • 1
    I think `flextable` would be a good bet. See [here](https://davidgohel.github.io/flextable/index.html). – p0bs Nov 14 '18 at 18:33
  • Thanks mate. While using *flextable*, can data with multiple headers be exported to Excel as it is? – Doctor Nov 18 '18 at 20:48
  • Sorry, I don't know, but I suspect that the documentation for `flextable` should help ... or a previous question on Stack Overflow! – p0bs Nov 19 '18 at 13:46

0 Answers0