From 1a7e42170d2993560cd83d85d60fcd9e472ad967 Mon Sep 17 00:00:00 2001 From: Jan Marvin Garbuszus Date: Tue, 29 Oct 2024 22:30:51 +0100 Subject: [PATCH] [slicer] allow setting `hide items with no data` (#1169) --- NEWS.md | 4 ++++ R/class-workbook-wrappers.R | 1 + R/class-workbook.R | 18 +++++++++++++++--- man/wb_add_slicer.Rd | 1 + tests/testthat/test-write.R | 26 ++++++++++++++++++++++++++ 5 files changed, 47 insertions(+), 3 deletions(-) diff --git a/NEWS.md b/NEWS.md index 6c6b7d6ed..aff60e545 100644 --- a/NEWS.md +++ b/NEWS.md @@ -1,5 +1,9 @@ # openxlsx2 (development version) +## New features + +* Add `hide_no_data_items` option in `wb_add_slicer()`. [1169](https://github.com/JanMarvin/openxlsx2/pull/1169) + ## Fixes * Previously rows that trigger scientific notation (e.g. `1e+05`) would cause issues, when matched against a non scientific version. [1170](https://github.com/JanMarvin/openxlsx2/pull/1170) diff --git a/R/class-workbook-wrappers.R b/R/class-workbook-wrappers.R index 726952cf5..c04f0fbdd 100644 --- a/R/class-workbook-wrappers.R +++ b/R/class-workbook-wrappers.R @@ -493,6 +493,7 @@ wb_add_pivot_table <- function( #' `c(agegp = 'x > "25-34"')` for the `esoph` dataset. #' * locked_position #' * start_item +#' * hide_no_data_items #' #' Possible `params` arguments for timelines are listed below. #' * beg_date/end_date: dates when the timeline should begin or end diff --git a/R/class-workbook.R b/R/class-workbook.R index 2153a1646..44909d97f 100644 --- a/R/class-workbook.R +++ b/R/class-workbook.R @@ -1758,8 +1758,8 @@ wbWorkbook <- R6::R6Class( } else { arguments <- c( "caption", "choose", "column_count", "cross_filter", "edit_as", - "level", "locked_position", "row_height", "show_caption", - "show_missing", "sort_order", "start_item", "style" + "hide_no_data_items", "level", "locked_position", "row_height", + "show_caption", "show_missing", "sort_order", "start_item", "style" ) params <- standardize_case_names(params, arguments = arguments, return = TRUE) } @@ -1819,6 +1819,16 @@ wbWorkbook <- R6::R6Class( xml_children = get_items(x, which(names(x) == slicer), NULL, slicer = TRUE, choose = choo, has_default = TRUE) ) + + hide_items_with_no_data <- "" + if (isTRUE(params$hide_no_data_items)) { + hide_items_with_no_data <- ' + + + + ' + } + slicer_cache <- read_xml(sprintf( ' @@ -1827,12 +1837,14 @@ wbWorkbook <- R6::R6Class( %s + %s ', uni_name, slicer, sheet, pivot_table, - tab_xml + tab_xml, + hide_items_with_no_data ), pointer = FALSE) # we need the slicer cache diff --git a/man/wb_add_slicer.Rd b/man/wb_add_slicer.Rd index 4584741d4..e86d30b8e 100644 --- a/man/wb_add_slicer.Rd +++ b/man/wb_add_slicer.Rd @@ -62,6 +62,7 @@ Possible \code{params} arguments for slicers are listed below. \code{c(agegp = 'x > "25-34"')} for the \code{esoph} dataset. \item locked_position \item start_item +\item hide_no_data_items } Possible \code{params} arguments for timelines are listed below. diff --git a/tests/testthat/test-write.R b/tests/testthat/test-write.R index 47171ee75..2ae32408c 100644 --- a/tests/testthat/test-write.R +++ b/tests/testthat/test-write.R @@ -784,6 +784,32 @@ test_that("removing timelines works", { }) +test_that("slicer extension 'hide_no_data_items' works", { + + dat <- data.frame( + var = c("x", "y", "y", "z", "z", "x"), + speed = c(4, 4, 7, 7, 8, 9), + dis = c(2, 10, 4, 22, 16, 10), + slicervar1 = c("option1", "option1", "option1", "option2", "option2", "option2"), + slicervar2 = c("choice1", "choice1", "choice2", "choice3", "choice3", "choice1") + ) + + wb <- wb_workbook()$ + add_worksheet("pivot")$ + add_worksheet("dat")$add_data(x = dat) + + df <- wb_data(wb) + + wb$ + add_pivot_table(df, sheet = "pivot", dims = "A3", slicer = c("slicervar1", "slicervar2"), rows = c("var"), data = "speed", params = list(name = "pivot_1"))$ + add_slicer(x = df, sheet = "pivot", dims = "D3:E9", slicer = "slicervar1", pivot_table = "pivot_1", param = list(hide_no_data_items = TRUE))$ + add_slicer(x = df, sheet = "pivot", dims = "F3:G9", slicer = "slicervar2", pivot_table = "pivot_1") + + expect_true(grepl("x15:slicerCacheHideItemsWithNoData", wb$slicerCaches[[1]])) + expect_true(!grepl("x15:slicerCacheHideItemsWithNoData", wb$slicerCaches[[2]])) + +}) + test_that("writing na.strings = NULL works", { # write na.strings = na_strings()