能否将openxlsx-Workbook和rvg中的xl_add_vg结合起来,导出可编辑的图形?

4

我希望能够从一个闪亮的应用程序中导出一些表格和ggplot图形到一个excel文件中。目前我使用的是openxlsx,添加了许多工作表和内容。现在我想将ggplot作为可编辑的矢量图形导出到Excel中。这可以很好地使用rvg/officer包实现。 有没有可能将它们结合起来?我想把含有这个可编辑图形的工作表添加到由openxlsx创建的具有更多内容的Excel文件中。到目前为止,我只能分别在两个单独的文件中实现这一点。

    library(shiny)
    library(ggplot2)
    library(openxlsx)
    library(officer)
    library(rvg)


    ui <- fluidPage(
      plotOutput("plot", height = "350px"),
      downloadButton('Export', label = 'Export as png in xlsx'),
      downloadButton('Export2', label = 'Export as editable xlsx')
    )
    server <- function(input, output) {
      # some plot
      plot <- ggplot(mpg, aes(x = class, fill = drv)) + 
        geom_bar(position = "stack")
      output$plot <- renderPlot({
          print(plot)
      })
      # Export plot as png in xlsx
      output$Export <- downloadHandler(
        filename = function() {
            paste0("someNicePlot", ".xlsx")
        },
        content = function(file) {
          wb <- createWorkbook()
          addWorksheet(wb, "someNicePlot", gridLines = F)
          ggsave("plot.png", plot = plot, width = 5, height = 5)
          insertImage(wb, "someNicePlot", "plot.png", width = 5, height = 5)
          # add some more worksheets and content
          saveWorkbook(wb, file)
        }
      ) 
      # Export plot as editable graphic in xlsx
      output$Export2 <- downloadHandler(
        filename = function() {
          paste0("someNicePlot_editable", ".xlsx")
        },
        content = function(file) {
          wb <- read_xlsx()
          # unfortunately the first sheet is named in french (from template)
          wb <- xl_add_vg(wb, sheet = "Feuil1",
                          code = print(plot), width = 5, height = 5, left = 1, top = 2 )
          print(wb, target = file)
        }
      ) 
    }

    shinyApp(ui, server)
1个回答

4

您可以使用library(openxlsx)创建一个带有内容的临时xlsx文件,然后使用library(officer)读取它并进行编辑(添加另一个工作表),最后将其提供给downloadHandler

library(shiny)
library(ggplot2)
library(openxlsx)
library(officer)
library(rvg)

ui <- fluidPage(
  plotOutput("plot", height = "350px"),
  downloadButton('Export', label = 'Export as png in xlsx')
)

server <- function(input, output) {
  # some plot
  plot <- ggplot(mpg, aes(x = class, fill = drv)) + 
    geom_bar(position = "stack")
  output$plot <- renderPlot({
    print(plot)
  })
  # Export plot as png in xlsx
  output$Export <- downloadHandler(
    filename = function() {
      paste0("someNiceCombination", ".xlsx")
    },
    content = function(file) {

      openxlsxwb <- createWorkbook()
      addWorksheet(openxlsxwb, "someNicePlot", gridLines = F)
      ggsave("plot.png", plot = plot, width = 5, height = 5)
      insertImage(openxlsxwb, "someNicePlot", "plot.png", width = 5, height = 5)
      # add some more worksheets and content
      tmpwb <- tempfile(fileext = ".xlsx")
      saveWorkbook(openxlsxwb, tmpwb)

      # Export plot as editable graphic in xlsx
      officerwb <- read_xlsx(tmpwb)
      file.remove(tmpwb)
      officerwb <- add_sheet(officerwb, label = "someNicePlot_editable")
      officerwb <- xl_add_vg(officerwb, sheet = "someNicePlot_editable",
                             code = print(plot), width = 5, height = 5, left = 1, top = 2 )
      print(officerwb, target = file)
    }
  )
}

shinyApp(ui, server)

是否可以将通过rvg/officer包生成的可编辑图形内容添加到与library(openxlsx)中的内容相同的工作表中,而不是添加一个单独的工作表? - lrclark

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接