Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add function to read all sheets into a list #407

Closed
sncr-github opened this issue Nov 24, 2017 · 3 comments
Closed

Add function to read all sheets into a list #407

sncr-github opened this issue Nov 24, 2017 · 3 comments

Comments

@sncr-github
Copy link

Suggestion: add a function to read all available sheets into a list. Simple version below:

library(readxl)

read_all_excel<-function(file){
 sheets <- list()
 idx <- 1
 sheet_names <- excel_sheets(file)
 for (s in sheet_names){
  sheets[[idx]] <- read_excel(file, sheet = s)
  idx <- idx + 1
 }
 names(sheets) <- sheet_names
 return(sheets)
}

Pros:

  • A convenience for immediately reading in all sheets.

Cons:

  • Dataframes are the usual "top level object" in the Tidyverse.
  • Version as shown does not include additional function arguments available in excel_sheets. This is less confusing and suggests that individuals with special requirements per sheet implement their own.
@cderv
Copy link

cderv commented Nov 25, 2017

I find it pretty simple to combine readxl with purrr functions, as shown also in vignette workflows

path <- readxl_example("datasets.xlsx")
path %>% 
  excel_sheets() %>% 
  set_names() %>% 
  map(read_excel, path = path)

Using map_df, you can also get a data.frame if all your sheets are containing same data

path <- readxl_example("deaths.xlsx")
deaths <- path %>%
  excel_sheets() %>%
  set_names() %>% 
  map_df(~ read_excel(path = path, sheet = .x, range = "A5:F15"), .id = "sheet")

Did you know about purrr way?
I think relying on purrr functions for iterating through sheets offers great flexibility.

@jennybc
Copy link
Member

jennybc commented Nov 25, 2017

Adding a link to the vignette @cderv refers to:

http://readxl.tidyverse.org/articles/articles/readxl-workflows.html

I absolutely see the utility of this but am on the fence re: a built-in function for it. Which is why I wrote the vignette.

Obviously if this was typical use for most users, it's a no brainer to bring it into readxl. As a user, I have the occasional spreadsheet that I treat this way, but it's the exception rather than the rule. Worth discussing, though.

@jennybc
Copy link
Member

jennybc commented Apr 15, 2018

I still think this is a matter to handle via documentation (and this exists in our current documentation). It doesn't require too much code to do this in a base R or tidyverse style.

@jennybc jennybc closed this as completed Apr 15, 2018
@lock lock bot locked and limited conversation to collaborators Oct 10, 2019
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants