英文:
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'
)
答案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"
)
英文:
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 <- 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"
)
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。



评论