Aim
Render large numbers as with thousands separators in a shiny DataTable by default.
Problem
The documentation reads (to me) like it should already be showing thousand separators. Am I suffering compatibility issues or do I need to add more to my code, and if so what and how?
[UPDATE] There is another SO question that attempts to resolve sorting of pre-formatted data in shiny. The question is unresolved but suggests another route - formatting beforehand. Is this an advisable route and if so, how to resolve correctly the other OP's issues with sorting?
Background
I am trying to improve the output of a table in shiny to improve readability. I have successfully used another SO question to freeze the headers but am struggling to get numbers formatted correctly.
MWE
library(shiny)
runApp(
list(ui = fluidPage(
tagList(
singleton(tags$head(tags$script(src='//cdn.datatables.net/fixedheader/2.1.2/js/dataTables.fixedHeader.min.js',type='text/javascript'))),
singleton(tags$head(tags$link(href='//cdn.datatables.net/fixedheader/2.1.2/css/dataTables.fixedHeader.css',rel='stylesheet',type='text/css')))
),
dataTableOutput("mytable")
)
, server = function(input, output, session){
output$mytable <- renderDataTable(iris*1000,
options = list(
pageLength = 50,
language.thousands=",",
initComplete = I("function(settings, json){
new $.fn.dataTable.FixedHeader(this, {
left: true
} );
}")
)
)
})
)
Documentation
Looking at the DataTable documentation it appears like setting the language.thousands should be sufficient:
DataTables' built in number formatter (formatNumberDT) is used to format large numbers that are used in the table information.
language.thousands
By default DataTables will use the character specified in language.thousandsDT (in turn, that, by default, is a comma) as the thousands separator.
formatNumber
In formatNumber it has a function that could be added, however, this is very new to me so I'm unsure exactly how this function would be utilised in the context of the existing code.
$('#example').dataTable( {
"formatNumber": function ( toFormat ) {
return toFormat.toString().replace(
/\B(?=(\d{3})+(?!\d))/g, "'"
);
};
} );
Browser details
Chrome: Version 39.0.2171.71 m