Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[WIP] add wb_add_slicers #822

Merged
merged 9 commits into from Oct 22, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
1 change: 1 addition & 0 deletions NAMESPACE
Expand Up @@ -66,6 +66,7 @@ export(wb_add_page_break)
export(wb_add_person)
export(wb_add_pivot_table)
export(wb_add_plot)
export(wb_add_slicer)
export(wb_add_sparklines)
export(wb_add_style)
export(wb_add_thread)
Expand Down
2 changes: 2 additions & 0 deletions NEWS.md
Expand Up @@ -17,6 +17,8 @@

* New set of function `wb_get_properties()`/`wb_set_properties()` to view and modify workbook properties. [782](https://github.com/JanMarvin/openxlsx2/pull/782) This was subsequently improved to handle more workbook properties like `company` and `manager`. ([799](https://github.com/JanMarvin/openxlsx2/pull/799), @olivroy)

* Basic (experimental) support to add slicers to pivot tables created by `openxlsx2`. [822](https://github.com/JanMarvin/openxlsx2/pull/822)

## Fixes

* Removing the worksheet that is the active tab does no longer result in warnings in spreadsheet software. [792](https://github.com/JanMarvin/openxlsx2/pull/792)
Expand Down
91 changes: 75 additions & 16 deletions R/class-workbook-wrappers.R
Expand Up @@ -317,6 +317,8 @@ wb_add_data_table <- function(
#' @param data The column name(s) of `x` used as data
#' @param fun A vector of functions to be used with `data`
#' @param params A list of parameters to modify pivot table creation.
#' @param pivot_table An optional name for the pivot table
#' @param slicer a character object with names used as slicer
#' @seealso [wb_data()]
#' @examples
#' wb <- wb_workbook() %>% wb_add_worksheet() %>% wb_add_data(x = mtcars)
Expand All @@ -340,30 +342,87 @@ wb_add_pivot_table <- function(
cols,
data,
fun,
params
params,
pivot_table,
slicer
) {
assert_workbook(wb)
if (missing(filter)) filter <- substitute()
if (missing(rows)) rows <- substitute()
if (missing(cols)) cols <- substitute()
if (missing(data)) data <- substitute()
if (missing(fun)) fun <- substitute()
if (missing(params)) params <- substitute()
if (missing(filter)) filter <- substitute()
if (missing(rows)) rows <- substitute()
if (missing(cols)) cols <- substitute()
if (missing(data)) data <- substitute()
if (missing(fun)) fun <- substitute()
if (missing(params)) params <- substitute()
if (missing(pivot_table)) pivot_table <- substitute()
if (missing(slicer)) slicer <- substitute()

wb$clone()$add_pivot_table(
x = x,
sheet = sheet,
dims = dims,
filter = filter,
rows = rows,
cols = cols,
data = data,
fun = fun,
params = params
x = x,
sheet = sheet,
dims = dims,
filter = filter,
rows = rows,
cols = cols,
data = data,
fun = fun,
params = params,
pivot_table = pivot_table,
slicer = slicer
)

}

#' Add a slicer to a pivot table
#'
#' Add a slicer to a previously created pivot table. This function is still experimental and might be changed/improved in upcoming releases.
#'
#' @param wb A Workbook object containing a #' worksheet.
#' @param x A `data.frame` that inherits the [`wb_data`][wb_data()] class.
#' @param sheet A worksheet containing a #'
#' @param dims The worksheet cell where the pivot table is placed
#' @param pivot_table the name of a pivot table on the selected sheet
#' @param slicer a variable used as slicer for the pivot table
#' @param params a list of parameters to modify pivot table creation
#' @family workbook wrappers
#' @family worksheet content functions
#' @details This assumes that the slicer variable initialization has happened before. Unfortunately, it is unlikely that we can guarantee this for loaded workbooks, and we *strictly* discourage users from attempting this. If the variable has not been initialized properly, this may cause the spreadsheet software to crash.
#'
#' For the time being, the slicer needs to be placed on the slide with the pivot table.
#' @examples
#' wb <- wb_workbook() %>%
#' wb_add_worksheet() %>% wb_add_data(x = mtcars)
#'
#' df <- wb_data(wb, sheet = 1)
#'
#' wb <- wb %>%
#' wb_add_pivot_table(
#' df, dims = "A3", slicer = "vs", rows = "cyl", cols = "gear", data = "disp",
#' pivot_table = "mtcars"
#' ) %>%
#' wb_add_slicer(x = df, slicer = "vs", pivot_table = "mtcars")
#' @export
wb_add_slicer <- function(
wb,
x,
dims = "A1",
sheet = current_sheet(),
pivot_table,
slicer,
params
) {
assert_workbook(wb)
if (missing(params)) params <- substitute()

wb$clone()$add_slicer(
x = x,
sheet = sheet,
dims = dims,
pivot_table = pivot_table,
slicer = slicer,
params = params
)

}

#' Add a formula to a cell range in a worksheet
#'
Expand Down