Skip to content
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

calculationType "value" should work with totals #2

Closed
cbailiss opened this issue Sep 21, 2017 · 0 comments
Closed

calculationType "value" should work with totals #2

cbailiss opened this issue Sep 21, 2017 · 0 comments

Comments

@cbailiss
Copy link
Owner

cbailiss commented Sep 21, 2017

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.
  • Allows user full control of the calculations

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()

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

1 participant