I am trying to create a function that reads in all sheets in an excel workbook using readxl::read_excel
and binds them into a single data frame, and allows me to pass through additional arguments to read_excel
. I can do the first part fine, but not the second part.
library(magrittr)
# example excel workbook with multiple sheets
path <- readxl::readxl_example("datasets.xlsx")
# function with simple forwarding
read_all <- function(path, ...) {
path %>%
readxl::excel_sheets() %>%
rlang::set_names() %>%
purrr::map_df(~ readxl::read_excel(path = path, sheet = .x, ...))
}
# errors with and without additional arguments
read_all(path)
read_all(path, skip = 5)
I should get back a single file, instead I get an error:
Error: Can't guess format of this cell reference: iris
In addition: Warning message: Cell reference follows neither the A1 nor R1C1 format. Example: iris NAs generated.
Without argument passing the function works fine:
# Function works without passing extra params
read_all_0 <- function(path) {
path %>%
readxl::excel_sheets() %>%
rlang::set_names() %>%
purrr::map_df(~ readxl::read_excel(path = path, sheet = .x))
}
read_all_0(path)
Argument passing works fine in a simple function without purrr::map_df
read_test <- function(path, ...) {
path %>% readxl::read_excel(...)
}
read_test(path, skip = 10)