I can format a column of a datatable object like so
library(DT)
datatable(data.frame(Amount=c(1.00, 2.20, 4.15))) %>% formatCurrency(columns='Amount')
But how can I do this 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)
The R package DT provides an R interface to the JavaScript library DataTables. R data objects (matrices or data frames) can be displayed as tables on HTML pages, and DataTables provides filtering, pagination, sorting, and many other features in the tables.
DT is an interface to the JavaScript library DataTables based on the htmlwidgets framework, to present rectangular R data objects (such as data frames and matrices) as HTML tables. You can filter, search, and sort the data in the table.
You can edit a table via the editable argument of datatable() . After you finish editing, you can obtain the row/column indices and the new values of the cells that were edited via input$tableId_cell_info .
data.table is an R package that provides an enhanced version of data.frame s, which are the standard data structure for storing data in base R. In the Data section above, we already created a data.table using fread() . We can also create one using the data.table() function.
Read the help page on DT::renderDataTable
:
renderDataTable(expr, ...
expr
an expression to create a table widget (normally via datatable()), or a data object to be passed to datatable() to create a table widget
So you just create the datatable
object first, 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})
})
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With