-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
adds functions for reading excel sheets and writing them as csv into …
…a destination folder
- Loading branch information
Showing
4 changed files
with
122 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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")), | ||
...) | ||
} | ||
}) | ||
) | ||
|
||
} |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.