2014-11-21 52 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)