Shiny-server,openxlsx 绘图无法插入

bwb*_*nfo 3 r shiny shiny-server

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

无法打开文件“Rplots.pdf”

  • 闪亮服务器 v1.4.2.786
  • Node.js v0.10.40
  • R 版本 3.3.1 (2016-06-21) -- “你头发里的虫子”
  • 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)
    
    Run Code Online (Sandbox Code Playgroud)

Jos*_*tho 5

我想提供一个最小的例子,因为我一直在努力解决openxlsxsinsertPlot函数在闪亮服务器内无法工作的问题,可能是由于tempfile或 的权限问题dev.copy。对我来说最有效的方法是使用ggsave,因为它可以非常巧妙地处理分辨率,并且还有一个比例参数,可以调整图像元素的大小,使其更适合演示。我还提供了使用该函数的第二种方法png

下面是一个可重现的示例,部分取自@sebkopf 在“保存在闪亮应用程序中制作的绘图”中的答案

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)
Run Code Online (Sandbox Code Playgroud)