Skip to content

Commit

Permalink
[pt] fix non distinct names in wb_data object (#911)
Browse files Browse the repository at this point in the history
  • Loading branch information
JanMarvin committed Jan 24, 2024
1 parent 5443d62 commit 94935d7
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 0 deletions.
5 changes: 5 additions & 0 deletions R/class-workbook.R
Original file line number Diff line number Diff line change
Expand Up @@ -1395,6 +1395,11 @@ wbWorkbook <- R6::R6Class(
}
}

if (any(sel <- duplicated(tolower(names(x))))) {
nms <- names(x)
names(x) <- fix_pt_names(nms)
}

# for now we use a new worksheet
if (add_sheet) {
self$add_worksheet()
Expand Down
19 changes: 19 additions & 0 deletions R/helper-functions.R
Original file line number Diff line number Diff line change
Expand Up @@ -488,6 +488,25 @@ distinct <- function(x) {
unis[dups == FALSE]
}

# append number of duplicated value.
# @details c("Foo", "foo") -> c("Foo", "foo2")
# @param x a character vector
fix_pt_names <- function(x) {
lwrs <- tolower(x)

dups <- duplicated(lwrs)
dups <- stringi::stri_unique(lwrs[dups])

for (dup in dups) {
sel <- which(lwrs == dup)
for (i in seq_along(sel)[-1]) {
x[sel[i]] <- paste0(x[sel[i]], i)
}
}

x
}

cacheFields <- function(wbdata, filter, rows, cols, data, slicer) {
sapply(
names(wbdata),
Expand Down
10 changes: 10 additions & 0 deletions tests/testthat/test-helper-functions.R
Original file line number Diff line number Diff line change
Expand Up @@ -214,6 +214,16 @@ test_that("distinct() works", {

})

test_that("fix_pt_names() works", {

x <- c("Foo", "foo", "bar", "FOO", "bar", "x")

exp <- c("Foo", "foo2", "bar", "FOO3", "bar2", "x")
got <- fix_pt_names(x)
expect_equal(exp, got)

})

test_that("validate_colors() works", {

col <- c("FF0000FF", "#0000FF", "000FF", "#FF000FF", "blue")
Expand Down

0 comments on commit 94935d7

Please sign in to comment.