Shiny: Download Zip Archive

Shiny: download zip archive

You are using <- inside the downloadHandler function and should be using =. Also you may need to define the contentType:

library(shiny)

runApp(
list(server = function(input, output) {
output$downloadData <- downloadHandler(
filename = function() {
paste("output", "zip", sep=".")
},
content = function(fname) {
fs <- c()
tmpdir <- tempdir()
setwd(tempdir())
for (i in c(1,2,3,4,5)) {
path <- paste0("sample_", i, ".csv")
fs <- c(fs, path)
write(i*2, path)
}
zip(zipfile=fname, files=fs)
},
contentType = "application/zip"
)
}
, ui = fluidPage(
titlePanel(""),
sidebarLayout(
sidebarPanel(
downloadButton("downloadData", label = "Download")
),
mainPanel(h6("Sample download", align = "center"))
)
))
)

Why can I not put files in directory and download in zip using Shiny?

When running this on Windows make sure zip works. See this related article and follow the procedure in section "Putting Rtools on the PATH".

The following works as intended:

library(ggplot2)
library(shiny)

data(iris)
write.csv(iris, "iris.csv")
print(getwd())

#UI
ui <- fluidPage(
sidebarLayout(
sidebarPanel(
fileInput("file1", "Choose CSV File", accept = ".csv"), # input button
downloadButton("dl", label = "Download zip!") #download button
),
mainPanel(plotOutput("plot")) # showing the plot
)
)

server <- function(input, output, session) {

# read input file
up_res <- reactive({
inFile <- input$file1
if (is.null(inFile)) {
return(NULL)
}
read.csv(inFile$datapath)
})

# generate plot
myPlot <- reactiveVal(ggplot())
output$plot <- renderPlot({
g <- ggplot(req(up_res()), aes(x = Sepal.Length, y = Petal.Length)) +
geom_dotplot(binaxis='y', stackdir='center')
myPlot(g)
return(g)
})

# supposed to create zip file containing png file of plot
output$dl <- downloadHandler(
filename = function() {
paste('iris-', Sys.Date(), '.zip', sep='')
},
content = function(comp) {
pngPath <- normalizePath(file.path(tempdir(), "iris.png"))
ggsave(pngPath, plot = myPlot(), device = "png")
zip(zipfile = comp, files = pngPath, extras = '-j')
}
)
}

shinyApp(ui = ui, server = server)

Download multiple csv files in a zipped folder in Shiny

The top solution still wasn't working for me. I was working in RStudio Server on a Linux server. The problem was that RStudio couldn't automatically locate the path to the zip executable. I had to manually specify it. In the command line, which zip revealed to me /usr/bin/zip.

So, I just had to set the R_ZIPCMD environment variable at the top of my code.

Sys.setenv(R_ZIPCMD="/usr/bin/zip")

Source: The help file for zip() mentions R_ZIPCMD.

how to download multiple plots as a zip file in R shiny?

Below is an reproducible example:

ui.R


shinyUI( fluidPage(

titlePanel( "Application Template"), br(),

sidebarLayout(

sidebarPanel(
sliderInput( "sample.size", "Number of Samples:", min = 50, max = 500, value = 50),
downloadButton( 'plot.download', 'Download Plots')
),

# Display a histogram of the generated distribution
mainPanel(
fluidRow(
column(6, plotOutput( "norm.plot")),
column(6, plotOutput( "unif.plot"))
)
)
)

))

server.R

shinyServer(function(input, output){

# Define a function that creates a histogram of random normal data
norm.plot <- function(){

# Generate data based on the slider input from the UI file
samp <- rnorm(input$sample.size)

# Plot a histogram of the random normal sample
ggplot( data.frame( samp = samp), aes( x = samp)) +
geom_histogram( bins = 30) +
labs( x = 'Random Normal Draws', y = 'Count')
}

# Return the normal histogram to the app
output$norm.plot <- renderPlot({
return( norm.plot())
})

# Define a function that creates a histogram of random uniform data
unif.plot <- function(){

# Generate data based on the slider input from the UI file
samp <- runif(input$sample.size)

# Plot a histogram of the random uniform sample
ggplot( data.frame(samp = samp), aes(x = samp)) +
geom_histogram( bins = 30) +
labs( x = 'Random Uniform Draws', y = 'Count')
}

# Return the uniform histogram to the app
output$unif.plot <- renderPlot({
return( unif.plot())
})

# Download the plots
output$plot.download = downloadHandler(
filename = 'plots.zip',
content = function( file){

# Set temporary working directory
owd <- setwd( tempdir())
on.exit( setwd( owd))

# Save the histograms (a loop can be used here for a bunch of plots)
ggsave( 'norm_hist.png', plot = norm.plot(), device = "png")
ggsave( 'unif_hist.png', plot = unif.plot(), device = "png")

# Zip them up
zip( file, c( 'norm_hist.png', 'unif_hist.png'))
}
)

})



Related Topics



Leave a reply



Submit