2014-11-21 3 views
1

너무 간단하다면 유감스럽게 생각합니다 ... 다운로드 용으로 생성 된 PDF 파일을 압축해야합니다. 나는 Zip 기능을 사용하려고했으나 오류로 인해 실패 : 아래반짝이 R 우편 번호 다운로드를위한 복수 PDF 파일

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

+0

@jdharrison 예, 저는 7 개의 zip이 설치되어 있으며,이 응용 프로그램은 www.shinyapps.io '. –

+0

당신의 PATH 상태에서 7의 zip은 아마도 zip 실행 파일을 찾을 수 없음을 나타냅니다. – jdharrison

답변

2

get_a_pdf_plot 당신은 ommi 다운로드 유형에 반짝 프롬프트로 downloadHandler 당신의 필요에서 .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() 
} 

을 tted :

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

0

gist helped me setup the export합니다. 그것은 Mac에서 실행됩니다. Windows에서 Rtools를 다운로드하고 Rtools에서 zip을 가리켜 야했습니다 (from this question). 나는 아직 문제가 없었다.

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

?zip 문서는 언급 '당신이 좋아하는 압축 프로그램의 압축 실행 파일을 가리 키십시오. 나는 그것을 확신한다 "윈도우에서 기본은 Rtools에서 것을 (예를 들어 우편 프로그램에 의존" 비슷하게 작동합니다 (Rtools을 다운로드하고 싶지 않은 경우)

관련 문제