从rvg组合openxlsx-Workbook和xl_add_vg的可能性-导出可编辑图形

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了从rvg组合openxlsx-Workbook和xl_add_vg的可能性-导出可编辑图形相关的知识,希望对你有一定的参考价值。

我想从excel文件中的闪亮应用程序导出一些表和ggplot图形。到目前为止,我使用openxlsx,添加了许多工作表和内容。现在,我想将ggplot作为可编辑的矢量图形导出到Excel。这对于rvg / people-packages是完全可行的。有可能将其结合起来吗?我想将带有此可编辑图形的工作表添加到由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)
答案

您可以使用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组合openxlsx-Workbook和xl_add_vg的可能性-导出可编辑图形的主要内容,如果未能解决你的问题,请参考以下文章

优化后的组合算法

从语法语音语义方面谈组合关系与聚合关系,并举例

从列表组合生成 Python 字典

用递归函数计算从n个人中选择k个人组成一个委员会的不同组合数

从2个vigenere键中制作1个键

从具有重复元素的向量生成所有唯一组合