闪亮服务器,openxlsx图表无法插入

3

当使用runApp()从控制台运行以下闪亮的应用程序时,它按预期工作,显示绘图并提供下载包含绘图的Excel文件的功能。在shiny-server上运行相同的应用程序会产生以下错误:

无法打开文件'Rplots.pdf'

  • Shiny Server v1.4.2.786
  • Node.js v0.10.40
  • R version 3.3.1 (2016-06-21) -- "Bug in Your Hair"
  • Ubuntu 14.04.4 LTS

    library(shiny)
    library(openxlsx)
    library(magrittr)
    
    # Define UI for application that draws a histogram
    ui <- shinyUI(fluidPage(
    
       # Application title
       titlePanel("Old Faithful Geyser Data"),
    
       # Sidebar with a slider input for number of bins 
       sidebarLayout(
          sidebarPanel(
             sliderInput("bins",
                         "Number of bins:",
                         min = 1,
                         max = 50,
                         value = 30),
             downloadButton('specDataDownload',
                            label = "Download",
                            class = NULL)
          ),
    
          # Show a plot of the generated distribution
          mainPanel(
             plotOutput("distPlot")
          )
       )
    ))
    
    # Define server logic required to draw a histogram
    server <- shinyServer(function(input, output) {
    
       output$distPlot <- renderPlot({
          # generate bins based on input$bins from ui.R
          x    <- isolate({faithful[, 2] })
          bins <- seq(min(x), max(x), length.out = input$bins + 1)
    
          # draw the histogram with the specified number of bins
          hist(x, breaks = bins, col = 'darkgray', border = 'white')
    
       })
       output$specDataDownload <- downloadHandler(
           filename = function() {
               paste("ProcessedPlateAssay",
                     gsub("-|[[:space:]]|:",
                          "",
                          Sys.time()),
                     ".xlsx",
                     sep = "_")
           },
           content = function(con) {
               x    <- isolate({faithful[, 2] })
               bins <- seq(min(x), max(x), length.out = input$bins + 1)
               output <- createWorkbook()
               addWorksheet(
                   output,
                   "One")
               hist(
                   x,
                   breaks = bins,
                   col = 'darkgray',
                   border = 'white')
               insertPlot(
                   output,
                   sheet = 1,
                   startRow = (1),
                   startCol = 5,
                   width = 6.5,
                   height = 3,
                   fileType = "png",
                   units = "in",
                   dpi = 600)
    
           saveWorkbook(
               wb = output,
               file = con
           )
           })
    })
    
    # Run the application 
    shinyApp(ui = ui, server = server)
    

遇到了相同的问题。以下是我们解决的方法:(1) 使用jpeg打开一个(临时)图像;(2) 执行您的绘图命令,(3) 关闭jpeg,使用insertImage。 - undefined
嗨,我最终也解决了那个问题,但根本的问题是我的服务器上的Shiny用户没有对文件夹有写入权限(它是作为一个Git仓库创建并推送到服务器的)。 - undefined
1个回答

5
我想提供一个最简单的例子,因为我一直在努力解决openxlsxinsertPlot函数在shiny-server内无法正常工作的问题,可能是由于tempfiledev.copy权限问题所致。对我来说最有效的方法是使用ggsave,因为它非常好地处理了分辨率,并且还有一个比例参数,可以调整图像元素的大小,使其更适合演示。我还包括了使用png函数的第二种方法。
下面是一个可重现的例子,部分取自@sebkopf在Save plots made in a shiny app中的答案。
library(shiny)
library(openxlsx)
library(ggplot2)

ui <- fluidPage(
  downloadLink("downloadExcel", "Excel Graph Download")
)

server <- function(input, output) {

  p1 <- qplot(mpg, data=mtcars, geom="density", fill=as.factor(gear), 
alpha=I(.5), main="Distribution of Gas Mileage")
  p2 <- qplot(age, circumference, data = Orange, geom = c("point", "line"), 
colour = Tree)

  output$downloadExcel <- downloadHandler(
    filename = "savePlot.xlsx",
    content = function(file){
      wb <- createWorkbook()

      addWorksheet(wb, "ggsave", gridLines = F)
      addWorksheet(wb, "png", gridLines = F)

      # Method 1: using ggsave

      ggsave("p1.png", plot = p1, scale = .6) # Scale parameter resizes the object making text more legible
      ggsave("p2.png", plot = p2, scale = .6)

      insertImage(wb, "ggsave", "p1.png", width = 5, height = 3.5)
      insertImage(wb, "ggsave", "p22.png", startCol = "J", width = 16, height = 10, units = "cm")

      # Method 2: using png function

      png("p11.png")
      print(p1)
      dev.off()

      insertImage(wb, "png", "p11.png", width = 5, height = 3.5)

      png("p22.png")
      print(p2)
      dev.off()

      insertImage(wb, "png", "p22.png", startCol = "J", width = 16, height = 10, units = "cm")

      saveWorkbook(wb, file, overwrite = T)

      unlink(c("p1", "p2", "p11", "p22")) # To remove the images from the server
    }
  )
}

shinyApp(ui, server)

1
请记住,检查一下 shiny 是否是你尝试写入的目录的所有者:https://groups.google.com/forum/#!topic/shiny-discuss/srWETT6uL-I,使用 sudo chown shiny:shiny /var/shiny-server/www/shiny_test/work - undefined
我需要一个tempdir()来使它工作。例如,在这里:https://stackoverflow.com/questions/47983222/putting-images-into-xlsx-docs-through-shiny-on-shiny-server -- 我想这可以代替chown来完成。 - undefined

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接