如果这太简单了,我很抱歉...我需要压缩一些生成的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)
}
)
})
答案 0 :(得分:2)
在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"
)
答案 1 :(得分:0)
这gist helped me setup the export。它在Mac上开箱即用。 Windows需要下载Rtools并指向Rtools中的zip(from this question)。我还没有遇到任何问题。
Sys.setenv(R_CMDZIP = 'C:/Rtools/bin/zip')
?zip
文档提及&#34;在Windows上,默认依赖于zip程序(例如来自Rtools&#34;。如果你指向你最喜欢的zip程序的zip可执行文件我&#39; ;我相信它的工作方式类似(如果你不想下载Rtools)。