Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Try to replicate Sum aggregator
library(dplyr)
library(htmlwidgets)
library(rpivotTable)
mtcars %>%
rpivotTable(rows = "gear",
cols = c("cyl", "carb"),
vals = "hp",
# Step 1. Make this line accomplish the same thing as the default
# (in other words, it should really have no effect).
#
# Step 2. Fold in `numberOfDigits`.
#
# Step 3. Celebrate!
#
aggregators = htmlwidgets::JS('{ Sum : $.pivotUtilities.aggregatorTemplates.sum() }'),
aggregatorName = "Sum")
@timelyportfolio
Copy link

timelyportfolio commented Sep 26, 2016

This is far from obvious, so very good question. Here is a (hacky) way to accomplish.

library(dplyr)
library(htmlwidgets)
library(rpivotTable)

rpvt <- mtcars %>%
  rpivotTable(
    rows = "gear",
    cols = c("cyl", "carb"),
    vals = "hp",

    # Step 1. Make this line accomplish the same thing as the default
    #         (in other words, it should really have no effect).
    #
    # Step 2. Fold in `numberOfDigits`.
    #
    # Step 3. Celebrate!
    #
    aggregators = htmlwidgets::JS('{Sum:$.pivotUtilities.aggregatorTemplates.sum()}')
  )

# rpivotTable auto boxes everything except inclusions and exclusions
#  https://github.com/smartinsightsfromdata/rpivotTable/blame/master/R/rpivotTable.R#L110-L117
#  so we need to unbox or unlist
rpvt$x$params$aggregators <- rpvt$x$params$aggregators[[1]]

rpvt

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