The readxl workflows page shows great examples of reading in all sheets from a workbook.
But what if I only want a selection of sheets?
In real life I have over 50. I already have a vector of sheet names I want to import.
I tried to adapt the example, as below, and failed miserably.
( I have tried a few things, this is one of the least worst)
I can hack my way round this, but was wondering how it could be done in more seamlessly in a piped manner, and more specifically, what to pass to the pipe / map part.
In real life, I will be extracting a specific range from each of the desired sheets, so the call to map will be more along the lines of
map_df(~ read_excel(path = path, sheet = .x, range = "A5:F15"), .id = "sheet")
The erroneous code I tried:
library(readxl)
library(tidyverse)
sheets_to_keep <- c("iris","mtcars")
path <- readxl_example("datasets.xlsx")
all_sheets <- path %>%
excel_sheets()
path %>%
excel_sheets() %>%
set_names() %>%
filter(.x %in% sheets_to_keep) %>%
map(read_excel)
#> Error in UseMethod("filter_"): no applicable method for 'filter_' applied to an object of class "character"
Created on 2020-05-18 by the reprex package (v0.3.0)