I'm importing a specific sheet from several hundred .xlsx-files into a dataframe in R. Unfortunately, there are a few files where that sheet is missing. Therefore, I'm getting "Error: Sheet 'XXX' not found".
Is there a neat way to handle this error by just skipping those files? Preferably without a loop.
This is what I've done so far, but like I said - without error handling:
library(tidyverse)
library(readxl)
path <- "here is my file path"
files <- list.files(path, pattern="partial_filename*.xlsx", recursive=TRUE)
read_xlsx_files <- function(x){
df <- read_xlsx(path = paste(path, x, sep="/"), sheet="XXX")
}
df <- lapply(files, read_xlsx_files) %>%
bind_rows()
You could add a try/catch in your 'read_xlsx_files'.
read_xlsx_files <- function(x){
tryCatch(
{ df <- read_xlsx(path = paste(path, x, sep="/"), sheet="XXX")},
error = function(e) {
print("Skipping")
df <- data.frame()
}
)
}