I am looping over read_xlsx (from readxl library) to read multiple sheets in an excel. However, if there's an error that the sheet (specified with a certain name - "missing" in below example) is not present, I want it to be ignored and the loop should continue. But at the same time if there's any other error (say related to calculations), then that should not be ignored and thrown as error, while stopping the loop. How to do this?
Note: I used tryCatch / try; they both ignore all types of errors (learning from this answer).
sheets=c("name1","name2","missing","name3")
for (k in sheets) {
tryCatch({temp=read_xlsx("someexcel.xlsx",sheet = k,col_names = F)
some_calc=mean(temp[1,])},error=function(e){})
}