Skip to content

Commit

Permalink
Zap openxlsx and use writexl instead, fix #310
Browse files Browse the repository at this point in the history
* Introduce `lifecycle` (which is a dependency of `tibble` anyway) to
implement the deprecation of `import(readxl)`.

* Remove the overwriting of existing sheets by using `which`.

Ref #343

But `which` is not yet mapped to `col_names` for export(). #326
  • Loading branch information
chainsawriot committed Sep 11, 2023
1 parent 294ba02 commit 46f0f07
Show file tree
Hide file tree
Showing 8 changed files with 24 additions and 51 deletions.
7 changes: 4 additions & 3 deletions DESCRIPTION
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
Package: rio
Type: Package
Title: A Swiss-Army Knife for Data I/O
Version: 0.5.30
Version: 0.5.31
Authors@R: c(person("Jason", "Becker", role = "aut", email = "[email protected]"),
person("Chung-hong", "Chan", role = c("aut", "cre"), email = "[email protected]",
comment = c(ORCID = "0000-0002-6232-7530")),
Expand Down Expand Up @@ -49,9 +49,10 @@ Imports:
curl (>= 0.6),
data.table (>= 1.11.2),
readxl (>= 0.1.1),
openxlsx,
tibble,
stringi
stringi,
writexl,
lifecycle
Suggests:
datasets,
bit64,
Expand Down
2 changes: 1 addition & 1 deletion R/export.R
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
#' \item SPSS (.sav), using [haven::write_sav()]
#' \item SPSS compressed (.zsav), using [haven::write_sav()]
#' \item Stata (.dta), using [haven::write_dta()]. Note that variable/column names containing dots (.) are not allowed and will produce an error.
#' \item Excel (.xlsx), using [openxlsx::write.xlsx()]. Existing workbooks are overwritten unless `which` is specified, in which case only the specified sheet (if it exists) is overwritten. If the file exists but the `which` sheet does not, data are added as a new sheet to the existing workbook. `x` can also be a list of data frames; the list entry names are used as sheet names.
#' \item Excel (.xlsx), using [writexl::write_xlsx()]. `x` can also be a list of data frames; the list entry names are used as sheet names.
#' \item R syntax object (.R), using [base::dput()] (by default) or [base::dump()] (if `format = 'dump'`)
#' \item Saved R objects (.RData,.rda), using [base::save()]. In this case, `x` can be a data frame, a named list of objects, an R environment, or a character vector containing the names of objects if a corresponding `envir` argument is specified.
#' \item Serialized R objects (.rds), using [base::saveRDS()]. In this case, `x` can be any serializable R object.
Expand Down
30 changes: 2 additions & 28 deletions R/export_methods.R
Original file line number Diff line number Diff line change
Expand Up @@ -219,34 +219,8 @@ export_delim <- function(file, x, fwrite = TRUE, sep = "\t", row.names = FALSE,
}

#' @export
.export.rio_xlsx <- function(file, x, which, ...) {
dots <- list(...)
if (!missing(which)) {
if (file.exists(file)) {
wb <- openxlsx::loadWorkbook(file = file)
sheets <- openxlsx::getSheetNames(file = file)
if (is.numeric(which)) {
if (which <= length(sheets)) {
which <- sheets[which]
} else {
which <- paste("Sheet", length(sheets) + 1L)
}
}
if (!which %in% sheets) {
openxlsx::addWorksheet(wb, sheet = which)
} else {
openxlsx::removeWorksheet(wb, sheet = which)
openxlsx::addWorksheet(wb, sheet = which)
openxlsx::worksheetOrder(wb) <- sheets
}
openxlsx::writeData(wb, sheet = which, x = x)
openxlsx::saveWorkbook(wb, file = file, overwrite = TRUE)
} else {
openxlsx::write.xlsx(x = x, file = file, sheetName = which, ...)
}
} else {
openxlsx::write.xlsx(x = x, file = file, ...)
}
.export.rio_xlsx <- function(file, x, ...) {
writexl::write_xlsx(x = x, path = file, ...)
}

#' @export
Expand Down
2 changes: 1 addition & 1 deletion R/import.R
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@
#' \item SPSS compressed (.zsav), using [haven::read_sav()].
#' \item Stata (.dta), using [haven::read_dta()]. If `haven = FALSE`, [foreign::read.dta()] can be used.
#' \item SPSS Portable Files (.por), using [haven::read_por()].
#' \item Excel (.xls and .xlsx), using [readxl::read_excel()]. Use `which` to specify a sheet number. For .xlsx files, it is possible to set `readxl = FALSE`, so that [openxlsx::read.xlsx()] can be used instead of readxl (the default).
#' \item Excel (.xls and .xlsx), using [readxl::read_xlsx()] or [readxl::read_xls()]. Use `which` to specify a sheet number.
#' \item R syntax object (.R), using [base::dget()]
#' \item Saved R objects (.RData,.rda), using [base::load()] for single-object .Rdata files. Use `which` to specify an object name for multi-object .Rdata files. This can be any R object (not just a data frame).
#' \item Serialized R objects (.rds), using [base::readRDS()]. This can be any R object (not just a data frame).
Expand Down
24 changes: 9 additions & 15 deletions R/import_methods.R
Original file line number Diff line number Diff line change
Expand Up @@ -311,22 +311,16 @@ import_delim <-
}

#' @export
.import.rio_xlsx <- function(file, which = 1, readxl = TRUE, ...) {
if (isTRUE(readxl)) {
.check_pkg_availability("readxl")
arg_reconcile(readxl::read_xlsx,
path = file, ..., sheet = which,
.docall = TRUE,
.remap = c(colNames = "col_names", na.strings = "na")
)
} else {
.check_pkg_availability("openxlsx")
arg_reconcile(openxlsx::read.xlsx,
xlsxFile = file, ..., sheet = which,
.docall = TRUE,
.remap = c(col_names = "colNames", na = "na.strings")
)
.import.rio_xlsx <- function(file, which = 1, readxl = lifecycle::deprecated(), ...) {
if (lifecycle::is_present(readxl)) {
lifecycle::deprecate_warn(
when = "0.5.31",
what = "import(readxl)",
details = "xlsx will always be read by `readxl`. The parameter `readxl` will be dropped in v2.0.0.")
}
arg_reconcile(readxl::read_xlsx,
path = file, ..., sheet = which,
.docall = TRUE)
}

#' @export
Expand Down
2 changes: 1 addition & 1 deletion man/export.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/import.Rd

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

6 changes: 5 additions & 1 deletion tests/testthat/test_format_xls.R
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,11 @@ require("datasets")

test_that("Export to Excel (.xlsx)", {
expect_true(export(iris, "iris.xlsx") %in% dir())
expect_true(export(mtcars, "iris.xlsx", which = 2) %in% dir())
})

test_that("readxl is deprecated", {
lifecycle::expect_deprecated(import("iris.xlsx", readxl = TRUE))
lifecycle::expect_deprecated(import("iris.xlsx", readxl = FALSE))
})

test_that("Import from Excel (.xlsx)", {
Expand Down

0 comments on commit 46f0f07

Please sign in to comment.