Skip to content
9 changes: 9 additions & 0 deletions R/class-workbook-utils.R
Original file line number Diff line number Diff line change
Expand Up @@ -158,6 +158,15 @@ next_sheet <- function() {
structure("next_sheet", class = "openxlsx2_waiver")
}

na_strings <- function() {
structure("na_strings", class = "openxlsx2_waiver")
}

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

is_na_strings <- function(x) {
is_waiver(x) && isTRUE(x == "na_strings")
}

9 changes: 2 additions & 7 deletions R/class-workbook-wrappers.R
Original file line number Diff line number Diff line change
Expand Up @@ -114,12 +114,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 @@ -201,11 +198,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
8 changes: 2 additions & 6 deletions R/class-workbook.R
Original file line number Diff line number Diff line change
Expand Up @@ -982,11 +982,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 @@ -1048,11 +1046,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
22 changes: 7 additions & 15 deletions R/write.R
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
#' @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 +82,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 @@ -167,12 +167,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 +311,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 @@ -601,7 +599,7 @@ write_data_table <- function(
applyCellStyle = TRUE,
removeCellStyle = FALSE,
data_table = FALSE,
na.strings
na.strings = na_strings()
) {

op <- openxlsx2_options()
Expand All @@ -625,8 +623,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 @@ -930,11 +926,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 @@ -1236,11 +1230,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
4 changes: 2 additions & 2 deletions man/wbWorkbook.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_add_data_table.Rd

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

4 changes: 2 additions & 2 deletions man/write_data.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/write_data2.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/write_datatable.Rd

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