0

i have a loop function to unzip and read files inside into. But when the .zip file is empty, the function stop. What can i do for the function continue to next objects [j]?

Erro: 1: Document is empty Beside: Warning message: In utils::unzip(zipped.file, exdir = rnd.folder.name) : Erro: 1: Document is empty

    my.zip.file <- list.files()
    
    for (j in 1:length(my.zip.file)) {
    
    ### other codes before
    
    utils::unzip(my.zip.file[j], exdir = temp.dir, junkpaths = TRUE)
    
    ### other codes after
}
  • What is the length of `my.zip.file`? Are the files you want to list in your current working directory, because that is the default of the `list.files()` function. – Samuel Aug 08 '20 at 14:52
  • The length of 'my.zip.file' is 808, but can change any moment, because i need to download files every day. The directory is a folder that i have created to deposit all .zip files downloaded. Only have .zip in this folder. – Lucas Delmondes Aug 08 '20 at 15:08
  • [This](https://stackoverflow.com/questions/12193779/how-to-write-trycatch-in-r#:~:text=tryCatch%20returns%20the%20value%20associated,tryCatch%20) may help. – maydin Aug 08 '20 at 15:40

1 Answers1

1

You can use try to try and next to go to the next iteration if there's an error:

my.zip.file <- list.files()

for (j in 1:length(my.zip.file)) {
  
  ### other codes before
  
  attempt <- 
    try(utils::unzip(my.zip.file[j], exdir = temp.dir, junkpaths = TRUE))
  if(inherits(attempt, "try-error")) next
  
  ### other codes after
  
}
Stéphane Laurent
  • 75,186
  • 15
  • 119
  • 225