Shinny R Zip多个PDF供下载
我很抱歉,如果这太简单了。。。我需要压缩一些生成的PDF文件以供下载使用。我尝试使用Shinny R Zip多个PDF供下载,r,shiny,shiny-server,R,Shiny,Shiny Server,我很抱歉,如果这太简单了。。。我需要压缩一些生成的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 以下是我的代码,欢迎提出任何建议(基于):
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
以下是我的代码,欢迎提出任何建议(基于):
用户界面
库(闪亮)
shinyUI(fluidPage)(
单件(标签$head(HTML)(
'
$(文档).ready(函数(){
//在启动时禁用下载。data_文件是downloadButton的id
$(“#数据#文件”).attr(“禁用”、“真”).attr(“onclick”、“return false;”);
addCustomMessageHandler(“下载准备就绪”,函数(消息){
$(“#数据文件”).removeAttr(“禁用”).removeAttr(“onclick”).html(
“下载”+message.fileSize+”);
});
})
'
))),
选项卡面板(
tabPanel('数据下载示例',
操作按钮(“启动程序”,h5(“点击开始处理数据”),
hr(),
下载按钮(“数据文件”),
helpText(“处理完成后即可下载。”)
)
)
))
服务器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)
}
)
})
库(闪亮)
获取\u a\u pdf\u绘图0){
系统睡眠(2)
会话$sendCustomMessage(“下载准备就绪”,列表(fileSize=“就绪”))
}
})
在get\u pdf\u plot
中输出$data\u file,您已经输入了.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()
}
这个。它在Mac电脑上用完了。Windows需要下载Rtools并指向Rtools()中的zip。我还没有遇到问题
Sys.setenv(R\u CMDZIP='C:/Rtools/bin/zip')
?zip
文档提到“在Windows上,默认依赖于zip程序(例如来自Rtools的程序)。如果您指向您最喜欢的zip程序的zip可执行文件,我相信它也会工作类似(如果您不想下载Rtools).这是在windows计算机上吗?@jdharison:是的,我已经安装了7个zip。我还将把这个应用部署到www.shinyapps.io
。7个zip在你的路径中吗?状态127可能表示无法找到zip可执行文件。@jdharison:谢谢你的建议,我在我的路径中添加了C:\Program Files\7-zip
,但仍然得到了相同的结果错误…可能使用RTools和其中包含的zip。并将示例c:\RTools\bin放在路径中。您可能需要在适当的浏览器中运行该示例,而不是在rstudio弹出窗口中运行。
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()
}
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"
)