I essentially wrote this short function for importing all sheets from an excel file to dataframes in a list, and adding names to the list items:
read_full_excel <- function(x){
# Imports the name of the sheets in the excel file and creates a list of data frames:
sheet_names <- readxl::excel_sheets(x)
# Create list and add each excel sheet as a data frame by a loop:
sheets <- list()
for(i in 1:length(sheet_names)){
sheets[[i]] <- readxl::read_xlsx(x, sheet = sheet_names[i])
}
# Add the sheet names to the list:
names(sheets) <- sheet_names
return(sheets)
}
Now, the function successfully creates a list of data frames from the excel sheets, but for some reason the names are not applied. I've run names(list_name) <- excel_sheets("file.xlsx")
outside the function, after the list was created, and for some reason this works. So, why won't it work inside the function?