diff --git a/R/helper-functions.R b/R/helper-functions.R index 30fbead15..11ea3e561 100644 --- a/R/helper-functions.R +++ b/R/helper-functions.R @@ -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)) { diff --git a/tests/testthat/test-class-workbook.R b/tests/testthat/test-class-workbook.R index bfe757a95..77e9ec67e 100644 --- a/tests/testthat/test-class-workbook.R +++ b/tests/testthat/test-class-workbook.R @@ -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." ) })