S3AN556
S3AN556

Reputation: 79

readxl Iterate over multiple worksheets in a workbook while passing additional arguments

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)

Upvotes: 2

Views: 105

Answers (1)

akrun
akrun

Reputation: 887851

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)

Upvotes: 4

Related Questions