2017-03-21 14 views
3

データテーブルのフッターに合計を追加しようとしています。さまざまなソースからのコードを使用して、Shinyを使用して次のアプリケーションを作成しました。DT :: datatableに合計を追加する方法

"処理中..."

を、永遠にそこにとどまる:問題は、私はそれを実行すると、以下のメッセージが表示されます、です。

私の推測はJS()コードですが、それをデバッグすることはできません。

library(shiny) 
library(DT) 
library(htmltools) 

ui <- fluidPage(


    fluidRow(
    column(9, DT::dataTableOutput('withtotal')) 
) 

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

    # server-side processing 
    mtcars2 = mtcars[, 1:8] 
    #sketch <- htmltools::withTags(table(tableHeader(mtcars2), tableFooter(mtcars2))) 

    sketch = htmltools::withTags(table(tableFooter(c("",0,0,0,0,0,0,0)))) 

opts <- list(footerCallback = JS("function (row, data, start, end, display) {", 
    "var api = this.api();", 
    "var intVal = function (i) {", 
     "return typeof i === 'string' ?", 
     "i.replace(/[\\$,]/g, '')*1 :", 
     "typeof i === 'number' ?", 
     "i : 0;", 
    "};", 
    "if (api.column(COLNUMBER).data().length){", 
     "var total = api", 
     ".column(COLNUMBER)", 
     ".data()", 
     ".reduce(function (a, b) {", 
     "return intVal(a) + intVal(b);", 
     "}) }", 
    "else{ total = 0};", 
    "if (api.column(COLNUMBER).data().length){", 
     "var pageTotal = api", 
     ".column(COLNUMBER, { page: 'current'})", 
     ".data()", 
     ".reduce(function (a, b) {", 
     " return intVal(a) + intVal(b);", 
     "}) }", 
    "else{ pageTotal = 0};", 
    "$(api.column(COLNUMBER).footer()).html(", 
     "'$'+pageTotal", 
    ");", 
    "}")) 


    output$withtotal = DT::renderDataTable(DT::datatable(mtcars2,container = sketch, options = opts))  


} 

options(shiny.error = browser) 
# Run the application 
shinyApp(ui = ui, server = server) 
+0

あなたは[this](http://stackoverflow.com/a/42803610/5894457)のリンクを見ましたか? – SBista

+0

この投稿にコメントしてくれていない皆様ありがとうございます。http://stackoverflow.com/questions/42933859、私はTumbleweed賞を受賞しました:)この質問があまりにもスマートであるか、 – MPaydar

答えて

3

あなたはshiny DataTables footer Callback sums に私がやった同じ例のプログラムを使っているようなので、フッターのコールバック問題に対する私の解決策は以下の通りです見えます。これは、列単位で操作するのが最も簡単な方法です。

library(shiny) 
library(DT) 

ui <- fluidPage(

    title = 'Select Table Rows', 

    hr(), 

    h1('A Server-side Table'), 

    fluidRow(
    column(9, DT::dataTableOutput('x3')) 
) 

) 


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

    # server-side processing 

    mtcars2 = mtcars[, 1:8] 

    sketch <- htmltools::withTags(table(
        class = "display", 
        style = "bootstrap", 
        tableHeader(colnames(mtcars2)), 
        tableFooter(colnames(mtcars2)) 
     )) 

    output$x3 = DT::renderDataTable(DT::datatable(mtcars2, 
               container = sketch, 
               extensions = 'Buttons', 
               options = list(
                scrollX = TRUE, 
                scrollY = TRUE, 
                pageLength = 10, 
                order = list(list(1, 'asc')), 
                dom = 'Blrtip', 
                buttons = c('copy', 'csv', 'excel', 'pdf', 'print'), 
                footerCallback = JS(
     "function(tfoot, data, start, end, display) {", 
     "var api = this.api(), data;", 
     "total = api.column(1, { page: 'current'}).data().reduce(function (a, b) {return a + b;})", 
     "total1 = api.column(2, { page: 'current'}).data().reduce(function (a, b) {return a + b;})", 
     "total2 = api.column(3, { page: 'current'}).data().reduce(function (a, b) {return a + b;})", 
     "total3 = api.column(4, { page: 'current'}).data().reduce(function (a, b) {return a + b;})", 
     "total4 = api.column(5, { page: 'current'}).data().reduce(function (a, b) {return a + b;})", 
     "total5 = api.column(6, { page: 'current'}).data().reduce(function (a, b) {return a + b;})", 
     "total6 = api.column(7, { page: 'current'}).data().reduce(function (a, b) {return a + b;})", 
     "total7 = api.column(8, { page: 'current'}).data().reduce(function (a, b) {return a + b;})", 
     "$(api.column(1).footer()).html(total.toFixed(2)); 
     $(api.column(2).footer()).html(total1.toFixed(2)); 
     $(api.column(3).footer()).html(total2.toFixed(2)); 
     $(api.column(4).footer()).html(total3.toFixed(2)); 
     $(api.column(5).footer()).html(total4.toFixed(2)); 
     $(api.column(6).footer()).html(total5.toFixed(2)); 
     $(api.column(7).footer()).html(total6.toFixed(2)); 
     $(api.column(8).footer()).html(total7.toFixed(2));", 
     "}" 
     )) 
    )) 
} 

shinyApp(ui = ui, server = server) 
+0

ありがとうございます。それは私の問題を解決して解決します。 – MPaydar

関連する問題