将总计/小计添加到Shiny的数据表的底部 [英] Adding Total/Subtotal to the bottom of a DataTable in Shiny

查看:53
本文介绍了将总计/小计添加到Shiny的数据表的底部的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我希望能够在下面创建的数据框的底部添加总计/小计,以数据表的形式显示。我希望使用每个DataTable过滤器来更新总计/小计。假设用户过滤了前10条记录,我希望总数计算前10条记录的总和,或者如果用户过滤20条记录,我希望总数反映20条记录的总和,依此类推。



我想过一种在 R 中实现此目标的方法,方法是将新行绑定到现有数据框以执行所需的计算。但是我想知道是否有一种简单的方法可以通过DataTables选项实现这一目标。类似于


I would like to be able to add a Total/Subtotal at the bottom of the dataframe created below, displayed as a DataTable. I would like the Total/Subtotal to be updated with every DataTable filter applied. Let's say if the user filters the first 10 records, I would like the total to calculate the sum of the first 10 records or if the user filters 20 records, I would like the total to reflect the sum of the 20 records and so on.

I have thought of a way to achieve this in R by binding a new row to the existing dataframe to carry out the desired calculation. But I was wondering if there's an easy way to attain this through DataTables options. Something similar to this example using the Footer Callback feature.

Also, please click here and here to help out with similar questions.

 #Load required packages

require(shiny)

#Create a dataframe
df <- data.frame(random=1:25)

server <- function(input,output,session){

    #Display df using DataTable and apply desired options
    output$display <- renderDataTable({df})
}

ui <- shinyUI(fluidPage(

    #Add a title
    h1('Testing TableTools'),

        mainPanel(
           #Display results
           dataTableOutput('display')
                 )      


))

shinyApp(ui = ui, server = server)

解决方案

Here is an example how to do the SubTotal of the given page. To get the total you can pre-compute it and then maybe paste it into the JS output?

library(shiny)
library(DT)

ui <- shinyUI(fluidPage(
  h1('Testing TableTools'),
  mainPanel(
    dataTableOutput('display')
  )      
))

Names <- c("",names(mtcars))
FooterNames <- c(rep("",4),Names[5:6],rep("",6))

server <- function(input, output, session) {

  sketch <- htmltools::withTags(table(
    tableHeader(Names),tableFooter(FooterNames)
  ))

  opts <- list(
    dom = 'Bfrtip', buttons = list('colvis','print',list(extend='collection',text='Download',buttons = list('copy','csv','excel','pdf'))),
               footerCallback = JS(
                 "function( tfoot, data, start, end, display ) {",
                 "var api = this.api(), data;",
                 "$( api.column(5).footer()).html('SubTotal:  '+",
                 "api.column(5).data().reduce( function ( a, b ) {",
                 "return a + b;",
                 "} )",
                 ");",
                 "$( api.column(4).footer()).html('SubTotal: '+",
                 "api.column(4).data().reduce( function ( a, b ) {",
                 "return a + b;",
                 "} )",
                 ");","}")
  )

  output$display <- DT::renderDataTable(container = sketch,extensions = 'Buttons',options = opts,{
    mtcars
  })
}

shinyApp(ui = ui, server = server)

这篇关于将总计/小计添加到Shiny的数据表的底部的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆