Skip to content

Commit

Permalink
Merge pull request #9 from JanMarvin/gh_issue_5
Browse files Browse the repository at this point in the history
Magic conversion function. closes #5
  • Loading branch information
elipousson authored Jan 10, 2025
2 parents 14f6ba6 + 8b0fa43 commit ce02365
Show file tree
Hide file tree
Showing 7 changed files with 245 additions and 6 deletions.
13 changes: 8 additions & 5 deletions DESCRIPTION
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
8 changes: 8 additions & 0 deletions NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
export(as_sheet_list)
export(as_sheet_names)
export(as_wb)
export(csv_to_wb)
export(csv_to_xlsx)
export(fmt_lgl_cols)
export(fmt_marquee_txt)
export(map_wb)
Expand All @@ -24,10 +26,13 @@ export(wb_sheets_fmt)
export(wb_split)
export(wb_to_df_list)
export(write_xlsx_ext)
export(xlsx_to_csv)
import(rlang)
importFrom(cli,cli_abort)
importFrom(cli,cli_warn)
importFrom(fs,file_exists)
importFrom(fs,file_temp)
importFrom(fs,path_ext_set)
importFrom(lifecycle,badge)
importFrom(lifecycle,deprecated)
importFrom(openxlsx2,current_sheet)
Expand All @@ -36,6 +41,9 @@ importFrom(openxlsx2,wb_add_numfmt)
importFrom(openxlsx2,wb_data)
importFrom(openxlsx2,wb_set_col_widths)
importFrom(openxlsx2,wb_to_df)
importFrom(openxlsx2,write_xlsx)
importFrom(purrr,list_cbind)
importFrom(purrr,map_chr)
importFrom(utils,read.csv)
importFrom(utils,write.csv)
importFrom(vctrs,vec_recycle)
113 changes: 113 additions & 0 deletions R/convert_to.R
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)
}
79 changes: 79 additions & 0 deletions man/convert_to.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 5 additions & 0 deletions man/openxlsx2Extras-package.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion man/wb_set_col_widths_ext.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

31 changes: 31 additions & 0 deletions tests/testthat/test-convert_to.R
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)
})

0 comments on commit ce02365

Please sign in to comment.