Created
November 30, 2023 19:58
-
-
Save JanMarvin/e154c77c2c0377944d9ed70cd213e8c2 to your computer and use it in GitHub Desktop.
Pivot table example from LinkedIn
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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