简体   繁体   English

动态列数量的DT Shiny中的GrandTotal

[英]GrandTotal in DT Shiny with dynamic column quantity

How to add total row in DT in Shiny app like in example 如何像示例中一样在Shiny应用程序的DT中添加总行

在此处输入图片说明

I exlplored some topic here, but how to add total in Mean_price column, it's calculate total Turnover / total Qty 我在这里讨论了一些主题,但是如何在Mean_price列中添加总计,它计算的是总营业额/总数量

How to add total if column quantity in DT dynamicaly changed? 如果DT中的列数动态更改,如何添加总数?

Welcome to SO! 欢迎来到SO!

Here is a solution using library(data.table) : 这是使用library(data.table)的解决方案:

library(data.table)
library(DT)

ui <- basicPage(
  h2("Grand total"),
  DT::dataTableOutput("mytable")
)

server <- function(input, output) {

  DT <- data.table (Product = paste("Item", seq(10)), Turnover = round(runif(10, 1000, 3000)), Qty=round(runif(10, 100, 120)), Mean_price=round(runif(10, 10, 30), digits = 2))
  totalDT <- as.data.table(c(Product = "Total", DT[, lapply(.SD, sum, na.rm=TRUE), .SDcols=c("Turnover", "Qty")]))
  totalDT[, "Mean_price" := round(Turnover/Qty, digits = 2)]

  myContainer = htmltools::withTags(table(
    tableHeader(DT),
    tableFooter(as.character(totalDT))
  ))

  output$mytable = DT::renderDataTable({
    DT::datatable(DT, options = list(pageLength = nrow(DT)), rownames = FALSE, container = myContainer)
  })
}

shinyApp(ui, server)

See this for row specific styling. 请参阅为行特定的造型。

Edit, after further specification of the desired output (footer): you don't need a callback-function to create a footer, please see this . 在进一步指定所需的输出(页脚)之后,进行编辑:您不需要回调函数即可创建页脚,请参见this

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM