通过闪亮服务器上的闪亮将图像放入 xlsx 文档

Putting images into xlsx docs through Shiny on shiny-server

我相信我 运行 遇到了通过笔记本电脑上的 RStudio 和 ubuntu 上的 shiny-server 工作的闪亮应用程序 运行 之间的权限问题。

例如,此示例应用程序将在 xlsx 文档中写入图像并让您下载 xlsx。它可以在 rstudio 运行 本地运行,但不能通过 shiny-server 运行。我猜想有一种方法可以暂时以安全的方式写入 png,然后将其回调以写入带有 shiny-server 的 kosher xlsx。

server.R

library(shiny);library(openxlsx);library(ggplot2)

shinyServer(function(input, output) {

  output$downloadReport <- downloadHandler(
    filename = "test.xlsx",
    content = function(file){
      wb <- createWorkbook(paste0(Sys.time(), ".xlsx"))
      my_plot <- ggplot(mtcars) + geom_line(aes(x = cyl, y = gear))
      worksheet_name <- "ggplot"

      addWorksheet(wb, worksheet_name)
      png("plot.png", width=1024, height=768, units="px", res=144)
      print(my_plot)
      dev.off()  
      insertImage(wb, worksheet_name, "plot.png", width=11.18, height=7.82, units="in")

      saveWorkbook(wb, file, overwrite = TRUE)
    })
})

ui.R

library(shiny)

shinyUI(fluidPage(
  sidebarLayout(
    sidebarPanel(
      helpText(),
      downloadButton('downloadReport')),
    mainPanel()
  ))
)

根据 ralf-stubner 的提示,我更改了

png("plot.png", width=1024, height=768, units="px", res=144)

png(paste0(tempdir(), "/", "plot.png"), width=1024, height=768, units="px", res=144)

insertImage(wb, worksheet_name, "plot.png", width=11.18, height=7.82, units="in")

insertImage(wb, worksheet_name, paste0(tempdir(), "/", "plot.png"), width=11.18, height=7.82, units="in")

现在图像被写入具有正确权限的临时目录,而不是 app 目录,它只适用于我的本地开发笔记本电脑。