-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathread_excel_workbook.R
41 lines (33 loc) · 1.41 KB
/
read_excel_workbook.R
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
#' Import data from multiple sheets of an Excel workbook
#'
#' @description
#' `read_excel_workbook()` reads all the data from the sheets of an Excel workbook and return an appended dataframe.
#'
#' @inheritParams readxl::read_xlsx
#' @inheritParams dplyr::bind_rows
#' @return
#' A [tibble][tibble::tibble-package]. If there is any column type mismatch during data frames row binding, an error will occur. This is because R cannot combine columns of different types. For example, you cannot combine a column of integers with a column of characters.
#'
#' @seealso
#' [read_excel()], which reads a Sheet of an Excel file into a data frame, and [read_gsheets()], which imports data from multiple sheets in a Google Sheets.
#' @export
#'
#' @examples
#'
#' path <- system.file("extdata", "Diamonds.xlsx", package = "bulkreadr", mustWork = TRUE)
#'
#' read_excel_workbook(path = path, .id = "Year")
#'
#'
#' # Column types mismatch error --------------------------------------
#'# If the `read_excel_workbook()` function complains about a data type mismatch,
#'# then set the `col_types` argument to `"text"`.
#'# This will make all the column types in the resulting DataFrame be characters.
#'
read_excel_workbook <- function(path, col_types = NULL, .id = NULL) {
path <- check_file(path)
path %>%
readxl::excel_sheets() %>%
purrr::set_names() %>%
purrr::map_df(read_xlsx, path = path, col_types = col_types, .id = .id)
}