2

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:

enter image description here

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:

enter image description here

and this is sorted by the 'outside' group. And I would like to have it sorted by both groups like here:

enter image description here

Is it possible in rpivotTable package?

Marta
  • 3,032
  • 3
  • 17
  • 34

1 Answers1

0

I might learn more from your question than you learn from my answer, because I liked your clever mixing of dplyr and JavaScript. To restate your problem, you are specifying a single Eye sort list in your sorter function, but you really want a different Eye sort list depending on the Hair grouping. So your Eye sorting list:

library("dplyr")
library("reshape2")

dane <- melt(HairEyeColor)
dane %>% group_by(Eye) %>% summarise(i = sum(value)) %>% 
   arrange(-i) %>% .$Eye %>% paste(collapse = "\", \"")

...has this output:

"Brown\", \"Blue\", \"Hazel\", \"Green"

... and that's the same sort order used within each Hair grouping. See also this answer.

I'm not an expert in pivottable, but to do what you want, I think the sorter function would have to handle two attributes like [\"Hair\", \"Eye\"], not just one. I believe that a dplyr expression like this will get you to proper resulting two-dimensional list:

dane %>% group_by(Hair, Eye) %>% 
  summarise(hairEyeSum = sum(value)) %>% 
  ungroup() %>% 
  arrange( desc(hairEyeSum)) %>% 
  group_by( Hair) %>% 
  mutate( hairSum = sum(hairEyeSum)) %>% 
  arrange( desc(hairSum), desc(hairEyeSum)) %>%
  ungroup() %>% 
  transmute( hairEyeList = paste0( "[\"", Hair, "\",\"", Eye, "\"]")) 

Output:

# A tibble: 16 x 1
   hairEyeList            
   <chr>                  
 1 "[\"Brown\",\"Brown\"]"
 2 "[\"Brown\",\"Blue\"]" 
 3 "[\"Brown\",\"Hazel\"]"
 4 "[\"Brown\",\"Green\"]"
 5 "[\"Blond\",\"Blue\"]" 
 6 "[\"Blond\",\"Green\"]"
 7 "[\"Blond\",\"Hazel\"]"
 8 "[\"Blond\",\"Brown\"]"
 9 "[\"Black\",\"Brown\"]"
10 "[\"Black\",\"Blue\"]" 
11 "[\"Black\",\"Hazel\"]"
12 "[\"Black\",\"Green\"]"
13 "[\"Red\",\"Brown\"]"  
14 "[\"Red\",\"Blue\"]"   
15 "[\"Red\",\"Hazel\"]"  
16 "[\"Red\",\"Green\"]" 

But I came up short of getting the sorter function working.

Joel Buursma
  • 118
  • 6