2

Readxl provides a good solution to iterate over multiple sheets and name those sheets, which works great.

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

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

However I need to pass additional arguments to read_excel..such as below being the desired output.

read_excel("datasets.xlsx",col_names = FALSE, skip = 1)

Solutions I tried

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

and

read_excel_func <- function(y){
 readxl::read_excel(y, col_names = FALSE, skip = 1)
}

path <- readxl_example("datasets.xlsx")
path %>% 
 excel_sheets() %>% 
 set_names() %>% 
 map(read_excel_func, path = path)
S3AN556
  • 79
  • 1
  • 1
  • 9
  • This isn't actually a `readxl` issue, but an issue of how to pass arguments in `map` functions. You can use formula notation around the function. In the last block, you're passing `path = path` to a function that doesn't take an argument named `path` – camille Apr 19 '19 at 04:09

1 Answers1

4

We can do this using the anonymous function call with ~

library(readxl)
library(dplyr)
path %>% 
 excel_sheets() %>% 
   set_names() %>% 
   map(~ read_excel(.x, col_names = FALSE, skip = 1, path = path))

Or simply pass the arguments in map as path

path %>% 
  excel_sheets() %>% 
  set_names() %>% 
  map(read_excel, col_names = FALSE, skip = 1, path = path)
akrun
  • 874,273
  • 37
  • 540
  • 662