You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Original suggestion below. Better suggestion (which doesn't require merging data into one df) is:
Add additional parameter to pt$addData() function that allows a list of totalsDfs to be passed.
Each totalDf is itself a list, i.e. a data frame plus a vector of column names that defines the dimensionality of the totalDf (i.e. so the pvt can work out where it can be used).
The grand totals should be provided in a list where the names of the list elements are the calculation names and the list elements are the grand total values for the calculation.
Since the above may lead to lists of lists that are hard to manage, may be easier to add a new pt$addTotals() function, which has parameters parentData, grain, totalsData
These are then used for displaying calculation totals.
Original suggestion below. Better suggestion (which doesn't require merging data into one df) is:
I.e. it should be possible to calculate the totals outside the pivot table then have the pivot table use them. This would mean the following approach would also work for the Stack Overflow question (after adding appropriate totals rows (NULL or *) to the summary2 data frame):
https://stackoverflow.com/questions/46349307/pivottabler-counting-small-ns-and-row-percentages-r/46349873#46349873
library(dplyr)
summary1 <- bhmtrains %>%
group_by(TOC) %>%
summarise(TrainCountAllTrainCategories = n()) %>%
ungroup()
summary2 <- bhmtrains %>%
group_by(TOC, TrainCategory) %>%
summarise(TrainCount = n()) %>%
ungroup() %>%
inner_join(summary1, "TOC") %>%
mutate(PercentageOfAllCategories = TrainCount / TrainCountAllTrainCategories * 100)
pt <- PivotTable$new()
pt$addData(summary2)
pt$addColumnDataGroups("TrainCategory")
pt$addRowDataGroups("TOC")
pt$defineCalculation(calculationName="N", type="value", valueName="TrainCount")
pt$defineCalculation(calculationName="Percent", caption="%",
type="value", valueName="PercentageOfAllCategories", format="%.1f %%")
pt$renderPivot()
The text was updated successfully, but these errors were encountered: