Can someone please point out how I can make this download zip function work in server.R? When I run this, I get the following error:
Error reading: 9
library(shiny)
# server.R
server <- function(input, output) {
datasetInput <- reactive({
return(list(rock=rock, pressure=pressure, cars=cars))
})
output$downloadData <- downloadHandler(
filename = 'pdfs.zip',
content = function(fname) {
tmpdir <- tempdir()
setwd(tempdir())
print(tempdir())
fs <- c("rock.csv", "pressure.csv", "cars.csv")
write.csv(datasetInput()$rock, file = "rock.csv", sep =",")
write.csv(datasetInput()$pressure, file = "pressure.csv", sep =",")
write.csv(datasetInput()$cars, file = "cars.csv", sep =",")
print (fs)
zip(zipfile=fname, files=fs)
},
contentType = "application/zip"
)
}
# ui.R
ui <- shinyUI(fluidPage(
titlePanel('Downloading Data'),
sidebarLayout(
sidebarPanel(
downloadButton('downloadData', 'Download')
),
mainPanel()
)
)
)
shinyApp(ui = ui, server = server)
Solution: Include if(file.exists(paste0(fname, ".zip"))) {file.rename(paste0(fname, ".zip"), fname)} after zip() call.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With