4

I have a .xlsx file has two sheets and I want to generate a list of both excel sheets using read_excel from readxl package . I have used this code

my_work <- lapply(excel_sheets("data.xlsx"), 
                      read_excel, 
                      path = "data.xlsx")

The read_excel() function is called multiple times on the "data.xlsx" file and each sheet is loaded in one after the other. The result is a list of data frames, each data frame representing one of the sheets in data.xlsx. My question is , why should I write the path argument in lapplyfunction , since the file is already in the working directory ?

Sam Firke
  • 21,571
  • 9
  • 87
  • 105
Bahgat Nassour
  • 157
  • 4
  • 14
  • 4
    Because it is required by both functions `excel_sheets` and `read_excel`, no? –  Jan 08 '16 at 04:58
  • 3
    `read_excel` would otherwise have no idea where to look, since you only pass sheet names to `lapply`. – jbaums Jan 08 '16 at 05:08

2 Answers2

0

The documentation:

read_excel(path, sheet = 1, col_names = TRUE, col_types = NULL, na = "", skip = 0)

The parameter path is a required argument. So you need to fill it in. Otherwise, an error will pop up.

ah bon
  • 9,293
  • 12
  • 65
  • 148
0

Not sure this is the easiest way but you can create a short function modifying the read_excel() function to take in both a sheet name parameter and path, then lapply over that function.

library(readxl)
path <- "data.xlsx"
sheet_names <- excel_sheets(path)

# create function
read_excel_sheet <- function(sheet_name, path) {
  x <- read_excel(path = path, sheet = sheet_name)
}

my_work <- lapply(sheet_names, read_excel_sheet, path = path)

Greg B
  • 86
  • 4