0

There is this piece of code in which basically from UI page in which I want to select the file names for through checkbox and after selecting those, then clicking on download button selected files will get downloaded. I am stuck at UI i am unable to get those checkboxes on UI. its showing the output as

[object] [Object]

the code is below -

ui <- fluidPage(
verbatimTextOutput("links_list")
)

server <- function(input, output, session) {
get.files <- reactive({
list.files("/Users/harshmeetsingh/Downloads/")
})  

obsList <- list()

output$links_list <- renderUI({    
lapply(as.list(1:length(get.files())), function(i)
{
  btName <- get.files()[i]
print(btName)
# creates an observer only if it doesn't already exists
 if (is.null(obsList[[btName]])) {
 obsList[[btName]] <<- btName 
 }
fluidRow(checkboxInput(btName, get.files()[i])  )
 })
 })
output$downloadzip<-downloadHandler(
filename = function(){
  paste0("Extract.zip")
},
content = function(file){
  files <- NULL;
  for (i in 1:length(obsList)){
    if(input[[obsList[[i]]]])
      files <- c(paste("output_file/",obsList[[i]],sep=""),files)
  }
  #create the zip file
  zip(file,files)
},
contentType = "application/zip"
)

 tempText <- eventReactive({input$TempTest},{ 
l<-c()
for (i in 1:length(obsList)){
  
  if(input[[obsList[[i]]]])
    l<-c(l,paste("output_file/",obsList[[i]],sep=""))
}

return(paste(l) )
},
ignoreInit = TRUE)

output$Temp <-  renderPrint({ tempText()}) 



}

shinyApp(ui=ui,server=server)
  • This is probably fairly straightforward, but would be much easier to do with a [minimal reproducible example](https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example) (e.g. not with your home directory hard coded). – heds1 Jun 30 '21 at 05:29

1 Answers1

0

We can use checkboxGroupInput() to select all the files. input$files_chosen will be a list with all the filenames selected.

Notice that this app is showing the files in the home directory. This can be modified changing the path supplied in setwd().

app:

library(shiny)

#to use relative paths inside zip function
setwd('~')

ui <- fluidPage(
  downloadButton('downloadzip'),
  uiOutput("links_list")
)

server <- function(input, output, session) {
  get.files <- reactive({
    list.files()
  })  
  
  output$links_list <- renderUI({checkboxGroupInput(inputId = 'files_chosen',
                                                    label = 'Choose Files',
                                                    choices = get.files())
  })
  
  output$downloadzip <- downloadHandler(
    filename = function(){
      "Extract.zip"
    },
    content = function(file){
      #create the paths to look for the files.
      files <- input$files_chosen 
      #create the zip file
      zip(zipfile = file, files = files)
    },
    contentType = "application/zip"
  )
}

shinyApp(ui=ui,server=server)
jpdugo17
  • 6,816
  • 2
  • 11
  • 23
  • This one doesn't work for CSV files, can you please check it for CSV files ? – harshmeet chandhok Jul 01 '21 at 02:26
  • @harshmeetchandhok i used the app to compress some .csv files and it works. What error are you getting? – jpdugo17 Jul 01 '21 at 02:46
  • I provided my working directory and then selected csvs then getting this error while downloading in r studio zip warning: name not matched: table_from_page_12.csv zip warning: name not matched: table_from_page_124.csv zip error: Nothing to do! (try: zip -r9X /var/folders/dq/j0g3xb252n7_c5pq4gvm8dqc0000gp/T//RtmpoGsqJq/filecea64a8b9a1.zip . -i table_from_page_12.csv table_from_page_124.csv) – harshmeet chandhok Jul 01 '21 at 04:46
  • You can set the working directory with `setwd()` to be able to use relative paths. The error you are getting is because the file can't be found. I edited the answer. – jpdugo17 Jul 01 '21 at 05:07