Control Number Formatting in Shiny's Implementation of Datatable

Control number formatting in Shiny's implementation of DataTable

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, '')
)
}
)
)

DT and Shiny: Formatting numbers in a datatable with filters

Seems like yo forgot to use the datatable() function in your 3rd attempt. Here's what you need -

library(shiny)
library(dplyr)
library(DT)

# Define UI
ui <- fluidPage(
actionButton("start", "Click to Start"),
DTOutput('tbl3')
)

# Define Server
server = function(input, output) {

z <- eventReactive(input$start, {
iris %>% dplyr::filter(Species == "setosa")
})

output$tbl3 <- DT::renderDT({
datatable(z(), filter="top") %>%
formatPercentage(2:3, digits=2)
})
}

# Run the application
shinyApp(ui = ui, server = server)

Printing like a character but sorting like numeric in Shiny and DataTable

A bit late, but the DT Package now has format functions, including formatCurrency:

# format the columns A and C as currency, and D as percentages
datatable(m) %>% formatCurrency(c('A', 'C')) %>% formatPercentage('D', 2)

From the Functions page:

Under the hood, these formatting functions are just wrappers for the rowCallback option to generate the appropriate JavaScript code.
Similarly, there is a formatDate() function that can be used to format date/time columns. It has a method argument that takes values from a list of possible conversion methods: toDateString, toISOString, toLocaleDateString, toLocaleString, toLocaleTimeString, toString, toTimeString, toUTCString.

Custom sorting of R datatable column for numbers stored as strings

Section 4.5 - Row Rendering of this DT document has your answer:
https://rstudio.github.io/DT/options.html

How to format a Footer Total in datatables.net

Why not use Intl.NumberFormat?

new Intl.NumberFormat('en-US').format(api.column(7, { page: 'current' }).data().sum())

R Shiny renderDataTable show two decimal places and center align all data

output$Composite <- renderDataTable(datatable(FVI_DATA_COMPOSITE, 
options = list(pageLength = 10,lengthChange=FALSE)) %>% formatRound(c(3:9), 2)

Documentation here

Edit: To center align

output$Composite <- renderDataTable(datatable(FVI_DATA_COMPOSITE, 
options = list(pageLength = 10,lengthChange=FALSE)) %>%
formatRound(c(3:9), 2) %>%
formatStyle(columns = c(3:9), 'text-align' = 'center')


Related Topics



Leave a reply



Submit