-
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.
Merge pull request #9 from JanMarvin/gh_issue_5
Magic conversion function. closes #5
- Loading branch information
Showing
7 changed files
with
245 additions
and
6 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 |
---|---|---|
@@ -1,34 +1,37 @@ | ||
Package: openxlsx2Extras | ||
Title: Extra Functions for the openxlsx2 package | ||
Version: 0.0.0.9000 | ||
Authors@R: | ||
Authors@R: c( | ||
person("Eli", "Pousson", , "[email protected]", role = c("aut", "cre", "cph"), | ||
comment = c(ORCID = "0000-0001-8280-1706")) | ||
comment = c(ORCID = "0000-0001-8280-1706")), | ||
person("Jan Marvin", "Garbuszus", , "[email protected]", role = "ctb") | ||
) | ||
Description: Extends the openxlsx2 package with wrapper and helper | ||
functions designed to add new features and options when working with | ||
Excel workbooks. | ||
License: MIT + file LICENSE | ||
URL: https://github.com/elipousson/openxlsx2Extras, | ||
https://elipousson.github.io/openxlsx2Extras/ | ||
BugReports: https://github.com/elipousson/openxlsx2Extras/issues | ||
Imports: | ||
Imports: | ||
cli, | ||
fs, | ||
lifecycle, | ||
openxlsx2, | ||
purrr, | ||
rlang, | ||
utils, | ||
vctrs | ||
Suggests: | ||
Suggests: | ||
dplyr, | ||
gt, | ||
marquee, | ||
sf, | ||
testthat (>= 3.0.0), | ||
tidyselect, | ||
withr | ||
Config/testthat/edition: 3 | ||
Encoding: UTF-8 | ||
Language: en-US | ||
Roxygen: list(markdown = TRUE) | ||
RoxygenNote: 7.3.2 | ||
Config/testthat/edition: 3 |
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
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,113 @@ | ||
#' Convert CSV to XLSX files or XLSX to CSV | ||
#' | ||
#' @description A set of functions to convert between CSV and XLSX formats using | ||
#' flexible input and output options. | ||
#' | ||
#' @details | ||
#' These functions allow seamless conversion between CSV and XLSX formats: | ||
#' | ||
#' - `csv_to_wb`: Reads one or more CSV files and writes them to a workbook | ||
#' object. | ||
#' - `csv_to_xlsx`: Converts one or more CSV files to a XLSX file. | ||
#' - `xlsx_to_csv`: Converts an XLSX file to a CSV file. | ||
#' | ||
#' @name convert_to | ||
#' @author Jordan Mark Barbone \email{[email protected]} | ||
#' @author Jan Marvin Garbuszus \email{[email protected]} | ||
#' @author Eli Pousson \email{[email protected]} | ||
#' | ||
#' @importFrom utils read.csv write.csv | ||
#' @importFrom openxlsx2 write_xlsx wb_to_df | ||
#' | ||
#' @examples | ||
#' # Create example CSV file | ||
#' csv <- tempfile(fileext = ".csv") | ||
#' utils::write.csv(x = mtcars, file = csv) | ||
#' | ||
#' # Convert CSV to Workbook | ||
#' wb <- csv_to_wb(csv = csv) | ||
#' | ||
#' # Convert CSV to XLSX | ||
#' xlsx <- openxlsx2::temp_xlsx() | ||
#' csv_to_xlsx(csv = csv, xlsx = xlsx) | ||
#' | ||
#' # Convert XLSX back to CSV | ||
#' xlsx_to_csv(x = xlsx, csv = csv) | ||
NULL | ||
|
||
#' @rdname convert_to | ||
#' @param file Path or paths to input files. For [csv_to_wb()], users | ||
#' can pass multiple CSV files when creating a workbook or xlsx file. If these | ||
#' inputs are named, the names are used as worksheet names. | ||
#' @param new_file Path to output file. Optional. If `new_file` is not supplied | ||
#' and `file` is a string, `new_file` is set to use the same path with a new | ||
#' file extension or (if file is not a string) `new_file` is set to a temporary | ||
#' file. | ||
#' @param .f Function used to read or write the csv file. Defaults to | ||
#' `utils::read.csv` for [csv_to_wb()] and [csv_to_xlsx()] and | ||
#' `utils::write.csv` for [xlsx_to_csv()]. Other functions are allowed but must | ||
#' use the input or output file name as the second argument. | ||
#' @param ... additional arguments passed to [openxlsx2::write_xlsx()] | ||
#' @export | ||
csv_to_wb <- function(file, | ||
new_file = NULL, | ||
.f = utils::read.csv, | ||
...) { | ||
x <- lapply(file, .f) | ||
openxlsx2::write_xlsx(x = x, file = set_new_file(file, new_file), ...) | ||
} | ||
|
||
#' @rdname convert_to | ||
#' @export | ||
csv_to_xlsx <- function(file, | ||
new_file = NULL, | ||
.f = utils::read.csv, | ||
...) { | ||
csv_to_wb( | ||
file, | ||
.f = .f, | ||
... | ||
)$save( | ||
file = set_new_file(file, new_file, ext = "xlsx") | ||
) | ||
} | ||
|
||
#' @rdname convert_to | ||
#' @param sheet A sheet in the workbook specified by `file` (either an index or | ||
#' a sheet name). Defaults to 1. | ||
#' @param ext File extension for output file. Defaults to "csv". | ||
#' @param ... Additional arguments passed to `.f` | ||
#' @export | ||
xlsx_to_csv <- function(file, | ||
new_file = NULL, | ||
sheet = 1, | ||
.f = utils::write.csv, | ||
ext = "csv", | ||
...) { | ||
.f( | ||
openxlsx2::wb_to_df( | ||
file = file, | ||
sheet = sheet | ||
), | ||
set_new_file(file, new_file, ext = ext), | ||
... | ||
) | ||
} | ||
|
||
#' [set_new_file()] sets new_file based on file or as temporary file. | ||
#' @noRd | ||
#' @importFrom fs path_ext_set file_temp | ||
set_new_file <- function(file = NULL, | ||
new_file = NULL, | ||
tmp_dir = tempdir(), | ||
ext = "xlsx") { | ||
if (!is.null(new_file)) { | ||
return(new_file) | ||
} | ||
|
||
if (is.character(file)) { | ||
return(fs::path_ext_set(file, ext)) | ||
} | ||
|
||
fs::file_temp(tmp_dir = tmp_dir, ext = ext) | ||
} |
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.
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,31 @@ | ||
test_that("magic conversion functions work", { | ||
## temporary files | ||
csv <- tempfile(fileext = ".csv") | ||
xlsx <- openxlsx2::temp_xlsx() | ||
|
||
## create example csv | ||
utils::write.csv(x = mtcars, file = csv, row.names = FALSE) | ||
|
||
## works even with other packages | ||
wb <- csv_to_wb(csv) | ||
expect_equal(openxlsx2::wb_to_df(wb), mtcars, ignore_attr = TRUE) | ||
|
||
csv_to_xlsx(csv, xlsx) | ||
expect_equal(openxlsx2::wb_to_df(xlsx), mtcars, ignore_attr = TRUE) | ||
|
||
csv_to_xlsx(list(foo = csv), xlsx) | ||
expect_equal( | ||
openxlsx2::wb_get_sheet_names(openxlsx2::wb_load(xlsx)), | ||
c(foo = "foo") | ||
) | ||
|
||
## use other arguments frequently passed to openxlsx2::write_xlsx() | ||
csv_to_xlsx(csv, xlsx, as_table = TRUE) | ||
|
||
expect_equal(openxlsx2::wb_load(xlsx)$get_tables()$tab_name, "Table1") | ||
|
||
## go the other way around from spreadsheet to csv | ||
xlsx_to_csv(xlsx, csv, row.names = FALSE) | ||
|
||
expect_equal(read.csv(csv), mtcars, ignore_attr = TRUE) | ||
}) |