移除使用DT R包中的RowGroup扩展时的分组变量。

huangapple go评论53阅读模式
英文:

Remove grouping variable when using RowGroup extension in DT R package

问题

我正在尝试使用DT包中的RowGroup扩展。我能够创建行组,但现在我想要将该分组变量作为表格中的列移除(在两个地方都有它是多余的)。

在这个可重现的示例中,我正在按cyl分组,但cyl也出现在列中。显然,如果我从数据中删除它,就无法再进行分组了。

需要移除红色边框标记的列。

英文:

I'm trying to use the RowGroup extension in the DT package. I'm able to create the row groups but I now want to remove that grouping variable as a column in the table (it's redundant having it in two places).

In this reproducible example I'm grouping by cyl but cyl also appears as a column. Obviously if I remove it from the data I can't group anymore.

library(DT)

data_to_tabulate <- head(mtcars[order(mtcars$cyl), ])

datatable(
  data_to_tabulate,
  extensions = 'RowGroup',
  options = list(rowGroup = list(dataSrc = 2)),
  selection = 'none'
)

Need to remove the red bordered column.
移除使用DT R包中的RowGroup扩展时的分组变量。

答案1

得分: 1

你可以使用 columnDefs 选项通过设置 visible=FALSE 来隐藏列。对于你的示例,以下代码隐藏了第二列:

library(DT)

data_to_tabulate <- head(mtcars[order(mtcars$cyl), ])

datatable(
  data_to_tabulate,
  extensions = "RowGroup",
  options = list(
    rowGroup = list(dataSrc = 2),
    columnDefs = list(list(visible = FALSE, targets = c(2)))
  ),
  selection = "none"
)

移除使用DT R包中的RowGroup扩展时的分组变量。

英文:

You could use the columnDefs option to hide a column by setting visible=FALSE. For you example the following code hides the second column:

library(DT)

data_to_tabulate &lt;- head(mtcars[order(mtcars$cyl), ])

datatable(
  data_to_tabulate,
  extensions = &quot;RowGroup&quot;,
  options = list(
    rowGroup = list(dataSrc = 2),
    columnDefs = list(list(visible = FALSE, targets = c(2)))
  ),
  selection = &quot;none&quot;
)

移除使用DT R包中的RowGroup扩展时的分组变量。

huangapple
  • 本文由 发表于 2023年4月7日 01:22:42
  • 转载请务必保留本文链接:https://go.coder-hub.com/75952195.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定