英文:
DT RowGroup extension - (total) sum per column
问题
DT提供了一个RowGroup扩展。
output$my_table2 <- DT::renderDataTable({
datatable(iris[1:100, c(1:5)],
rownames = FALSE,
extensions = 'RowGroup',
options = list(rowGroup = list(dataSrc=c(4))
)
)})
是否可能显示额外的行或组内行中的每列的所有值的总和(不需要很多JS代码)?这主要是一个与R Shiny DT相关的可视化问题/问题(在使用RowGroup时)。
英文:
DT provides a RowGroup extension.
output$my_table2 <- DT::renderDataTable({
datatable(iris[1:100, c(1:5)],
rownames = FALSE,
extensions = 'RowGroup',
options = list(rowGroup = list(dataSrc=c(4))
)
)})
It it possible to display, an extra row or within the group row, the (total) sum of all values per column (without a lot of JS lines)? It is primarily a visualization R Shiny DT question/ issue (while using RowGroup).
答案1
得分: 1
以下是您要翻译的代码部分的翻译:
# 计算按 'cyl' 水平的 'mpg' 的小计
byFactor <- "cyl"
variable <- "mpg"
subtotals <- sapply(split(mtcars, mtcars[[byFactor]]), function(dat) {
sum(dat[[variable]])
})
n <- length(subtotals)
# 将 'mtcars' 的行名称移到第一列
dat0 <- mtcars
dat0 <- cbind(car = rownames(mtcars), mtcars)
# 将小计附加到此数据框 'dat0' 上
totals <- matrix(NA, nrow = n, ncol = ncol(dat0))
colnames(totals) <- colnames(dat0)
dat <- rbind(dat0, totals)
dat_tail <- (nrow(dat) - n + 1L):nrow(dat) # 新数据的行索引
dat[dat_tail, "car"] <- "Total"
dat[dat_tail, variable] <- subtotals
dat[dat_tail, byFactor] <- names(subtotals)
# 按 'cyl' 列对 'dat' 进行排序
dat <- dat[order(dat[[byFactor]]), ]
# 现在创建数据表
library(DT)
datatable(
dat, rownames = FALSE,
extensions = "RowGroup",
options = list(
rowGroup = list(dataSrc = list(2))
)
)
为了得到小计的粗体行,您可以使用行回调:
# 提取要加粗的行的索引
indices <- which(dat[["car"]] == "Total") - 1L # 减去1是为了适应 JavaScript!
cb <- JS(
"function(row, data, displayNum, index) {",
sprintf("var indices = [%s];", toString(indices)),
" if(indices.indexOf(index) > -1) {",
" $(row).css('font-weight', 'bold');",
" }",
"}"
)
library(DT)
datatable(
dat, rownames = FALSE,
extensions = "RowGroup",
options = list(
rowCallback = cb,
rowGroup = list(dataSrc = list(2))
)
)
奖励:
# 我还会在行组标题前加上 'cyl:',并隐藏 'cyl' 列
dat[[byFactor]] <- paste0(byFactor, ": ", dat[[byFactor]])
datatable(
dat, rownames = FALSE,
extensions = "RowGroup",
options = list(
rowCallback = cb,
rowGroup = list(dataSrc = list(2)),
columnDefs = list(
list(targets = 2, visible = FALSE)
)
)
)
请注意,这些翻译是代码的翻译,不包括问题的回答。如果您需要进一步的解释或信息,请提出相关问题。
英文:
A possible way:
# compute subtotals of 'mpg' by level of 'cyl'
byFactor <- "cyl"
variable <- "mpg"
subtotals <- sapply(split(mtcars, mtcars[[byFactor]]), function(dat) {
sum(dat[[variable]])
})
n <- length(subtotals)
# move the rownames of 'mtcars' to first column
dat0 <- mtcars
dat0 <- cbind(car = rownames(mtcars), mtcars)
# append the subtotals to this dataframe 'dat0'
totals <- matrix(NA, nrow = n, ncol = ncol(dat0))
colnames(totals) <- colnames(dat0)
dat <- rbind(dat0, totals)
dat_tail <- (nrow(dat)-n+1L):nrow(dat) # the row indices of the new data
dat[dat_tail, "car"] <- "Total"
dat[dat_tail, variable] <- subtotals
dat[dat_tail, byFactor] <- names(subtotals)
# sort 'dat' by the 'cyl' column
dat <- dat[order(dat[[byFactor]]), ]
# now make the datatable
library(DT)
datatable(
dat, rownames = FALSE,
extensions = "RowGroup",
options = list(
rowGroup = list(dataSrc = list(2))
)
)
Edit
To get bold rows for the totals, you can use a row callback:
# extract indices of the rows to be bolded
indices <- which(dat[["car"]] == "Total") - 1L # subtract 1 for JavaScript!
cb <- JS(
"function(row, data, displayNum, index) {",
sprintf("var indices = [%s];", toString(indices)),
" if(indices.indexOf(index) > -1) {",
" $(row).css('font-weight', 'bold');",
" }",
"}"
)
library(DT)
datatable(
dat, rownames = FALSE,
extensions = "RowGroup",
options = list(
rowCallback = cb,
rowGroup = list(dataSrc = list(2))
)
)
Bonus:
# I would also prepend 'cyl:' to the rowgroup headers and hide the 'cyl' column
dat[[byFactor]] <- paste0(byFactor, ": ", dat[[byFactor]])
datatable(
dat, rownames = FALSE,
extensions = "RowGroup",
options = list(
rowCallback = cb,
rowGroup = list(dataSrc = list(2)),
columnDefs = list(
list(targets = 2, visible = FALSE)
)
)
)
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论