Skip to content

Instantly share code, notes, and snippets.

@JanMarvin
Created November 30, 2023 19:58
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save JanMarvin/e154c77c2c0377944d9ed70cd213e8c2 to your computer and use it in GitHub Desktop.
Save JanMarvin/e154c77c2c0377944d9ed70cd213e8c2 to your computer and use it in GitHub Desktop.
Pivot table example from LinkedIn
library(openxlsx2)
packageVersion("openxlsx2")
wb <- wb_workbook() %>%
wb_add_worksheet("table") %>%
wb_add_worksheet("data") %>%
wb_add_data(x = datasets::esoph)
df <- wb_data(wb)
# add a pivot table and a slicer and preselect
# a few cases and style it a bit
wb <- wb %>%
wb_add_pivot_table(
df, dims = "A3", sheet = "table",
rows = "agegp", cols = "tobgp", data = "ncases",
slicer = "alcgp", pivot_table = "pt1",
param = list(
show_data_as = c("percentOfRow"),
numfmt = c(formatCode = "0.0%"),
compact = FALSE, outline = FALSE, compactData = FALSE,
rowGrandTotals = FALSE, colGrandTotals = FALSE,
choose = c(agegp = 'x > "25-34"')
)
) %>%
wb_add_slicer(
x = df, dims = "B14:D18",
slicer = "alcgp", pivot_table = "pt1",
param = list(
columnCount = 2,
choose = c(alcgp = 'x %in% c("40-79", "80-119")')
)
)
if (interactive()) wb$open()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment