闪亮的服务器,openxlsx图不能插入

时间:2016-07-05 22:12:38

标签: r shiny shiny-server

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

  

无法打开文件'Rplots.pdf'

  • Shiny Server 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)
    

1 个答案:

答案 0 :(得分:1)

我想提供一个最小的例子,因为我一直在为openxlsx的{​​{1}}函数无法在Shiny-server内部工作而苦恼,这可能是由于insertPlottempfile。最适合我的方法是使用dev.copy,因为它可以非常巧妙地处理分辨率,并且还具有scale参数,该参数可以调整图像元素的大小,使其更适合演示。我还包括了使用ggsave函数的第二种方法。

下面是一个可重现的示例,部分来自@sebkopf在Save plots made in a shiny app中的答案

png