How to format data columns using renderDataTable () in a DT package?

I can format the column of a datatable object like this

library(DT)
datatable(data.frame(Amount=c(1.00, 2.20, 4.15))) %>% formatCurrency(columns='Amount')

enter image description here

But how to do it using renderDataTable()?

library(shiny)
library(DT)

ui <- shinyUI(fluidPage(
  DT::dataTableOutput('dtoMyTable')
))

server <- shinyServer(function(input, output){
  output$dtoMyTable <- DT::renderDataTable({
    data.frame(Amount=c(1.00, 2.20, 4.15))
  })
})

shinyApp(ui = ui, server = server)
+4
source share
1 answer

Read the help page at DT::renderDataTable:

renderDataTable (expr, ...

expression
expression to create a table widget (usually via datatable ()) or a data object to be passed to datatable () to create a table widget

So, first create an object datatable, format as you wish, then call renderDataTable.

server <- shinyServer(function(input, output){
  dt <- datatable(data.frame(Amount=c(1.00, 2.20, 4.15))) %>% 
    formatCurrency(columns='Amount')
  output$dtoMyTable <- DT::renderDataTable({dt})
})
+4
source

Source: https://habr.com/ru/post/1650832/


All Articles