Replies: 2 comments 1 reply
-
Hi @vorpalvorpal, I moved your comment here, because it is not really related to the issue it was posted in, aside from the fact that it mentions another unimplemented feature. Because filtering for a case is not yet possible in But lets start from the top. A pivot table - I also didn't know them for the longest (and happiest? 😉 ) part of my life - is something like an interactive table. You add data to column and rows and the spreadsheet software will calculate what you want, frequencies, sums, means, etc. Indeed similar to certain Lets take some example: library(openxlsx2)
# "https://www.dcceew.gov.au/sites/default/files/documents/national-waste-database-2022.xlsx"
fl <- "~/Downloads/national-waste-database-2022.xlsx"
waste_db <- wb_load(fl)
wb <- wb_workbook()
wb$clone_worksheet(old = "Database 2022", new = "DB2022", from = waste_db)
## remove all the interactive parts and make the data digestable to openxlsx2
# the current selected dimensions cover the entire sheet A1:XFD1048576. This is
# a 1048576 x 16384 matrix wb_data() will have to import.
wb$worksheets[[1]]$dimension <- "<dimension ref=\"A1:Q71497\"/>"
# the data validation part is in here
wb$worksheets[[1]]$extLst <- character()
# we bail over something in here, maybe a column style we didn't clone?
wb$worksheets[[1]]$cols_attr <- character()
# since we removed the column attributes, we have to remove the sheet format
wb$worksheets[[1]]$sheetFormatPr <- character()
df <- wb_data(wb, sheet = "DB2022")
wb$add_pivot_table(df, filter = "Jurisdiction", row = "Category", col = "Fate", data = "Tonnes",
param = list(numfmts = c(formatCode = "#,###0")))
# looking great, opening fast
if (interactive()) wb$open() And now to answer your questions:
wb$pivotTables %>% xml_node("pivotTableDefinition", "pivotFields", "pivotField") or in xml:
This is the XML for the filter ( <pivotField axis="axisPage" multipleItemSelectionAllowed="1" showAll="0">
<items count="10">
<item h="1" x="0"/>
<item h="1" x="1"/>
<item x="2"/>
<item h="1" x="3"/>
<item h="1" x="4"/>
<item h="1" x="5"/>
<item h="1" x="6"/>
<item h="1" x="7"/>
<item h="1" x="8"/>
<item t="default"/>
</items>
</pivotField> So to get everything in order, you have to dig a bit into the pivot table creation code. Somewhere there is To make things a bit complicated, we have different data types: numeric, character and dates. And might require some logic to select single values, values ranges or negate our selections. Also, what people that use pivot tables like to do a lot: use slicers. The logic here is a bit different (have a look at #822 if you fancy). The slicer has another way of selecting elements, using
|
Beta Was this translation helpful? Give feedback.
-
Thanks for that! That is a great help! I'll see what I can put together. And yes, of course anything I come up with I won't include |
Beta Was this translation helpful? Give feedback.
Uh oh!
There was an error while loading. Please reload this page.
-
G'day Jan,
I don't even really understand what a pivot table is (as far as I can tell, its like
dplyr::summarise
?), but I need to re-create one with a filter for someone where the value of the filter is already selected. That is thefilter
arg towb_add_pivot_table
specifies thecol
part ofdplyr::filter(df, col == val)
, but not theval
part). I've made a test xlsx and unzipped it and had a poke around to try to figure out where the value is stored, but haven't managed to figure it out yet. I plan to look into this further however first I wanted to check in if:a. specifying the value to filter by is an already existing functionality of
openxlsx2
that I have just not discovered;b. assuming that it is not, if this is something that you think should go into the list of todos for pivot tables you set out above;
c. assuming that it is, if you have any pointers about where you think I might be able to find the xml setting; and
d. if you have any thoughts about what the API should look like for any PR that I put together. Another arg for
wb_add_pivot_table
calledfilter_value
?Originally posted by @vorpalvorpal in #820 (comment)
Beta Was this translation helpful? Give feedback.
All reactions