可以结合 openxlsx-Workbook 和来自 rvg 的 xl_add_vg - 导出可编辑图形
Possibility to combine openxlsx-Workbook and xl_add_vg from rvg - Export editable graphics
我想从闪亮的应用程序中导出一些表格和 ggplot 图形到 excel 文件中。到目前为止,我一直在使用 openxlsx,添加了许多 sheet 和内容。现在我想将 ggplot 作为可编辑矢量图形导出到 Excel。这在 rvg/officer-packages 中是可行的。
有没有可能结合这个?我想在由 openxlsx 创建的具有更多内容的 excel 文件中添加带有此可编辑图形的 sheet。到现在我只能在两个单独的文件中分别独立实现它。
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)
编辑它(添加另一个 sheet)读回它,最后将它提供给 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)
我想从闪亮的应用程序中导出一些表格和 ggplot 图形到 excel 文件中。到目前为止,我一直在使用 openxlsx,添加了许多 sheet 和内容。现在我想将 ggplot 作为可编辑矢量图形导出到 Excel。这在 rvg/officer-packages 中是可行的。 有没有可能结合这个?我想在由 openxlsx 创建的具有更多内容的 excel 文件中添加带有此可编辑图形的 sheet。到现在我只能在两个单独的文件中分别独立实现它。
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)
编辑它(添加另一个 sheet)读回它,最后将它提供给 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)