Skip to content

Instantly share code, notes, and snippets.

@dholstius
Created September 25, 2016 21:35
Show Gist options
  • Save dholstius/89f0dd48bc1599f67a1c9592303b6ad4 to your computer and use it in GitHub Desktop.
Save dholstius/89f0dd48bc1599f67a1c9592303b6ad4 to your computer and use it in GitHub Desktop.
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

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