Render large numbers as with thousands separators in a shiny DataTable by default.
The documentation reads (to me) like it should alr
Try formatting the df you pass to renderDatatable()
with thousands formatting
df$x <- format(df$x, big.mark=',', scientific=FALSE)
Or
df$x<-prettyNum(df$x, big.mark = ",")
I know this is quite an old post, however was struggling with this today too so thought that someone else may get value from this solution.
Since the question was asked the DT
package was released by RStudio (Github or CRAN).
At first glance it isn't too obvious how to format numbers as their isn't a corresponding formatNumber
function like their is formatCurrency
. The way around it is to use the formatCurrency
and just set the currency parameter to nothing. Example below.
library(shiny)
library(DT)
runApp(
list(ui = fluidPage(
DT::dataTableOutput("mytable")
),
server = function(input, output, session) {
output$mytable <- DT::renderDataTable(
DT::datatable(iris*1000,
options = list(pageLength = 50,
columnDefs = list(list(className = 'dt-left',
targets = 0:4)))) %>%
formatCurrency(1:4, '')
)
}
)
)
Problem with format
an prettyNum
is the fact that these functions produce characters instead of numerics. Thus, it is then impossible to style your rendered table.
A solution may be the following with this example :
output$tab <- renderDataTable({
datatable(mytable) %>%
formatCurrency(columns = ..., currency = "", interval = 3, mark = ",") %>%
formatStyle(
columns = ...,
color = styleInterval(myThreshold, c("black", "red"))) })