I've got a rpivottable generated with this code thanks to rpivotTable
package in R
:
library("rpivotTable")
library("dplyr")
library("reshape2")
dane <- melt(HairEyeColor)
rpivotTable(dane,
rows = c("Hair", "Eye"),
cols = c("Sex"),
vals = "value",
aggregatorName = "Integer Sum",
locale = "en",
rendererName = "Table With Subtotal",
subtotals = TRUE)
which looks like that:
It is sorted alphabetically. And I would like to sort it in descending order using Total sum of values.
I can try sth like that:
library("rpivotTable")
library("dplyr")
library("reshape2")
dane <- melt(HairEyeColor)
sorter <- paste0("function(attr) {",
"var sortAs = $.pivotUtilities.sortAs;",
"if (attr == \"Eye\") { return sortAs([\"",
dane %>% group_by(Eye) %>% summarise(i = sum(value)) %>% arrange(-i) %>% .$Eye %>% paste(collapse = "\", \""),
"\"]); }",
"if (attr == \"Hair\") { return sortAs([\"",
dane %>% group_by(Hair) %>% summarise(i = sum(value)) %>% arrange(-i) %>% .$Hair %>% paste(collapse = "\", \""),
"\"]); }",
"}")
rpivotTable(dane,
rows = c("Hair", "Eye"),
cols = c("Sex"),
vals = "value",
aggregatorName = "Integer Sum",
locale = "en",
rendererName = "Table With Subtotal",
subtotals = TRUE,
sorters = sorter)
Than I get this:
and this is sorted by the 'outside' group. And I would like to have it sorted by both groups like here:
Is it possible in rpivotTable
package?