通过Shiny on shiny-server将图像放入xlsx文档中

问题描述 投票:1回答:1

我相信我在我的笔记本电脑上通过RStudio运行的闪亮应用程序和ubuntu上的闪亮服务器之间遇到了许可问题。

例如,此示例应用程序将在xlsx文档中编写图像,并允许您下载xlsx。它将在rstudio本地运行闪亮但不通过闪亮服务器。我猜测有一种方法可以暂时以安全的方式编写png并将其调回来写入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)
    })
})

长子。 [R

library(shiny)

shinyUI(fluidPage(
  sidebarLayout(
    sidebarPanel(
      helpText(),
      downloadButton('downloadReport')),
    mainPanel()
  ))
)
r ggplot2 shiny xlsx shiny-server
1个回答
1
投票

从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目录,该目录仅适用于我的本地开发笔记本电脑。

© www.soinside.com 2019 - 2024. All rights reserved.