Skip to content

Commit

Permalink
change function name to wb_dims() handle some exceptions
Browse files Browse the repository at this point in the history
  • Loading branch information
JanMarvin committed Jul 15, 2023
1 parent 2e4d10b commit 21967f1
Show file tree
Hide file tree
Showing 4 changed files with 62 additions and 24 deletions.
2 changes: 1 addition & 1 deletion NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,6 @@ export(create_tablestyle)
export(current_sheet)
export(dataframe_to_dims)
export(delete_data)
export(dims)
export(dims_to_dataframe)
export(dims_to_rowcol)
export(fmt_txt)
Expand Down Expand Up @@ -77,6 +76,7 @@ export(wb_color)
export(wb_colour)
export(wb_copy_cells)
export(wb_data)
export(wb_dims)
export(wb_freeze_pane)
export(wb_get_active_sheet)
export(wb_get_base_font)
Expand Down
51 changes: 38 additions & 13 deletions R/utils.R
Original file line number Diff line number Diff line change
Expand Up @@ -261,38 +261,63 @@ rowcol_to_dim <- function(row, col) {
}

#' @rdname dims_helper
#' @param ... dims arguments, row/col, rows/cols or objects that can be converted to data frame
#' @param ... construct dims arguments, from rows/cols vectors or objects that can be coerced to data frame
#' @examples
#' # either vectors
#' wb_dims(rows = 1:10, cols = 1:10)
#' # or objects
#' wb_dims(mtcars)
#' @export
dims <- function(...) {
wb_dims <- function(...) {

args <- list(...)
nams <- names(args)

col_names <- args$col_names
row_names <- args$row_names

if (is.null(col_names)) col_names <- FALSE
if (is.null(row_names)) row_names <- FALSE
has_cnam <- is.null(col_names)
has_rnam <- is.null(row_names)

if (has_cnam) col_names <- FALSE
if (has_rnam) row_names <- FALSE

assert_class(col_names, "logical")
assert_class(row_names, "logical")

if (any("row" %in% nams) && any("col" %in% nams)) {
dims <- rowcol_to_dim(args$row, args$col)
} else if (any("rows" %in% nams) && any("cols" %in% nams)) {
dims <- rowcol_to_dims(args$rows, args$cols)
# wb_dims(rows, cols)
if (length(args) == 2 && has_cnam && has_rnam) {
rows <- 1L
cols <- 2L

# wb_dims(rows = rows, cols = cols)
sel <- pmatch(nams, c("rows", "cols"))
valid <- length(sel[!is.na(sel)])
if (valid == 2) {
rows <- sel[rows]
cols <- sel[cols]
} else if (valid == 1) {
stop("found only one cols/rows argument")
}

rows <- args[[rows]]
cols <- args[[cols]]

} else {

# wb_dims(data.frame())
x <- as.data.frame(args[[1]])
rows <- seq_len(nrow(x) + col_names)
cols <- seq_len(ncol(x) + row_names)

if (length(rows) == 1L && length(cols) == 1L) {
dims <- rowcol_to_dim(rows, cols)
} else {
dims <- rowcol_to_dims(rows, cols)
}
}

if (length(rows) == 1 && length(cols) == 1) {
# A1
dims <- rowcol_to_dim(rows, cols)
} else {
# A1:B2
dims <- rowcol_to_dims(rows, cols)
}

dims
Expand Down
10 changes: 7 additions & 3 deletions man/dims_helper.Rd

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

23 changes: 16 additions & 7 deletions tests/testthat/test-utils.R
Original file line number Diff line number Diff line change
Expand Up @@ -46,21 +46,30 @@ test_that("dims to col & row and back", {

})

test_that("dims() works", {
test_that("wb_dims() works", {

# dim(mtcars)

expect_equal(dims(mtcars), "A1:K32")
expect_equal(wb_dims(mtcars), "A1:K32")
expect_equal(wb_dims(mtcars, col_names = TRUE, row_names = TRUE), "A1:L33")

expect_equal(dims(letters), "A1:A26")
expect_equal(wb_dims(letters), "A1:A26")

expect_equal(dims(t(letters)), "A1:Z1")
expect_equal(wb_dims(t(letters)), "A1:Z1")

expect_equal(dims(1), "A1")
expect_equal(wb_dims(1), "A1")

expect_equal(dims(rows = 1:10, cols = 5:7), "E1:G10")
expect_equal(wb_dims(rows = 1:10, cols = 5:7), "E1:G10")
expect_equal(wb_dims(cols = 1:10, rows = 5:7), "A5:J7")
expect_error(
wb_dims(cols = 1:10, col = 5:7),
"found only one cols/rows argument"
)

expect_equal(wb_dims(row = 5, col = 7), "G5")

expect_equal(dims(row = 5, col = 7), "G5")
expect_equal(wb_dims(1:10, LETTERS), "A1:Z10")
expect_equal(wb_dims(1:10, 1:26), "A1:Z10")

})

Expand Down

0 comments on commit 21967f1

Please sign in to comment.