Skip to content

Commit

Permalink
[pt] another attemt to fix item sorting (#912)
Browse files Browse the repository at this point in the history
  • Loading branch information
JanMarvin authored Jan 24, 2024
1 parent 94935d7 commit a5282f1
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 13 deletions.
39 changes: 28 additions & 11 deletions R/helper-functions.R
Original file line number Diff line number Diff line change
Expand Up @@ -673,19 +673,36 @@ get_items <- function(data, x, item_order, slicer = FALSE, choose = NULL) {
dat <- distinct(data[[x]])

# check length, otherwise a certain spreadsheet software simply dies
if (!is.null(item_order) && (length(item_order) != length(dat))) {
msg <- sprintf(
"Length of sort order for '%s' does not match required length. Is %s, needs %s.\nCheck `openxlsx2:::distinct()` for the correct length. Resetting.",
names(data[x]), length(item_order), length(dat)
)
warning(msg)
item_order <- NULL
}
if (!is.null(item_order)) {

if (length(item_order) > length(dat)) {
msg <- sprintf(
"Length of sort order for '%s' does not match required length. Is %s, needs %s.\nCheck `openxlsx2:::distinct()` for the correct length. Resetting.",
names(data[x]), length(item_order), length(dat)
)
warning(msg)
item_order <- NULL
}

if (is.character(item_order)) {
# add remaining items
if (length(item_order) < length(dat)) {
item_order <- c(item_order, dat[!dat %in% item_order])
}

if (is.null(item_order)) {
item_order <- match(item_order, dat)

} else {
# add remaining items
if (length(item_order) < length(dat)) {
vals <- seq_along(dat)
item_order <- c(item_order, vals[!vals %in% item_order])
}
}

} else {
# item_order == NULL
item_order <- order(dat)
} else if (is.character(item_order)) {
item_order <- match(dat, item_order)
}

if (!is.null(choose)) {
Expand Down
4 changes: 2 additions & 2 deletions tests/testthat/test-class-workbook.R
Original file line number Diff line number Diff line change
Expand Up @@ -918,9 +918,9 @@ expect_silent(
expect_warning(
wb_add_pivot_table(wb, df, dims = "A3",
filter = "am", rows = "cyl", cols = "gear", data = "disp",
params = list(sort_item = list(gear = c(1)))
params = list(sort_item = list(gear = seq_len(4)))
),
"Length of sort order for 'gear' does not match required length. Is 1, needs 3."
"Length of sort order for 'gear' does not match required length. Is 4, needs 3."
)

})
Expand Down

0 comments on commit a5282f1

Please sign in to comment.