From 175b69f301ec10b50a664e7560bdc0dc1bf0a4ab Mon Sep 17 00:00:00 2001 From: Vaclav Hausenblas Date: Fri, 1 Feb 2019 16:18:59 +0100 Subject: [PATCH] adds functions for reading excel sheets and writing them as csv into a destination folder --- R/excel.R | 71 ++++++++++++++++++++++++++++++++++ man/excel_worksheets.Rd | 19 +++++++++ man/excel_worksheets_readxl.Rd | 14 +++++++ man/worksheets_to_csvs.Rd | 18 +++++++++ 4 files changed, 122 insertions(+) create mode 100644 R/excel.R create mode 100644 man/excel_worksheets.Rd create mode 100644 man/excel_worksheets_readxl.Rd create mode 100644 man/worksheets_to_csvs.Rd diff --git a/R/excel.R b/R/excel.R new file mode 100644 index 0000000..2c0b87e --- /dev/null +++ b/R/excel.R @@ -0,0 +1,71 @@ +#' Reads all sheets from an excel xlsx file into a list +#' +#' @param file file name +#' @param ... args passed to openxlsx::readWorkbook +#' +#' @return a list of data.frames +#' @export +excel_worksheets <- function(file, ...) { + + if (!requireNamespace("openxlsx")) stop("openxlsx package required!") + + wb <- openxlsx::loadWorkbook(file) + sh_names <- openxlsx::sheets(wb) + + sheets <- + lapply( + X = sh_names, + FUN = openxlsx::readWorkbook, + # arguments passed to readWorkbook + xlsxFile = wb, + ... + ) + names(sheets) <- sh_names + + return(sheets) +} + + +#' Reads all sheets from an excel xlsx file into a list +#' +#' @param file file name +#' +#' @export +excel_worksheets_readxl <- function(file) { + + if (!requireNamespace("readxl")) stop("readxl package required!") + sh_names <- readxl::excel_sheets(path = file) + + sheets <- lapply(sh_names, readxl::read_xlsx, path = file, col_types = "text") + names(sheets) <- sh_names + + return(sheets) +} + + +#' Writes list of data.frames as csv files into a folder +#' +#' @param sheets a list of data.frames +#' @param path path to a destination dir +#' @param ... args passed to fwrite +#' @export +worksheets_to_csvs <- function(sheets, path, ...) { + if (!requireNamespace("data.table")) stop("data.table package required!") + stopifnot(is.list(sheets)) + + sh_names <- names(sheets) + if (is.null(sh_names)) sh_names <- seq_along(sheets) + + invisible( + sapply(sh_names, + function(s) { + if (!is.null(sheets[[s]])) { + data.table::fwrite( + x = sheets[[s]], + file = file.path(path, paste0(s, ".csv")), + ...) + } + }) + ) + +} diff --git a/man/excel_worksheets.Rd b/man/excel_worksheets.Rd new file mode 100644 index 0000000..b8ac592 --- /dev/null +++ b/man/excel_worksheets.Rd @@ -0,0 +1,19 @@ +% Generated by roxygen2: do not edit by hand +% Please edit documentation in R/excel.R +\name{excel_worksheets} +\alias{excel_worksheets} +\title{Reads all sheets from an excel xlsx file into a list} +\usage{ +excel_worksheets(file, ...) +} +\arguments{ +\item{file}{file name} + +\item{...}{args passed to openxlsx::readWorkbook} +} +\value{ +a list of data.frames +} +\description{ +Reads all sheets from an excel xlsx file into a list +} diff --git a/man/excel_worksheets_readxl.Rd b/man/excel_worksheets_readxl.Rd new file mode 100644 index 0000000..d0130be --- /dev/null +++ b/man/excel_worksheets_readxl.Rd @@ -0,0 +1,14 @@ +% Generated by roxygen2: do not edit by hand +% Please edit documentation in R/excel.R +\name{excel_worksheets_readxl} +\alias{excel_worksheets_readxl} +\title{Reads all sheets from an excel xlsx file into a list} +\usage{ +excel_worksheets_readxl(file) +} +\arguments{ +\item{file}{file name} +} +\description{ +Reads all sheets from an excel xlsx file into a list +} diff --git a/man/worksheets_to_csvs.Rd b/man/worksheets_to_csvs.Rd new file mode 100644 index 0000000..ecf5d5a --- /dev/null +++ b/man/worksheets_to_csvs.Rd @@ -0,0 +1,18 @@ +% Generated by roxygen2: do not edit by hand +% Please edit documentation in R/excel.R +\name{worksheets_to_csvs} +\alias{worksheets_to_csvs} +\title{Writes list of data.frames as csv files into a folder} +\usage{ +worksheets_to_csvs(sheets, path, ...) +} +\arguments{ +\item{sheets}{a list of data.frames} + +\item{path}{path to a destination dir} + +\item{...}{args passed to fwrite} +} +\description{ +Writes list of data.frames as csv files into a folder +}