【问题标题】:Customize output of excel export of button extension自定义按钮扩展的excel导出输出
【发布时间】:2016-08-24 22:54:01
【问题描述】:

是否可以调整 Excel 输出的输出? 我希望能够以递减的紧迫性做以下事情。

  1. 在表中添加一个包含一些文本“此表基于 iris 数据集并使用 input$width 作为最小值”的表头
  2. 为列名标题添加粗底边框
  3. 在第一列后添加左边框
  4. 在标题上方添加一个空行
  5. 我可以在一些合并的单元格中写入内容,即我想在长度、宽度、...的四列上方写下“花瓣大小”...

这是一个使用按钮扩展的 MWE。我找到了一些原始 javascrtip DT here 的信息,但这对我来说有点难以转移到 R 中。

    rm(list=ls())   
    library(shiny)
    library(datasets)
    library(DT)
    library(data.table)
    DT<-data.table(iris)
    server<-shinyServer(function(input, output) {
      output$view <- DT::renderDataTable(
        DT[Sepal.Width<=input$width,.SD],extensions = c( 'FixedHeader','Buttons'),
          options=list(pageLength=60,fixedHeader = TRUE,dom = 'Bfrtip',buttons = c( 'csv', 'excel'  )))
    }) 

    ui<-shinyUI(fluidPage(
      titlePanel("Shiny MWE"),
         sidebarLayout(
            sidebarPanel(
            sliderInput("width", label = h3("Min width"),
                        min=min(DT$Sepal.Width), max=max(DT$Sepal.Width), value=mean(DT$Sepal.Width),
                        )),

        mainPanel(
          DT::dataTableOutput("view")
        )
      )
    ))

    runApp(list(ui=ui,server=server))

【问题讨论】:

  • 您可以使用 r 库之一(例如 xlsx)创建下载按钮并编写 xlsx

标签: r shiny dt


【解决方案1】:

我也意识到我不得不放弃“按钮”扩展,还有其他原因。例如,excel 下载按钮仅导出应用程序上的视图,而不是整个数据集。 (可以通过选项 server=FALSE 修复,对于较大的数据集来说太慢了)

我选择了 openxlsx 包,它需要安装 Rtools,但我遇到了一些困难(找到了将其添加到 windows 路径的解决方案 ([Error: zipping up workbook failed when trying to write.xlsx)

所以我发布的代码大部分都是我想要的,或者我可以继续使用 openxlsx 命令。 xlsx 包或其他包有替代品,我在安装时也遇到了麻烦。

rm(list=ls())   
    library(shiny)
    library(datasets)
    library(DT)
    library(data.table)
    library(openxlsx)

    DT<-data.table(iris)
    # Style created for openxlsx see help
    hs <- createStyle(textDecoration = "BOLD", fontColour = "#FFFFFF", fontSize=12,
    fgFill = "#177B57",border="Bottom",borderStyle=c("thick"))
    #Server
    server<-shinyServer(function(input, output) {
      output$view <- DT::renderDataTable(
        DT[Sepal.Width<=input$width,.SD],extensions = c( 'FixedHeader'),
          options=list(pageLength=20,fixedHeader = TRUE,dom = 'frtip'))
    #Include DownloadHandler
      output$downloadData <- downloadHandler(
    filename = function() { paste0("test.xlsx") }, 
    content = function(file) {
        wb<-createWorkbook() # Create wb in R
        addWorksheet(wb,sheetName="Output") #create sheet
        #Creates a Data Table in Excel if you want, otherwhise only use write Data
        writeDataTable(wb,1, DT[Sepal.Width<=input$width,.SD], colNames = TRUE, headerStyle = hs,startRow=2,tableStyle = "TableStyleLight1")
        mergeCells(wb,sheet = "Output", cols=1:5, rows=1)
        writeData(wb,1, "Include text also based on reactive function and in merged cells" )
        saveWorkbook(wb, file = file, overwrite = TRUE)
        },
       contentType= "excel/xlsx")
       }) 

    ui<-shinyUI(fluidPage(
      titlePanel("Shiny MWE"),
         sidebarLayout(
            sidebarPanel(
            sliderInput("width", label = h3("Min width"),
                        min=min(DT$Sepal.Width), max=max(DT$Sepal.Width), value=mean(DT$Sepal.Width),
                        ),
                downloadButton('downloadData', 'Download')),

        mainPanel(
          DT::dataTableOutput("view")
        )
      )
    ))

    runApp(list(ui=ui,server=server),launch.browser=T) # Download button only works in browser

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2019-11-23
    • 2019-06-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多