2014-11-21 55 views
1

如果這太簡單了,我很抱歉...我需要壓縮一些生成的pdf用於下載。我試圖用Zip功能,但失敗,錯誤:Shiny R Zip多個PDFS下載

Warning: running command '"zip" -r9X "pdfs.zip" "plot_1.pdf" "plot_2.pdf" "plot_3.pdf" "plot_4.pdf" "plot_5.pdf" ' had status 127 
Error opening file: 2 
Error reading: 6 

下面是我的代碼和任何建議都歡迎(基於shiny app : disable downloadbutton):

UI.R

library(shiny) 

shinyUI(fluidPage(
    singleton(tags$head(HTML(
' 
    <script type="text/javascript"> 
    $(document).ready(function() { 
     // disable download at startup. data_file is the id of the downloadButton 
     $("#data_file").attr("disabled", "true").attr("onclick", "return false;"); 

     Shiny.addCustomMessageHandler("download_ready", function(message) { 
     $("#data_file").removeAttr("disabled").removeAttr("onclick").html(
      "<i class=\\"fa fa-download\\"></i>Download " + message.fileSize + " "); 
     }); 
    }) 
    </script> 
' 
))), 
    tabsetPanel(
    tabPanel('Data download example', 
     actionButton("start_proc", h5("Click to start processing data")), 
     hr(), 

     downloadButton("data_file"), 
     helpText("Download will be available once the processing is completed.") 
    ) 
) 
)) 

服務器UI .R

library(shiny) 

get_a_pdf_plot <- function(my_i){ 
     pdf(paste("plot_", my_i, sep="")) 
     plot(1:my_i*5, 1:my_i*5, 
      xlim = c(1, my_i*5), 
      ylim = c(1, my_i*5), 
      main = paste("1:", my_i, sep = "")) 
     dev.off() 
} 


shinyServer(function(input, output, session) { 

    observe({ 
    if (input$start_proc > 0) { 
     Sys.sleep(2) 
     session$sendCustomMessage("download_ready", list(fileSize= "Ready")) 
    } 
    }) 

    output$data_file <- downloadHandler(
     filename = 'pdfs.zip', 
     content = function(fname) { 
     fs <- c() 
     tmpdir <- tempdir() 
     setwd(tempdir()) 
     print (tempdir()) 

     for (i in c(1,2,3,4,5)) { 
      path <- paste("plot_", i, ".pdf", sep="") 
      fs <- c(fs, path) 
      get_a_pdf_plot(i) 
     } 
     print (fs) 
     zip(zipfile="pdfs.zip", files=fs) 
     } 
) 
}) 
+0

這是在一臺Windows機器上嗎? – jdharrison 2014-11-21 18:35:29

+0

@jdharrison是的,我已經安裝了7個zip文件,我也將這個應用程序部署到'www.shinyapps.io '。 – 2014-11-21 18:36:35

+0

在您的PATH狀態下是7個郵編127可能表示無法找到郵件可執行文件 – jdharrison 2014-11-21 18:39:13

回答

2

get_a_pdf_plot你有ommi tted的.pdf

get_a_pdf_plot <- function(my_i){ 
    pdf(paste("plot_", my_i,".pdf", sep="")) 
    plot(1:my_i*5, 1:my_i*5, 
     xlim = c(1, my_i*5), 
     ylim = c(1, my_i*5), 
     main = paste("1:", my_i, sep = "")) 
    dev.off() 
} 

在你downloadHandler你需要提示閃亮的下載類型:

output$data_file <- downloadHandler(
    filename = 'pdfs.zip', 
    content = function(fname) { 
     fs <- c() 
     tmpdir <- tempdir() 
     setwd(tempdir()) 
     print (tempdir()) 

     for (i in c(1,2,3,4,5)) { 
     path <- paste("plot_", i, ".pdf", sep="") 
     fs <- c(fs, path) 
     get_a_pdf_plot(i) 
     } 
     print (fs) 
     zip(zipfile="pdfs.zip", files=fs) 
    }, 
    contentType = "application/zip" 
) 
+0

您可能需要在適當的瀏覽器中運行該示例,而不是彈出rstudio。 – jdharrison 2014-11-21 19:12:34

0

gist helped me setup the export。它在Mac上用完了。 Windows需要下載Rtools並指向Rtools中的zip(from this question)。我還沒有問題。

Sys.setenv(R_CMDZIP = 'C:/Rtools/bin/zip')

?zip文檔中提到「在Windows中,默認情況下依賴於壓縮程序(例如,從Rtools。」如果你指向一個zip可執行文件爲您喜歡的壓縮程序我敢肯定,這是我會同樣工作(如果你不想下載Rtools)