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

Can derived calculations refer to each other? #57

Open
cbailiss opened this issue Aug 18, 2020 · 0 comments
Open

Can derived calculations refer to each other? #57

cbailiss opened this issue Aug 18, 2020 · 0 comments

Comments

@cbailiss
Copy link
Owner

Is it possible to separate the Percent calculation below into two separate derived calculations?

library(pivottabler)

pt <- PivotTable$new()
pt$addData(mtcars)
pt$addColumnDataGroups("am")
pt$addRowDataGroups("vs")
pt$addRowDataGroups("mpg", totalCaption= "Subtotal") 
pt$addRowDataGroups("disp", addTotal=FALSE)

# basic calculation
pt$defineCalculation(calculationName="CountVal", summariseExpression="n()", visible=FALSE)

# get column totals
filterOverrides <- PivotFilterOverrides$new(pt, keepOnlyFiltersFor="am")
pt$defineCalculation(calculationName="CountAll", summariseExpression="n()", filters=filterOverrides, visible=FALSE)

# percentage of column calculation total
pt$defineCalculation(calculationName="Percent", caption="Percent",
                     type="calculation", basedOn=c("CountVal", "CountAll"),
                     calculationExpression="paste0(values$CountVal, ' (', sprintf('%.1f %%', values$CountVal/values$CountAll*100), ')')")

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