下载R中的拉链PNG
我正在编写一个R Shinny应用程序(我想在Shinny服务器上托管),它将从API下载PNG,在屏幕上显示它们,然后允许用户将它们作为.zip文件下载。我一辈子都搞不懂如何拉上PNG的拉链。我已经设法让应用程序在本地运行时提示PNG下载。代码如下。谢谢你的帮助 ui.R: 服务器.R:下载R中的拉链PNG,r,shiny,R,Shiny,我正在编写一个R Shinny应用程序(我想在Shinny服务器上托管),它将从API下载PNG,在屏幕上显示它们,然后允许用户将它们作为.zip文件下载。我一辈子都搞不懂如何拉上PNG的拉链。我已经设法让应用程序在本地运行时提示PNG下载。代码如下。谢谢你的帮助 ui.R: 服务器.R: library(shiny) library(httr) library(jsonlite) shinyServer(function(input, output, session) { imgFile
library(shiny)
library(httr)
library(jsonlite)
shinyServer(function(input, output, session) {
imgFiles <- reactiveValues(mat=NULL)
output$images <- renderText({
dURL = input$URL
dId = gsub("\\?.*","",gsub('.*/', '', dURL))
dJson = paste0('[api Location]',dID,'/export/json')
tech = GET(dJSON)
data = fromJSON(rawToChar(tech$content))
nImage = data$image
nName = data$name
withProgress(message = 'Making plot', value = 0, {
# Number of times we'll go through the loop
n <- length(nImage)
count<-0
images1<-NULL
images2<-NULL
destFile<-NULL
for (x in nImage){
destFile = paste0(nName[(count+1)],'.png')
images = c('<img src="',x,'" width=10% height=10%>',images)
Sys.sleep(.69)
count = count+1
incProgress(1/n, detail = paste("Doing part", x))
imgFiles$html <- image1
imgFiles$imgs <- nImage
imgFiles$names<- nName
}
})
print(imgFiles$html)
})
output$imgs<-renderText({
as.vector(imgFiles$imgs)
})
})
库(闪亮)
图书馆(httr)
图书馆(jsonlite)
shinyServer(功能(输入、输出、会话){
imgFiles我找到了一种方法,通过使用tar
而不是zip
来让它工作。我将继续使用它,但这似乎在闪亮的服务器上起作用
output$imgs <- downloadHandler(
filename = "DownloadPNGs.tar",
content = function(file){
tmpdir <- tempdir()
setwd(tempdir())
count<-0
for (i in imgFiles$imgs){
path <-paste0(imgFiles$names[count+1],".png")
download.file(i,path,mode="wb")
Sys.sleep(.69)
count=count+1
}
tar(tarfile = file,files=tmpdir)
}
)
output$imgs这可能会对您有所帮助。@MritiAgarwal我知道downloadHandler最终是解决这个问题的答案。我已经厌倦了设计一个允许我压缩()的内容函数PNG放在一起,似乎什么都不起作用。这在本地起作用,但会提示保存每个单独的文件。一旦文件在服务器上,它只会提示最后一个文件。
output$imgs <- downloadHandler(
filename = "DownloadPNGs.tar",
content = function(file){
tmpdir <- tempdir()
setwd(tempdir())
count<-0
for (i in imgFiles$imgs){
path <-paste0(imgFiles$names[count+1],".png")
download.file(i,path,mode="wb")
Sys.sleep(.69)
count=count+1
}
tar(tarfile = file,files=tmpdir)
}
)