R/loadWorkbook.R

Defines functions loadWorkbook

Documented in loadWorkbook

#' Load Workbook
#'
#' Loads an Excel workbook using readxl and returns a lst of all the sheets.
#'
#' @param workbookPath Path and file name of the workbook.
#' @param guessVarTypes Try to guess variable types.
#' @param na Character vector of strings to interpret as missing data.
#'
#' @return A lst of tibbles containing the sheets of the workbook.
#' @export
#'
#' @examples
#' data <- loadWorkbook('data.xlsx')
loadWorkbook <- function(workbookPath, guessVarTypes = TRUE,
                         na = c('', 'NA', 'NULL')) {
  sheetNames <- readxl::excel_sheets(workbookPath)
  listOfSheets <- lapply(sheetNames, readxl::read_excel, path = workbookPath)
  returnLst <- tibble::lst(!!!listOfSheets)

  if (guessVarTypes) {
    guessing_function <- function(varNumber) {
      readr::type_convert(returnLst[[varNumber]], na = na)
    }
    returnLst <- lapply(1:length(returnLst), guessing_function)
  }

  names(returnLst) <- sheetNames

  return(returnLst)
}
JonasEngstrom/icudb documentation built on Dec. 18, 2021, 1:41 a.m.