Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I format columns of a datatable using renderDataTable() in the DT package?

Tags:

r

dt

shiny

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

enter image description here

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)
like image 835
Ben Avatar asked Aug 10 '16 17:08

Ben


People also ask

What is DT package in R?

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.

What is DT in R studio?

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.

How do you edit a shiny 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 .

What is Datatable in R?

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.


1 Answers

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})
})
like image 105
dracodoc Avatar answered Nov 15 '22 17:11

dracodoc