Search code examples
rhtmlwidgetsrpivottable

How to preselect a variable value of column or row variables in HTML widget rpivotTable?


I am using the very interesting html widget rpivotTable. I know how to preselect variables that are added as rows or columns to the pivottable, but what I really need is a preselection of certain values of these variables.

As an example of what I mean I use the code from the vignette page:

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"
)

If I want, e.g. to preselect the value "Red" of the "Hair" variable, is it possible to do that in this script? Something like:

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 select([\"Red\"]); }
  }", width = "100%", height = "400px"
)

I know this doesn't work but is it the way to go?


Solution

  • Yes, if I understand correctly, this can be accomplished with inclusions and exclusions. The format is a little funky though and requires everything to be a list.

    library(rpivotTable)
    data(HairEyeColor)
    rpivotTable(
      data = HairEyeColor,
      rows = "Hair",
      cols="Eye",
      vals = "Freq",
      aggregatorName = "Sum",
      rendererName = "Table",
      inclusions = list(
        "Hair" = list("Red")
      )
    )