From 013b8ef0d639cf5d46249fc5ee6767b544a5ce47 Mon Sep 17 00:00:00 2001 From: Reinhold Kainhofer Date: Sun, 19 Aug 2018 00:23:07 +0200 Subject: [PATCH] Feature #375: Allow strings for keepNA argument of writeData(Table)/write.xlsx to customize the #N/A output --- R/workbook_write_data.R | 6 ++++-- R/writeData.R | 2 +- R/writeDataTable.R | 2 +- R/writexlsx.R | 6 +++--- man/write.xlsx.Rd | 2 +- man/writeData.Rd | 2 +- man/writeDataTable.Rd | 2 +- 7 files changed, 12 insertions(+), 10 deletions(-) diff --git a/R/workbook_write_data.R b/R/workbook_write_data.R index dfe753a..874c9dd 100644 --- a/R/workbook_write_data.R +++ b/R/workbook_write_data.R @@ -130,8 +130,10 @@ Workbook$methods(writeData = function(df, sheet, startRow, startCol, colNames, c - - if(keepNA){ + if (is.character(keepNA)) { + t[is.na(v)] <- 4L + v[is.na(v)] <- keepNA + } else if (keepNA) { t[is.na(v)] <- 4L v[is.na(v)] <- "#N/A" }else{ diff --git a/R/writeData.R b/R/writeData.R index dae9acf..2591f6d 100644 --- a/R/writeData.R +++ b/R/writeData.R @@ -38,7 +38,7 @@ #' \item{\bold{slantDashDot}}{ slanted dash-dot border} #' } #' @param withFilter If \code{TRUE}, add filters to the column name row. NOTE can only have one filter per worksheet. -#' @param keepNA If \code{TRUE}, NA values are converted to #N/A in Excel else NA cells will be empty. +#' @param keepNA If \code{TRUE}, NA values are converted to #N/A in Excel else NA cells will be empty. If a string is passed, that string is used instead of "#N/A". #' @param name If not NULL, a named region is defined. #' @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). #' @seealso \code{\link{writeDataTable}} diff --git a/R/writeDataTable.R b/R/writeDataTable.R index a756eb2..4e531d8 100644 --- a/R/writeDataTable.R +++ b/R/writeDataTable.R @@ -16,7 +16,7 @@ #' @param tableName name of table in workbook. The table name must be unique. #' @param headerStyle Custom style to apply to column names. #' @param withFilter If \code{TRUE}, columns with have filters in the first row. -#' @param keepNA If \code{TRUE}, NA values are converted to #N/A in Excel else NA cells will be empty. +#' @param keepNA If \code{TRUE}, NA values are converted to #N/A in Excel else NA cells will be empty. If a string is passed, that string is used instead of "#N/A". #' @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 stack If \code{TRUE} the new style is merged with any existing cell styles. If FALSE, any #' existing style is replaced by the new style. diff --git a/R/writexlsx.R b/R/writexlsx.R index 0a9d2af..5e1ec68 100644 --- a/R/writexlsx.R +++ b/R/writexlsx.R @@ -47,7 +47,7 @@ #' between each column. If "\code{all}" all cell borders are drawn.} #' \item{\bold{borderColour}}{ Colour of cell border} #' \item{\bold{borderStyle}}{ Border line style.} -#' \item{\bold{keepNA}} {If \code{TRUE}, NA values are converted to #N/A in Excel else NA cells will be empty. Defaults to FALSE.} +#' \item{\bold{keepNA}} {If \code{TRUE}, NA values are converted to #N/A in Excel else NA cells will be empty. Defaults to FALSE. If a string is passed, that string is used instead of #N/A.} #' } #' #' \bold{freezePane Parameters} @@ -319,8 +319,8 @@ write.xlsx <- function(x, file, asTable = FALSE, ...){ keepNA <- FALSE if("keepNA" %in% names(params)){ - if(!"logical" %in% class(keepNA)){ - stop("keepNA must be a logical.") + if(!"logical" %in% class(keepNA) && !"character" %in% class(keepNA)){ + stop("keepNA must be a logical or a string.") }else{ keepNA <- params$keepNA } diff --git a/man/write.xlsx.Rd b/man/write.xlsx.Rd index bf88543..41d698c 100644 --- a/man/write.xlsx.Rd +++ b/man/write.xlsx.Rd @@ -61,7 +61,7 @@ data. If "rows", a surrounding border is drawn a border around each row. If "co between each column. If "\code{all}" all cell borders are drawn.} \item{\bold{borderColour}}{ Colour of cell border} \item{\bold{borderStyle}}{ Border line style.} - \item{\bold{keepNA}} {If \code{TRUE}, NA values are converted to #N/A in Excel else NA cells will be empty. Defaults to FALSE.} + \item{\bold{keepNA}} {If \code{TRUE}, NA values are converted to #N/A in Excel else NA cells will be empty. Defaults to FALSE. If a string is passed, that string is used instead of #N/A.} } \bold{freezePane Parameters} diff --git a/man/writeData.Rd b/man/writeData.Rd index e910d7b..952cf63 100644 --- a/man/writeData.Rd +++ b/man/writeData.Rd @@ -61,7 +61,7 @@ each column. If "\code{all}" all cell borders are drawn.} \item{withFilter}{If \code{TRUE}, add filters to the column name row. NOTE can only have one filter per worksheet.} -\item{keepNA}{If \code{TRUE}, NA values are converted to #N/A in Excel else NA cells will be empty.} +\item{keepNA}{If \code{TRUE}, NA values are converted to #N/A in Excel else NA cells will be empty. If a string is passed, that string is used instead of "#N/A".} \item{name}{If not NULL, a named region is defined.} diff --git a/man/writeDataTable.Rd b/man/writeDataTable.Rd index 0621fbb..93757d8 100644 --- a/man/writeDataTable.Rd +++ b/man/writeDataTable.Rd @@ -37,7 +37,7 @@ A vector of the form c(startCol, startRow)} \item{withFilter}{If \code{TRUE}, columns with have filters in the first row.} -\item{keepNA}{If \code{TRUE}, NA values are converted to #N/A in Excel else NA cells will be empty.} +\item{keepNA}{If \code{TRUE}, NA values are converted to #N/A in Excel else NA cells will be empty. If a string is passed, that string is used instead of "#N/A".} \item{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).}