Skip to content

Commit

Permalink
provide dims()
Browse files Browse the repository at this point in the history
  • Loading branch information
JanMarvin committed Jul 15, 2023
1 parent 46a1080 commit 2e4d10b
Show file tree
Hide file tree
Showing 4 changed files with 62 additions and 0 deletions.
1 change: 1 addition & 0 deletions NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ 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
39 changes: 39 additions & 0 deletions R/utils.R
Original file line number Diff line number Diff line change
Expand Up @@ -259,6 +259,45 @@ rowcol_to_dim <- function(row, col) {
# we will always return something like "A1"
stringi::stri_join(min_col, min_row)
}

#' @rdname dims_helper
#' @param ... dims arguments, row/col, rows/cols or objects that can be converted to data frame
#' @export
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

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)
} else {

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)
}

}

dims
}

# Relationship helpers --------------------
#' removes entries from worksheets_rels
#' @param x character string
Expand Down
5 changes: 5 additions & 0 deletions man/dims_helper.Rd

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

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

})

test_that("dims() works", {

# dim(mtcars)

expect_equal(dims(mtcars), "A1:K32")

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

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

expect_equal(dims(1), "A1")

expect_equal(dims(rows = 1:10, cols = 5:7), "E1:G10")

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

})

test_that("create_char_dataframe", {

Expand Down

0 comments on commit 2e4d10b

Please sign in to comment.