DT RowGroup extension – (total) sum per column

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

DT RowGroup extension - (total) sum per column

问题

DT提供了一个RowGroup扩展

  1. output$my_table2 <- DT::renderDataTable({
  2. datatable(iris[1:100, c(1:5)],
  3. rownames = FALSE,
  4. extensions = 'RowGroup',
  5. options = list(rowGroup = list(dataSrc=c(4))
  6. )
  7. )})

是否可能显示额外的行或组内行中的每列的所有值的总和(不需要很多JS代码)?这主要是一个与R Shiny DT相关的可视化问题/问题(在使用RowGroup时)。

首选可视化效果(额外行):
DT RowGroup extension – (total) sum per column

组内行中的总和(例如mpg的总和为132.8):
DT RowGroup extension – (total) sum per column

英文:

DT provides a RowGroup extension.

  1. output$my_table2 <- DT::renderDataTable({
  2. datatable(iris[1:100, c(1:5)],
  3. rownames = FALSE,
  4. extensions = 'RowGroup',
  5. options = list(rowGroup = list(dataSrc=c(4))
  6. )
  7. )})

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

Prefered visualization (extra row):
DT RowGroup extension – (total) sum per column

Sum within row group (.e.g. total = 132.8 for mpg):
DT RowGroup extension – (total) sum per column

答案1

得分: 1

以下是您要翻译的代码部分的翻译:

  1. # 计算按 'cyl' 水平的 'mpg' 的小计
  2. byFactor <- "cyl"
  3. variable <- "mpg"
  4. subtotals <- sapply(split(mtcars, mtcars[[byFactor]]), function(dat) {
  5. sum(dat[[variable]])
  6. })
  7. n <- length(subtotals)
  8. # 将 'mtcars' 的行名称移到第一列
  9. dat0 <- mtcars
  10. dat0 <- cbind(car = rownames(mtcars), mtcars)
  11. # 将小计附加到此数据框 'dat0' 上
  12. totals <- matrix(NA, nrow = n, ncol = ncol(dat0))
  13. colnames(totals) <- colnames(dat0)
  14. dat <- rbind(dat0, totals)
  15. dat_tail <- (nrow(dat) - n + 1L):nrow(dat) # 新数据的行索引
  16. dat[dat_tail, "car"] <- "Total"
  17. dat[dat_tail, variable] <- subtotals
  18. dat[dat_tail, byFactor] <- names(subtotals)
  19. # 按 'cyl' 列对 'dat' 进行排序
  20. dat <- dat[order(dat[[byFactor]]), ]
  21. # 现在创建数据表
  22. library(DT)
  23. datatable(
  24. dat, rownames = FALSE,
  25. extensions = "RowGroup",
  26. options = list(
  27. rowGroup = list(dataSrc = list(2))
  28. )
  29. )

为了得到小计的粗体行,您可以使用行回调:

  1. # 提取要加粗的行的索引
  2. indices <- which(dat[["car"]] == "Total") - 1L # 减去1是为了适应 JavaScript!
  3. cb <- JS(
  4. "function(row, data, displayNum, index) {",
  5. sprintf("var indices = [%s];", toString(indices)),
  6. " if(indices.indexOf(index) > -1) {",
  7. " $(row).css('font-weight', 'bold');",
  8. " }",
  9. "}"
  10. )
  11. library(DT)
  12. datatable(
  13. dat, rownames = FALSE,
  14. extensions = "RowGroup",
  15. options = list(
  16. rowCallback = cb,
  17. rowGroup = list(dataSrc = list(2))
  18. )
  19. )

奖励:

  1. # 我还会在行组标题前加上 'cyl:',并隐藏 'cyl' 列
  2. dat[[byFactor]] <- paste0(byFactor, ": ", dat[[byFactor]])
  3. datatable(
  4. dat, rownames = FALSE,
  5. extensions = "RowGroup",
  6. options = list(
  7. rowCallback = cb,
  8. rowGroup = list(dataSrc = list(2)),
  9. columnDefs = list(
  10. list(targets = 2, visible = FALSE)
  11. )
  12. )
  13. )

请注意,这些翻译是代码的翻译,不包括问题的回答。如果您需要进一步的解释或信息,请提出相关问题。

英文:

A possible way:

  1. # compute subtotals of &#39;mpg&#39; by level of &#39;cyl&#39;
  2. byFactor &lt;- &quot;cyl&quot;
  3. variable &lt;- &quot;mpg&quot;
  4. subtotals &lt;- sapply(split(mtcars, mtcars[[byFactor]]), function(dat) {
  5. sum(dat[[variable]])
  6. })
  7. n &lt;- length(subtotals)
  8. # move the rownames of &#39;mtcars&#39; to first column
  9. dat0 &lt;- mtcars
  10. dat0 &lt;- cbind(car = rownames(mtcars), mtcars)
  11. # append the subtotals to this dataframe &#39;dat0&#39;
  12. totals &lt;- matrix(NA, nrow = n, ncol = ncol(dat0))
  13. colnames(totals) &lt;- colnames(dat0)
  14. dat &lt;- rbind(dat0, totals)
  15. dat_tail &lt;- (nrow(dat)-n+1L):nrow(dat) # the row indices of the new data
  16. dat[dat_tail, &quot;car&quot;] &lt;- &quot;Total&quot;
  17. dat[dat_tail, variable] &lt;- subtotals
  18. dat[dat_tail, byFactor] &lt;- names(subtotals)
  19. # sort &#39;dat&#39; by the &#39;cyl&#39; column
  20. dat &lt;- dat[order(dat[[byFactor]]), ]
  21. # now make the datatable
  22. library(DT)
  23. datatable(
  24. dat, rownames = FALSE,
  25. extensions = &quot;RowGroup&quot;,
  26. options = list(
  27. rowGroup = list(dataSrc = list(2))
  28. )
  29. )

DT RowGroup extension – (total) sum per column


Edit

To get bold rows for the totals, you can use a row callback:

  1. # extract indices of the rows to be bolded
  2. indices &lt;- which(dat[[&quot;car&quot;]] == &quot;Total&quot;) - 1L # subtract 1 for JavaScript!
  3. cb &lt;- JS(
  4. &quot;function(row, data, displayNum, index) {&quot;,
  5. sprintf(&quot;var indices = [%s];&quot;, toString(indices)),
  6. &quot; if(indices.indexOf(index) &gt; -1) {&quot;,
  7. &quot; $(row).css(&#39;font-weight&#39;, &#39;bold&#39;);&quot;,
  8. &quot; }&quot;,
  9. &quot;}&quot;
  10. )
  11. library(DT)
  12. datatable(
  13. dat, rownames = FALSE,
  14. extensions = &quot;RowGroup&quot;,
  15. options = list(
  16. rowCallback = cb,
  17. rowGroup = list(dataSrc = list(2))
  18. )
  19. )

Bonus:

  1. # I would also prepend &#39;cyl:&#39; to the rowgroup headers and hide the &#39;cyl&#39; column
  2. dat[[byFactor]] &lt;- paste0(byFactor, &quot;: &quot;, dat[[byFactor]])
  3. datatable(
  4. dat, rownames = FALSE,
  5. extensions = &quot;RowGroup&quot;,
  6. options = list(
  7. rowCallback = cb,
  8. rowGroup = list(dataSrc = list(2)),
  9. columnDefs = list(
  10. list(targets = 2, visible = FALSE)
  11. )
  12. )
  13. )

DT RowGroup extension – (total) sum per column

huangapple
  • 本文由 发表于 2023年3月31日 17:44:59
  • 转载请务必保留本文链接:https://go.coder-hub.com/75897047.html
匿名

发表评论

匿名网友

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

确定