-
Notifications
You must be signed in to change notification settings - Fork 15
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
Conversation
@lauraearle515 @Amaru6, maybe you guys can give this a try and see if it works for your use case. You can install from this branch with the following command and example code is above remotes::install_github("JanMarvin/openxlsx2#822") I won't work on adding slicers to existing pivot tables (aka those that are loaded from a workbook) and wont work on further complicated cases. For the moment the slicer must be on the table of the pivot table. |
Hi @JanMarvin . Thank you so much for your effort and time. Your example worked perfectly in my machine: `
Matrix products: default locale: `
This results in: When I only add the parameter name = "pivot_1", and not the slicers I get the following when I open the Excel file: |
Thank you for testing @Amaru6 ! (The error was caused by a simple typo, caused by late night coding ...). I'm not yet sure how the default interface should look like. Unfortunately slicers use distinct column items similar to pivot tables, that's why I have to pass the And there is another little thing, we simply overwrite the extension list of the worksheet where we add the slicer. This needs some kind of integration. Otherwise, it needs testing. I cannot test every corner case, therefore I'm glad if people help me out and report their findings. Even "everything works perfect" is a very useful reply in that regard. This should work now: library(openxlsx2)
tbl_test <- tibble::tribble(
~Total, ~char_1, ~año, ~mes, ~char_2, ~char_3,
1.21, "a", 2023, "Mar", "fuente_1", "no",
5, "b", 2023, "Ene", "fuente_1", "si",
0.24, "c", 2021, "May", "fuente_2", "no",
6.3, "d", 2022, "Jun", "fuente_1", "no",
0.3, "e", 2022, "Jul", "fuente_2", "no",
3504, "f", 2021, "Abr", "fuente_2", "no",
88.4, "g", 2023, "Jun", "fuente_1", "no",
0, "h", 2023, "Set", "fuente_1", "no",
0, "i", 2023, "Jul", "fuente_1", "no",
29.4, "j", 2021, "Jun", "fuente_2", "si"
)
# tbl_test
wb_test <- wb_workbook() |>
wb_add_worksheet(sheet = "Resumen") |>
wb_add_worksheet(sheet = "Detalle") |>
wb_add_data(sheet = "Detalle",
x = tbl_test)
# wb_test
data_to_pivot <- wb_data(wb_test, sheet = "Detalle")
# data_to_pivot
vars_to_slicers <- c("char_2", "char_3")
wb_test$
add_pivot_table(
x = data_to_pivot,
sheet = "Resumen",
dims = "B10",
rows = "char_1",
cols = c("año", "mes"),
data = "Total",
slicer = vars_to_slicers,
params = list(
name = "pivot_1"
)
)$
add_slicer(
x = data_to_pivot,
dims = "B2:D6",
sheet = "Resumen",
varname = "char_2",
pivot_table = "pivot_1"
)$
add_slicer(
x = data_to_pivot,
dims = "F2:I6",
sheet = "Resumen",
varname = "char_3",
pivot_table = "pivot_1"
)
if (interactive()) wb_test$open() |
e1fd1a9
to
25eceba
Compare
Hi there. I ran the code after "This should work now" and it says: Does it has somethind to do with the fact that the arguments in I've tried the following (from reading your first example): tribble(
~Total, ~char_1, ~año, ~mes, ~char_2, ~char_3,
1.21, "a", 2023, "Mar", "fuente_1", "no",
5, "b", 2023, "Ene", "fuente_1", "si",
0.24, "c", 2021, "May", "fuente_2", "no",
6.3, "d", 2022, "Jun", "fuente_1", "no",
0.3, "e", 2022, "Jul", "fuente_2", "no",
3504, "f", 2021, "Abr", "fuente_2", "no",
88.4, "g", 2023, "Jun", "fuente_1", "no",
0, "h", 2023, "Set", "fuente_1", "no",
0, "i", 2023, "Jul", "fuente_1", "no",
29.4, "j", 2021, "Jun", "fuente_2", "si"
) -> tbl_test
tbl_test
wb_test <- wb_workbook() |>
wb_add_worksheet(sheet = "Resumen") |>
wb_add_worksheet(sheet = "Detalle") |>
wb_add_data(sheet = "Detalle",
x = tbl_test)
data_to_pivot <- wb_data(wb_test,sheet = "Detalle")
data_to_pivot
vars_to_slicer <- c("char_2", "char_3")
vars_to_slicer
wb_test$add_pivot_table(
x = data_to_pivot,
sheet = "Resumen",
dims = "B10",
rows = "char_1",
cols = c("año", "mes"),
data = "Total",
pivot_table = "pivot_1", # CHANGE HERE
slicer = vars_to_slicer
)$add_slicer(
x = data_to_pivot,
dims = "B2:D6",
slicer = "char_2", # CHANGE HERE (no more varname)
sheet = "Resumen",
pivot_table = "pivot_1"
)$add_slicer(
x = data_to_pivot,
dims = "F2:I6",
sheet = "Resumen",
slicer = "char_3", # CHANGE HERE (no more varname)
pivot_table = "pivot_1"
) This returns |
25eceba
to
a6feba0
Compare
😵💫, typo. Your example should work now. |
Yes, sorry, I've updated the API afterwards and forgot to update the code above. But now your code works and you can even play around a bit with various slicer options. Like descing sort order or multiple columns etc. |
Now, this is awesome. It can replace small projects that require basic user interface (Shiny). Awesome public good! 👏 |
🎉 But please do some more tests with different files, different column types etc.! And please test not only that the files open with Excel without warning, but create the pivot table and slicers from Excel and from |
* `pivot_table` is always the name of the pivot table * `slicer` is always a name of a slicer variable
…e cell is provided, create a cell range. use editAs = "oneCell". Similar to what redmonds spreadsheet software uses.
a6feba0
to
92daf08
Compare
Hi there, @JanMarvin I noticed something while using the slicers iris_2 <- iris |> as_tibble() |>
rename("Petal Length" = "Petal.Length") # "Split" the column name (Common for presentation purposes)
iris_2
wb_1 <- wb_workbook() |>
wb_add_worksheet(sheet = "x") |>
wb_add_data(x = iris_2,
sheet = current_sheet()) |>
wb_add_worksheet(sheet = "y")
tbl_to_pivot <- wb_1 |> wb_data("x")
wb_1 |>
wb_add_pivot_table(
x = tbl_to_pivot,
sheet = "y",
rows = "Species",
data = "Petal.Width",
pivot_table = "pivot_1",
slicer = "Petal Length"
) |>
wb_add_slicer(
x = tbl_to_pivot,
pivot_table = "pivot_1",
slicer = "Petal Length"
) Excel returns the following error: Records removed: /xl/slicerCaches/slicerCache1.xml In the mean time, I've been using the caption argument and my variable without spaces |
Thanks for the report, I've added a fix in #842 |
Early draft.wb$pivotDefinitions
. If the variable is not available Excel in MS365 on Mac simply crashes.can not yet add two slicers to a pivot table.need awb_add_slicer()
function.cannot yet select a condition (something like: i currently wont add thisam = 1
)cannot modify the slicer columns/rowscolumnCount="3" style="SlicerStyleLight2"
Something like "draw the rest of the f**king owl" ... this works as intended
Update:
After some cleanup now both slicer and pivot table use
pivot_table
as argument andslicer
for the variable used for slicing.