【问题标题】:Putting images into xlsx docs through Shiny on shiny-server通过闪亮服务器上的 Shiny 将图像放入 xlsx 文档
【发布时间】:2018-06-07 13:40:40
【问题描述】:

我相信我在通过笔记本电脑上的 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)
    })
})

ui.R

library(shiny)

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

【问题讨论】:

  • 您是否收到任何错误消息?您可以使用 tempdir() 来获取存储临时图像的保存位置。
  • 谢谢。 tempdir 是我解决此问题的方法。

标签: r ggplot2 shiny xlsx shiny-server


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

【讨论】:

    猜你喜欢
    • 2021-04-15
    • 2015-10-21
    • 2018-01-06
    • 1970-01-01
    • 1970-01-01
    • 2014-05-06
    • 1970-01-01
    • 2014-01-01
    • 2019-08-23
    相关资源
    最近更新 更多