【问题标题】:Shiny R Zip multiple PDFS for downloadShiny R Zip 压缩多个 PDF 文件以供下载
【发布时间】:2015-01-19 23:44:40
【问题描述】:

如果这太简单了,我很抱歉...我需要压缩一些生成的 pdf 文件以供下载。我尝试使用Zip 函数,但失败并出现错误:

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)
       }
  )
})

【问题讨论】:

  • 这是在 Windows 机器上吗?
  • @jdharrison 是的,我安装了 7 个 zip。另外我会将这个应用部署到www.shinyapps.io
  • 是 7 zip 在您的 PATH 状态 127 可能表示找不到 zip 可执行文件。
  • @jdharrison:感谢您的建议,我将 C:\Program Files\7-Zip 添加到我的 PATH 中,但仍然遇到相同的错误...
  • 也许使用 RTools 和其中包含的 zip。 cran.r-project.org/bin/windows/Rtools/installer.html 并将例如 c:\Rtools\bin 放入您的 PATH 中。

标签: r shiny shiny-server


【解决方案1】:

get_a_pdf_plot 中您省略了.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"
  )

【讨论】:

  • 您可能需要在适当的浏览器中运行示例,而不是 rstudio 弹出窗口。
【解决方案2】:

这个gist helped me setup the export。它在 Mac 上开箱即用。 Windows 需要下载 Rtools 并指向 Rtools (from this question) 中的 zip。我还没有遇到问题。

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

?zip 文档提到“在 Windows 上,默认依赖于 zip 程序(例如来自 Rtools 的程序”。如果您指向您最喜欢的 zip 程序的 zip 可执行文件,我相信它会以类似方式工作(如果您不想下载 Rtools)。

【讨论】:

    猜你喜欢
    • 2022-06-15
    • 2011-01-28
    • 1970-01-01
    • 1970-01-01
    • 2020-09-15
    • 2019-06-08
    • 1970-01-01
    • 2011-02-21
    • 1970-01-01
    相关资源
    最近更新 更多