3

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:

  • [1] "/var/folders/00/1dk1r000h01000cxqpysvccm005p87/T//Rtmps3T6Ua"
  • Warning in write.csv(datasetInput()$rock, file = "rock.csv", sep = ",") : attempt to set 'sep' ignored
  • Warning in write.csv(datasetInput()$pressure, file = "pressure.csv", sep = ",") : attempt to set 'sep' ignored
  • Warning in write.csv(datasetInput()$cars, file = "cars.csv", sep = ",") : attempt to set 'sep' ignored
  • [1] "rock.csv" "pressure.csv" "cars.csv"
    • adding: rock.csv (deflated 54%)
    • adding: pressure.csv (deflated 42%)
    • adding: cars.csv (deflated 57%)
  • Error opening file: 2
  • 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)
    
ed0reddie
  • 143
  • 2
  • 13

2 Answers2

3

Solution: Include if(file.exists(paste0(fname, ".zip"))) {file.rename(paste0(fname, ".zip"), fname)} after zip() call.

Agaz Wani
  • 5,514
  • 8
  • 42
  • 62
ed0reddie
  • 143
  • 2
  • 13
1

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.

sph21
  • 300
  • 1
  • 3
  • 11