0

我正在寻找 R 中的一个库,它可以生成图片中所附的结果,即多个 columnnames 和 rownames。目前,我出于相同目的使用rpivotTable 和 DT::Datatable 库并使用它们构建ShinyApp

但我面临的问题是使用 ShinyApp将输出导出到 excel中,这要么带有错误的标题重复或跳过标题。例如,当我按照下面的代码下载 Iris 的输出时,我没有得到 Sepal 和 Petal 的顶部标题。

我需要这个

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

0 回答 0