Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add openxlsx2.na.strings option #968

Merged
merged 4 commits into from Mar 14, 2024
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 2 additions & 0 deletions NEWS.md
Expand Up @@ -8,6 +8,8 @@

* `wb_dims()` now accepts `from_dims` to specify a starting cell [960](https://github.com/JanMarvin/openxlsx2/pull/960).

* You can now set `options(openxlsx2.na.strings)` to a value to have a default value for `na.strings` in `wb_add_data()`, `wb_add_data_table()`, and `write_xlsx()` [968](https://github.com/JanMarvin/openxlsx2/pull/968).

## Fixes

* Export `wb_add_ignore_error()`. [955](https://github.com/JanMarvin/openxlsx2/pull/955)
Expand Down
3 changes: 2 additions & 1 deletion R/class-workbook-wrappers.R
Expand Up @@ -112,7 +112,8 @@ wb_save <- function(wb, file = NULL, overwrite = TRUE, path = NULL) {
#' @param apply_cell_style Should we write cell styles to the workbook
#' @param remove_cell_style keep the cell style?
#' @param na.strings Value used for replacing `NA` values from `x`. Default
#' [na_strings()] uses the special `#N/A` value within the workbook.
#' looks if `options(openxlsx2.na.strings)` is set. Otherwise [na_strings()]
#' uses the special `#N/A` value within the workbook.
#' @param inline_strings write characters as inline strings
#' @param ... additional arguments
#' @export
Expand Down
4 changes: 3 additions & 1 deletion R/class-workbook.R
Expand Up @@ -6322,7 +6322,9 @@ wbWorkbook <- R6::R6Class(
sheet <- private$get_sheet_index(sheet)

if (!table %in% self$tables$tab_name) {
stop(sprintf("table '%s' does not exist.", table), call. = FALSE)
stop(sprintf("table '%s' does not exist.\n
Call `wb_get_tables()` to get existing table names", table),
call. = FALSE)
}

## delete table object (by flagging as deleted)
Expand Down
4 changes: 4 additions & 0 deletions R/openxlsx2-package.R
Expand Up @@ -158,6 +158,8 @@
#' * `options("openxlsx2.scientificFormat" = 48)`
#' * `options("openxlsx2.string_nums" = TRUE)` numerics in character columns
#' will be converted. `"1"` will be written as `1`
#' * `options("openxlsx2.na.strings" = "#N/A")` consulted by `write_xlsx()`,
#' `wb_add_data()` and `wb_add_data_table()`.
#' @name openxlsx2_options
NULL
# matches enum celltype
Expand Down Expand Up @@ -197,6 +199,8 @@ openxlsx2_celltype <- c(
#'
#' You should be able to modify
#' * [delete_data()] -> [wb_clean_sheet()]
#' * [write_data()] -> [wb_add_data()]
#' * [write_datatable()] -> [wb_add_data_table()]
#' * [write_comment()] -> [wb_add_comment()]
#' * [remove_comment()] -> [wb_remove_comment()]
#' * [write_formula()] -> [wb_add_formula()]
Expand Down
15 changes: 12 additions & 3 deletions R/write.R
Expand Up @@ -10,7 +10,8 @@
#' @param colNames has colNames (only in update_cell)
#' @param removeCellStyle remove the cell style (only in update_cell)
#' @param na.strings Value used for replacing `NA` values from `x`. Default
#' `na_strings()` uses the special `#N/A` value within the workbook.#' @keywords internal
#' `na_strings()` uses the special `#N/A` value within the workbook.
#' @keywords internal
#' @noRd
inner_update <- function(
wb,
Expand Down Expand Up @@ -176,7 +177,8 @@ update_cell <- function(x, wb, sheet, cell, colNames = FALSE,
#' @param applyCellStyle apply styles when writing on the sheet
#' @param removeCellStyle keep the cell style?
#' @param na.strings Value used for replacing `NA` values from `x`. Default
#' `na_strings()` uses the special `#N/A` value within the workbook.
#' looks if `options(openxlsx2.na.strings)` is set. Otherwise [na_strings()]
#' uses the special `#N/A` value within the workbook.
#' @param data_table logical. if `TRUE` and `rowNames = TRUE`, do not write the cell containing `"_rowNames_"`
#' @param inline_strings write characters as inline strings
#' @details
Expand Down Expand Up @@ -723,7 +725,8 @@ write_data2 <- function(
#' @param applyCellStyle apply styles when writing on the sheet
#' @param removeCellStyle if writing into existing cells, should the cell style be removed?
#' @param na.strings Value used for replacing `NA` values from `x`. Default
#' `na_strings()` uses the special `#N/A` value within the workbook.
#' looks if `options(openxlsx2.na.strings)` is set. Otherwise [na_strings()]
#' uses the special `#N/A` value within the workbook.
#' @param inline_strings optional write strings as inline strings
#' @param total_row optional write total rows
#' @noRd
Expand Down Expand Up @@ -782,6 +785,12 @@ write_data_table <- function(
if (is.null(x)) {
return(wb)
}
# overwrite na.strings if nothing was provided
# with whatever is in the option if not set to default
if (is_na_strings(na.strings) && !is.null(getOption("openxlsx2.na.strings"))) {

olivroy marked this conversation as resolved.
Show resolved Hide resolved
na.strings <- getOption("openxlsx2.na.strings")
}

if (data_table && nrow(x) < 1) {
warning("Found data table with zero rows, adding one.",
Expand Down
2 changes: 2 additions & 0 deletions R/write_xlsx.R
Expand Up @@ -267,6 +267,8 @@ write_xlsx <- function(x, file, as_table = FALSE, ...) {
na.strings <-
if ("na.strings" %in% names(params)) {
params$na.strings
} else if (!is.null(getOption("openxlsx2.na.strings"))) {
getOption("openxlsx2.na.strings")
} else {
na_strings()
}
Expand Down
2 changes: 2 additions & 0 deletions man/openxlsx2-deprecated.Rd

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

2 changes: 2 additions & 0 deletions man/openxlsx2_options.Rd

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

3 changes: 2 additions & 1 deletion man/wb_add_data.Rd

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

3 changes: 2 additions & 1 deletion man/wb_add_data_table.Rd

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

3 changes: 2 additions & 1 deletion man/write_data.Rd

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

3 changes: 2 additions & 1 deletion man/write_datatable.Rd

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

3 changes: 2 additions & 1 deletion man/write_xlsx.Rd

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

27 changes: 27 additions & 0 deletions tests/testthat/test-options.R
Expand Up @@ -15,3 +15,30 @@ test_that("sheet.default_name", {
expect_equal(exp, got)

})

test_that("nastrings option works", {

op <- options("openxlsx2.na.strings" = fmt_txt("N/A", italic = TRUE, color = wb_color("gray")))
on.exit(options(op), add = TRUE)

## create a data set with missings
dd <- mtcars
dd[dd < 3] <- NA

wb <- write_xlsx(x = dd)

exp <- "<is><r><rPr><i/><color rgb=\"FFBEBEBE\"/></rPr><t>N/A</t></r></is>"
got <- wb$worksheets[[1]]$sheet_data$cc[17, "is"]
expect_equal(got, exp)

wb <- wb_workbook()
wb$add_worksheet()
wb$add_data(x = dd, na.strings = "", row_names = TRUE)
res <- wb$to_df(row_names = TRUE)
expect_equal(res, dd)

wb$add_worksheet()
wb$add_data(x = dd, row_names = TRUE)
res2 <- wb$to_df(row_names = TRUE, na.strings = "N/A")
expect_equal(res2, dd)
})