1

I am sorry if this too simple... I need to zip a number of generated pdfs for uses to download. I tried to use Zip function, but failed with error:

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

Below is my code and any suggestions are welcomed (based on 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.")
    )
  )
))

Server 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)
       }
  )
})
Community
  • 1
  • 1
TTT
  • 4,354
  • 13
  • 73
  • 123
  • Is this on a windows machine? – jdharrison Nov 21 '14 at 18:35
  • @jdharrison Yes, and I have 7 zip installed. Also I will deploy this app to `www.shinyapps.io`. – TTT Nov 21 '14 at 18:36
  • Is 7 zip in your PATH status 127 probably indicates the zip executable couldnt be found. – jdharrison Nov 21 '14 at 18:39
  • @jdharrison: Thanks for the suggestion and I added `C:\Program Files\7-Zip` to my PATH but still got the same error... – TTT Nov 21 '14 at 18:41
  • Maybe use RTools and the zip contained in there. http://cran.r-project.org/bin/windows/Rtools/installer.html and put for example c:\Rtools\bin in your PATH. – jdharrison Nov 21 '14 at 18:42
  • I just re-installed RTools 3.1 and add `C:\Rtools\bin` to my PATH, still the same error... – TTT Nov 21 '14 at 18:49
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/65394/discussion-between-jdharrison-and-tao-hong). – jdharrison Nov 21 '14 at 18:49

2 Answers2

3

In get_a_pdf_plot you have ommitted the .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()
}

In your downloadHandler your need to prompt shiny on the download type:

 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"
  )
jdharrison
  • 30,085
  • 4
  • 77
  • 89
0

This gist helped me setup the export. It runs out of the box on a Mac. Windows required downloading Rtools and pointing to the zip in Rtools (from this question). I've not had problems yet.

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

The ?zip documentation mentions "On Windows, the default relies on a zip program (for example that from Rtools". If you point to a zip executable for your favorite zip program I'm sure it'll work similarly (if you don't want to download Rtools).

Community
  • 1
  • 1
Mitch
  • 652
  • 1
  • 8
  • 14