I came across your question looking for the solution, and I believe I have found a way to do what you want. I know this question is over 2 years old but hopefully it will be found by someone who needs a solution and deemed helpful.
To sort by the "vals" AND also by another metric such as "cols" or "rows", requires a simple inclusion of another "if" statement within the sorters function.
Using the reproducible example provided by the developer as a starting point:
library(rpivotTable)
data(HairEyeColor)
rpivotTable(data = HairEyeColor, rows = "Hair",cols="Eye", vals = "Freq", aggregatorName = "Sum", rendererName = "Table", sorters = "
function(attr) {
var sortAs = $.pivotUtilities.sortAs;
if (attr == \"Hair\") { return sortAs([\"Red\", \"Brown\", \"Blond\", \"Black\"]); }
}", width="100%", height="400px")
Your next step is to add another if (attr == '\"\"
) statement after the closing bracket after the hair color. Let's say for example you also wanted to sort by Eye Color, with the color Brown listed first instead of Blue. You would write the following code to accomplish this:
library(rpivotTable)
data(HairEyeColor)
rpivotTable(data = HairEyeColor, rows = "Hair",cols="Eye", vals = "Freq",
aggregatorName = "Sum", rendererName = "Table", sorters = "
function(attr) {
var sortAs = $.pivotUtilities.sortAs;
if (attr == \"Hair\") { return sortAs([\"Red\", \"Brown\", \"Blond\", \"Black\"]);}
if (attr == \"Eye\") { return sortAs([\"Brown\", \"Blue\", \"Green\", \"Hazel\"]);}
}", width="100%", height="400px")
Remember that this package is developed around PivotTable.js, so you can include the functionality of the $.pivotUtilities.sortAs
function within your code to do other sorting, such as ordering ascending and descending. Refer to the PivotTable.js documentation