Home > Net >  Reading all sheets in multiple excel files into R
Reading all sheets in multiple excel files into R

Time:02-01

I am trying to read a bunch of excel files, and all of the sheets from these files into R. I would like to then save each sheet as a separate data frame with the name of the data frame the same name as the name of the sheet. Some files only have 1 sheet, while others have more than one sheet so I'm not sure how to specify all sheets as opposed to just a number. I have tried:

     library(XLConnect)   
     files.list <- list.files(recursive=T,pattern='*.xlsx')  #get files list from folder

     for (i in 1:length(files.list)){                                           
     wb <- loadWorkbook(files.list[i])           
     sheet <- getSheets(wb, sheet = )                      

     for (j in 1:length(sheet)){ 
         tmp<-read.xlsx(files.list[i], sheetIndex=j,
               sheetName=NULL,
               as.data.frame=TRUE, header=F)   
    if (i==1&j==1) dataset<-tmp else dataset<-rbind(dataset,tmp)   

      }
    }

and I get an error "could not find function "loadWorkbook"". At one point I resolved that issue and got an error "could not find function "getSheets"". I have had some issues getting this package to work so if anyone has a different alternative I would appreciate it!

CodePudding user response:

I'm pretty sure, the loadWorkbook function comes from package openxlsx. So you should use:

library(openxlsx)

https://cran.r-project.org/web/packages/openxlsx/openxlsx.pdf

CodePudding user response:

You could try with readxl...

I've not tested this for the case of different workbooks with duplicate worksheet names.

There were a number of issues with your code:

  1. the list.files pattern included a . which is a reserved character so needs to be escaped with the \\
  2. As @deschen pointed out the excel referring functions are from the openxlsx package
library(readxl)

files.list <- list.files(recursive = T, pattern = '*\\.xlsx')  #get files list from folder

for (i in seq_along(files.list)){
  
  sheet_nm <- excel_sheets(files.list[i])

    for (j in seq_along(sheet_nm)){
  
      assign(x = sheet_nm[j], value = read_xlsx(path = files.list[i], sheet = sheet_nm[j]), envir = .GlobalEnv)
  }

}

Created on 2022-01-31 by the reprex package (v2.0.1)

  •  Tags:  
  • Related