如何在 HTML 小部件 rpivotTable 中预选列或行变量的变量值?

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

我正在使用非常有趣的 html 小部件 rpivotTable。我知道如何预选作为行或列添加到数据透视表的变量,但我真正需要的是预选这些变量的某些值。

作为我的意思的示例,我使用 vignette 页面中的代码:

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

如果我想要,例如预选 "Hair" 变量的值 "Red",是否可以在此脚本中执行此操作?类似于:

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

我知道这行不通,但这是可行的方法吗?

是的,如果我没理解错的话,这可以用inclusionsexclusions来完成。虽然格式有点古怪,但要求所有内容都是 list.

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