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

473 na strings #474

Merged
merged 14 commits into from
Feb 20, 2023
3 changes: 3 additions & 0 deletions NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ export(create_font)
export(create_hyperlink)
export(create_numfmt)
export(create_sparklines)
export(current_sheet)
export(dataframe_to_dims)
export(delete_data)
export(dims_to_dataframe)
Expand All @@ -28,6 +29,8 @@ export(get_date_origin)
export(get_named_regions)
export(guess_col_type)
export(int2col)
export(na_strings)
export(next_sheet)
export(read_sheet_names)
export(read_xlsx)
export(read_xml)
Expand Down
24 changes: 24 additions & 0 deletions R/class-workbook-utils.R
Original file line number Diff line number Diff line change
Expand Up @@ -150,14 +150,38 @@ validate_cf_params <- function(params) {

# waivers -----------------------------------------------------------------

#' `openxlsx2` waivers
#'
#' Waiver functions for `openxlsx2` functions
#'
#' @name waivers
#' @returns An object of class `openxlsx2_waiver`
NULL

#' @rdname waivers
#' @export
current_sheet <- function() {
structure("current_sheet", class = "openxlsx2_waiver")
}

#' @rdname waivers
#' @export
next_sheet <- function() {
structure("next_sheet", class = "openxlsx2_waiver")
}

#' @rdname waivers
#' @export
na_strings <- function() {
JanMarvin marked this conversation as resolved.
Show resolved Hide resolved
structure("na_strings", class = "openxlsx2_waiver")
}

# helpers -----------------------------------------------------------------

is_waiver <- function(x) {
inherits(x, "openxlsx2_waiver")
}

is_na_strings <- function(x) {
is_waiver(x) && isTRUE(x == "na_strings")
}
15 changes: 6 additions & 9 deletions R/class-workbook-wrappers.R
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,8 @@ wb_save <- function(wb, path = NULL, overwrite = TRUE) {
#' @param sep Only applies to list columns. The separator used to collapse list columns to a character vector e.g. sapply(x$list_column, paste, collapse = sep).
#' @param applyCellStyle Should we write cell styles to the workbook
#' @param removeCellStyle keep the cell style?
#' @param na.strings na.strings
#' @param na.strings Value used for replacing `NA` values from `x`. Default
#' `na_strings()` uses the special `#N/A` value within the workbook.
#' @export
#' @details Formulae written using write_formula to a Workbook object will not get picked up by read_xlsx().
#' This is because only the formula is written and left to Excel to evaluate the formula when the file is opened in Excel.
Expand All @@ -114,12 +115,9 @@ wb_add_data <- function(
sep = ", ",
applyCellStyle = TRUE,
removeCellStyle = FALSE,
na.strings
na.strings = na_strings()
) {
assert_workbook(wb)

if (missing(na.strings)) na.strings <- substitute()

wb$clone(deep = TRUE)$add_data(
sheet = sheet,
x = x,
Expand Down Expand Up @@ -172,7 +170,8 @@ wb_add_data <- function(
#' @param bandedCols logical. If TRUE, the columns are colour banded
#' @param applyCellStyle Should we write cell styles to the workbook
#' @param removeCellStyle keep the cell style?
#' @param na.strings optional
#' @param na.strings Value used for replacing `NA` values from `x`. Default
#' `na_strings()` uses the special `#N/A` value within the workbook.
#'
#' @details columns of x with class Date/POSIXt, currency, accounting,
#' hyperlink, percentage are automatically styled as dates, currency,
Expand Down Expand Up @@ -201,11 +200,9 @@ wb_add_data_table <- function(
bandedCols = FALSE,
applyCellStyle = TRUE,
removeCellStyle = FALSE,
na.strings
na.strings = na_strings()
) {
assert_workbook(wb)
if (missing(na.strings)) na.strings <- substitute()

wb$clone()$add_data_table(
sheet = sheet,
x = x,
Expand Down
14 changes: 6 additions & 8 deletions R/class-workbook.R
Original file line number Diff line number Diff line change
Expand Up @@ -1008,7 +1008,8 @@ wbWorkbook <- R6::R6Class(
#' @param sep sep
#' @param applyCellStyle applyCellStyle
#' @param removeCellStyle if writing into existing cells, should the cell style be removed?
#' @param na.strings na.strings
#' @param na.strings Value used for replacing `NA` values from `x`. Default
#' `na_strings()` uses the special `#N/A` value within the workbook.
#' @param return The `wbWorkbook` object
add_data = function(
sheet = current_sheet(),
Expand All @@ -1025,11 +1026,9 @@ wbWorkbook <- R6::R6Class(
sep = ", ",
applyCellStyle = TRUE,
removeCellStyle = FALSE,
na.strings
na.strings = na_strings()
) {

if (missing(na.strings)) na.strings <- substitute()

write_data(
wb = self,
sheet = sheet,
Expand Down Expand Up @@ -1070,7 +1069,8 @@ wbWorkbook <- R6::R6Class(
#' @param bandedCols bandedCols
#' @param applyCellStyle applyCellStyle
#' @param removeCellStyle if writing into existing cells, should the cell style be removed?
#' @param na.strings na.strings
#' @param na.strings Value used for replacing `NA` values from `x`. Default
#' `na_strings()` uses the special `#N/A` value within the workbook.
#' @returns The `wbWorkbook` object
add_data_table = function(
sheet = current_sheet(),
Expand All @@ -1091,11 +1091,9 @@ wbWorkbook <- R6::R6Class(
bandedCols = FALSE,
applyCellStyle = TRUE,
removeCellStyle = FALSE,
na.strings
na.strings = na_strings()
) {

if (missing(na.strings)) na.strings <- substitute()

write_datatable(
wb = self,
sheet = sheet,
Expand Down
37 changes: 17 additions & 20 deletions R/write.R
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,13 @@
#' @param cell the cell you want to update in Excel connotation e.g. "A1"
#' @param colNames if TRUE colNames are passed down
#' @param removeCellStyle keep the cell style?
#' @param na.strings optional na.strings argument. if missing #N/A is used. If NULL no cell value is written, if character or numeric this is written (even if NA is part of numeric data)
#' @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
#' @noRd
update_cell <- function(x, wb, sheet, cell, colNames = FALSE,
removeCellStyle = FALSE, na.strings) {
removeCellStyle = FALSE, na.strings = na_strings()) {

sheet_id <- wb$validate_sheet(sheet)

Expand Down Expand Up @@ -82,7 +83,7 @@ update_cell <- function(x, wb, sheet, cell, colNames = FALSE,
wb$worksheets[[sheet_id]]$dimension <- paste0("<dimension ref=\"", min_cell, ":", max_cell, "\"/>")
}

if (missing(na.strings)) {
if (is_na_strings(na.strings)) {
na.strings <- NULL
}

Expand Down Expand Up @@ -133,7 +134,8 @@ nmfmt_df <- function(x) {
#' @param startCol col to place it
#' @param applyCellStyle apply styles when writing on the sheet
#' @param removeCellStyle keep the cell style?
#' @param na.strings optional na.strings argument. if missing #N/A is used. If NULL no cell value is written, if character or numeric this is written (even if NA is part of numeric data)
#' @param na.strings Value used for replacing `NA` values from `x`. Default
#' `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_"`
#' @details
#' The string `"_openxlsx_NA"` is reserved for `openxlsx2`. If the data frame
Expand Down Expand Up @@ -167,12 +169,10 @@ write_data2 <- function(
startCol = 1,
applyCellStyle = TRUE,
removeCellStyle = FALSE,
na.strings,
na.strings = na_strings(),
data_table = FALSE
) {

if (missing(na.strings)) na.strings <- substitute()

is_data_frame <- FALSE
#### prepare the correct data formats for openxml
dc <- openxlsx2_type(data)
Expand Down Expand Up @@ -313,7 +313,7 @@ write_data2 <- function(
## replace NA, NaN, and Inf
is_na <- which(cc$is == "<is><t>_openxlsx_NA</t></is>" | cc$v == "NA")
if (length(is_na)) {
if (missing(na.strings)) {
if (is_na_strings(na.strings)) {
cc[is_na, "v"] <- "#N/A"
cc[is_na, "c_t"] <- "e"
cc[is_na, "is"] <- ""
Expand Down Expand Up @@ -576,7 +576,8 @@ write_data2 <- function(
#' @param name If not NULL, a named region is defined.
#' @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 optional na.strings argument. if missing #N/A is used. If NULL no cell value is written, if character or numeric this is written (even if NA is part of numeric data)
#' @param na.strings Value used for replacing `NA` values from `x`. Default
#' `na_strings()` uses the special `#N/A` value within the workbook.
#' @noRd
write_data_table <- function(
wb,
Expand All @@ -601,7 +602,7 @@ write_data_table <- function(
applyCellStyle = TRUE,
removeCellStyle = FALSE,
data_table = FALSE,
na.strings
na.strings = na_strings()
) {

## Input validating
Expand All @@ -628,8 +629,6 @@ write_data_table <- function(
startRow <- min(dims[[2]])
}

if (missing(na.strings)) na.strings <- substitute()

## common part ---------------------------------------------------------------
if ((!is.character(sep)) || (length(sep) != 1))
stop("sep must be a character vector of length 1")
Expand Down Expand Up @@ -856,7 +855,8 @@ write_data_table <- function(
#' @param name If not NULL, a named region is defined.
#' @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 optional na.strings argument. if missing #N/A is used. If NULL no cell value is written, if character or numeric this is written (even if NA is part of numeric data)
#' @param na.strings Value used for replacing `NA` values from `x`. Default
#' `na_strings()` uses the special `#N/A` value within the workbook.
#' @seealso [write_datatable()]
#' @export write_data
#' @details Formulae written using write_formula to a Workbook object will not get picked up by read_xlsx().
Expand Down Expand Up @@ -933,11 +933,9 @@ write_data <- function(
name = NULL,
applyCellStyle = TRUE,
removeCellStyle = FALSE,
na.strings
na.strings = na_strings()
) {

if (missing(na.strings)) na.strings <- substitute()

write_data_table(
wb = wb,
sheet = sheet,
Expand Down Expand Up @@ -1126,7 +1124,8 @@ write_formula <- function(
#' @param bandedCols logical. If TRUE, the columns are colour banded
#' @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 optional na.strings argument. if missing #N/A is used. If NULL no cell value is written, if character or numeric this is written (even if NA is part of numeric data)
#' @param na.strings Value used for replacing `NA` values from `x`. Default
#' `na_strings()` uses the special `#N/A` value within the workbook.
#' @details columns of x with class Date/POSIXt, currency, accounting,
#' hyperlink, percentage are automatically styled as dates, currency, accounting,
#' hyperlinks, percentages respectively.
Expand Down Expand Up @@ -1239,11 +1238,9 @@ write_datatable <- function(
bandedCols = FALSE,
applyCellStyle = TRUE,
removeCellStyle = FALSE,
na.strings
na.strings = na_strings()
) {

if (missing(na.strings)) na.strings <- substitute()

write_data_table(
wb = wb,
sheet = sheet,
Expand Down
9 changes: 3 additions & 6 deletions R/write_xlsx.R
Original file line number Diff line number Diff line change
Expand Up @@ -284,10 +284,7 @@ write_xlsx <- function(x, file, asTable = FALSE, ...) {
tableStyle <- params$tableStyle
}

na_strings <- substitute()
if ("na.strings" %in% names(params)) {
na_strings <- params$na.strings
}
na.strings <- params$na.strings %||% na_strings()


## create new Workbook object
Expand Down Expand Up @@ -378,7 +375,7 @@ write_xlsx <- function(x, file, asTable = FALSE, ...) {
tableStyle = tableStyle[[i]],
tableName = NULL,
withFilter = withFilter[[i]],
na.strings = na_strings
na.strings = na.strings
)
} else {
write_data(
Expand All @@ -390,7 +387,7 @@ write_xlsx <- function(x, file, asTable = FALSE, ...) {
xy = xy,
colNames = colNames[[i]],
rowNames = rowNames[[i]],
na.strings = na_strings
na.strings = na.strings
)
}

Expand Down
21 changes: 21 additions & 0 deletions man/waivers.Rd

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

10 changes: 6 additions & 4 deletions man/wbWorkbook.Rd

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

5 changes: 3 additions & 2 deletions man/wb_add_data_table.Rd

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

Loading