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

时间:2016-02-01 09:31:57

标签: r htmlwidgets rpivottable

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

作为我的意思的一个例子,我使用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"
)

如果我想,例如预选价值"红色" "头发"变量,是否可以在此脚本中执行此操作?类似的东西:

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

我知道这不起作用,但它是要走的路吗?

1 个答案:

答案 0 :(得分:1)

是的,如果我理解正确,可以使用inclusionsexclusions完成此操作。格式虽然有点时髦,但要求所有内容都是list

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