0
install.packages("readxl")

library("readxl")

data <- read_excel("spreadsheet.xlsx")

sheets <- excel_sheets("spreadsheet.xlsx")

sheetList <- as.list(sheets) #convert sheets to list

for (s in sheetList){
  read_excel("spreadsheet.xlsx",sheet=s)
} #want to do such that when looping through each sheet, it would read in the data and be assigned its own 'variable'(dataframe)
Rubén
  • 34,714
  • 9
  • 70
  • 166
E. Huang
  • 11
  • 3
  • Do you want to read all the worksheets in one workbook or do you want to read all the sheets located in different workbooks? – Henry Cyranka Nov 24 '18 at 20:19
  • all worksheet in one workbook – E. Huang Nov 24 '18 at 20:37
  • Possible duplicate of [Read all worksheets in an Excel workbook into an R list with data.frames](https://stackoverflow.com/questions/12945687/read-all-worksheets-in-an-excel-workbook-into-an-r-list-with-data-frames) – phiver Nov 25 '18 at 13:06

1 Answers1

1

I wouldn't use a for loop. Easier to loop through the lists with lapply. Here is how I would do it.

library(readxl)
sheets_to_read <- excel_sheets("spreadsheet.xlsx") ##Obtain the sheets in the workbook


list_with_sheets <- lapply(sheets_to_read,
                           function(i)read_excel("spreadsheet.xlsx", sheet = i))

names(list_with_sheets) <- sheets_to_read ##Add names

list2env(mylist ,.GlobalEnv) ##This function should put them all in the global environment
Henry Cyranka
  • 2,970
  • 1
  • 16
  • 21