Skip to content

Commit

Permalink
adds warning for col.names
Browse files Browse the repository at this point in the history
adds warning and tests for use of col.names
  • Loading branch information
jmbarbone committed May 30, 2021
1 parent 8fe13fc commit 9fda214
Show file tree
Hide file tree
Showing 6 changed files with 42 additions and 9 deletions.
8 changes: 7 additions & 1 deletion R/writeData.R
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
#' \code{c(startCol, startRow)}.
#' @param colNames If \code{TRUE}, column names of x are written.
#' @param rowNames If \code{TRUE}, data.frame row names of x are written.
#' @param row.names Deprecated, please use \code{rowNames} instead
#' @param row.names,col.names Deprecated, please use \code{rowNames}, \code{colNames} instead
#' @param headerStyle Custom style to apply to column names.
#' @param borders Either "\code{none}" (default), "\code{surrounding}",
#' "\code{columns}", "\code{rows}" or \emph{respective abbreviations}. If
Expand Down Expand Up @@ -175,6 +175,7 @@ writeData <- function(
na.string = openxlsx_getOp("na.string"),
name = NULL,
sep = ", ",
col.names,
row.names
) {

Expand All @@ -186,6 +187,11 @@ writeData <- function(
rowNames <- row.names
}

if (!missing(col.names)) {
warning("Please use 'colNames' instead of 'col.names'", call. = FALSE)
colNames <- col.names
}

# Set NULLs
borders <- borders %||% "none"
borderColour <- borderColour %||% "black"
Expand Down
8 changes: 7 additions & 1 deletion R/writeDataTable.R
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
#' A vector of the form c(startCol, startRow)
#' @param colNames If \code{TRUE}, column names of x are written.
#' @param rowNames If \code{TRUE}, row names of x are written.
#' @param row.names Deprecated, please use \code{rowNames} instead
#' @param row.names,col.names Deprecated, please use \code{rowNames}, \code{colNames} instead
#' @param tableStyle Any excel table style name or "none" (see "formatting" vignette).
#' @param tableName name of table in workbook. The table name must be unique.
#' @param headerStyle Custom style to apply to column names.
Expand Down Expand Up @@ -157,6 +157,7 @@ writeDataTable <- function(
lastColumn = openxlsx_getOp("lastColumn", FALSE),
bandedRows = openxlsx_getOp("bandedRows", TRUE),
bandedCols = openxlsx_getOp("bandedCols", FALSE),
col.names,
row.names
) {
op <- get_set_options()
Expand All @@ -169,6 +170,11 @@ writeDataTable <- function(
row.names <- rowNames
}

if (!missing(col.names)) {
warning("Please use 'colNames' instead of 'col.names'", call. = FALSE)
colNames <- col.names
}

# Set NULLs
withFilter <- withFilter %||% TRUE
keepNA <- keepNA %||% FALSE
Expand Down
3 changes: 2 additions & 1 deletion man/writeData.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/writeDataTable.Rd

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

24 changes: 24 additions & 0 deletions tests/testthat/test-build_workbook.R
Original file line number Diff line number Diff line change
Expand Up @@ -16,3 +16,27 @@ test_that("buildWorkbook() accepts tableName [187]", {
# try to define 1/2 table names
expect_error(buildWorkbook(list(x, x), asTable = TRUE, tableName = "table_x"))
})

test_that("row.name and col.name are deprecated", {
x <- data.frame(a = 1)

expect_warning(
buildWorkbook(x, file = temp_xlsx(), row.names = TRUE, overwrite = TRUE),
"Please use 'rowNames' instead of 'row.names'"
)

expect_warning(
buildWorkbook(x, file = temp_xlsx(), row.names = TRUE, overwrite = TRUE, asTable = TRUE),
"Please use 'rowNames' instead of 'row.names'"
)

expect_warning(
buildWorkbook(x, file = temp_xlsx(), col.names = TRUE, overwrite = TRUE),
"Please use 'colNames' instead of 'col.names'"
)

expect_warning(
buildWorkbook(x, file = temp_xlsx(), col.names = TRUE, overwrite = TRUE, asTable = TRUE),
"Please use 'colNames' instead of 'col.names'"
)
})
5 changes: 0 additions & 5 deletions tests/testthat/test-write_read_equality.R
Original file line number Diff line number Diff line change
Expand Up @@ -119,11 +119,6 @@ test_that("Writing then reading rowNames, colNames combinations", {
curr_wd <- getwd()
mt <- utils::head(mtcars) # don't need the whole thing

expect_warning(
write.xlsx(mt, file = temp_xlsx(), row.names = TRUE, overwrite = TRUE),
"Please use 'rowNames' instead of 'row.names'"
)

# write the row and column names for testing
write.xlsx(mt, file = fileName, overwrite = TRUE, rowNames = TRUE, colNames = TRUE)

Expand Down

0 comments on commit 9fda214

Please sign in to comment.