Home » excel » excel – r batch reading xls files from specific sheet

excel – r batch reading xls files from specific sheet

Posted by: admin May 14, 2020 Leave a comment

Questions:

I have around 30 excel files in my folder.

I am interested in reading them all.

I used this code below

library(readxl)
file.list <- list.files(pattern='*.xlsx')
df.list <- lapply(file.list, read_excel)

The problem is each excel file has multiple sheets and I am interested in the contents of only one sheet , sheetName = “Piano”, not interested in the contents of other sheets.

So how can I ensure that in addition to reading all the 30 excel files r reads only data from sheetName=”Piano” from all these excel files. Thanks.

How to&Answers:

We can make use of the sheet argument of read_excel. According to ?read_excel

sheet – Sheet to read. Either a string (the name of a sheet), or an integer (the position of the sheet). Ignored if the sheet is specified via range. If neither argument specifies the sheet, defaults to the first sheet.

library(dplyr)
library(readxl)
df.list <- map(file.list, read_excel, sheet = 'Piano')