Search code examples
rskip

Skip list errors with R


I have one 'for', which tries to load multiple files, however, when any of these files are open, it will get the following error:

Error: Evaluation error: zip file 'V:/Planejamento/2021/Programação/04. Abr/Balun/~$Schedule Balun 0804.xlsx' cannot be opened.

Is there any way that searches and recordings on the list can continue to ignore only those that are open?

Code:

for(i in i:length(arquivo_caracter)){
  master[[i]] <- data.frame(read_excel(arquivo_caracter[i], sheet = "Master", skip = 0, .name_repair = "minimal"))
  dados[[i]] <- data.frame(read_excel(arquivo_caracter[i], sheet = "Dados",skip = 1, .name_repair = "minimal"))
}

Solution

  • As suggested in comments, with tryCatch:

    arquivo_caracter <- list("test1.xls","test2.xls")
    
    for(i in 1:length(arquivo_caracter)){
      tryCatch({
       master[[i]] <- data.frame(read_excel(arquivo_caracter[i], sheet = "Master", skip = 0, .name_repair = "minimal"))
       dados[[i]] <- data.frame(read_excel(arquivo_caracter[i], sheet = "Dados",skip = 1, .name_repair = "minimal"))},
      error = function(e) {warning(paste("Couldn't open",arquivo_caracter[i]))}
      )
    }
    
    #Warning messages:
    #1: In value[[3L]](cond) : Couldn't open test1.xls
    #2: In value[[3L]](cond) : Couldn't open test2.xls