Esta es una pregunta de seguimiento a esta pregunta: Agregar Total/Subtotal al final de un DataTable en Shiny con el siguiente fragmento de código:
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)En mi marco de datos tengo columnas que contienen dobles con dos dígitos después de la coma. El subtotal se muestra como algo como esto:
1234,51999999999 en lugar de 1234,52.
¿Qué tengo que cambiar en el código de Javascript para obtener solo dos dígitos después de la coma, los separadores de miles como un punto y el punto decimal como una coma y también, un símbolo de Euro (€) al lado de la suma?
¿Por qué quieres hacer esto en JavaScript si estás usando shiny? Esta es la forma R:
paste0( formatC(1234.51999999999, format="f", big.mark=".", decimal.mark = ",", digits=2), "€" ) # [1] "1.234,52€"O use JS para hacer el trabajo:
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; var sum1 = api.column(5).data().reduce(function(a, b) { return a + b; }); sum1 = Intl.NumberFormat('de-DE', { style: 'currency', currency: 'EUR' }).format(sum1) $(api.column(5).footer()).html('SubTotal: ' + sum1) } " ) ) output$display <- DT::renderDataTable(container = sketch, extensions = "Buttons", options = opts, { mtcars }) } shinyApp(ui = ui, server = server)