add a currency format for renderDataTable with proper sorting

1,304 views
Skip to first unread message

Dan

unread,
Oct 14, 2014, 4:03:34 PM10/14/14
to shiny-...@googlegroups.com
Hi, how do I generate a currency format in a table and get the correct sorting?  Here's a minimal example:

server.R
# load some data
mydata<-mtcars[,1:3]
# make up a cost variable
mydata$price <- runif(nrow(mydata),5000,35000)
# format as dollar using scales library
library(scales)
mydata$price <- dollar(mydata$price)
# Define a server for the Shiny app
shinyServer(function(input, output) {
  # Filter data based on selections
  output$table <- renderDataTable({
    mydata
  })
})

UI.R
shinyUI(
  fluidPage(
    # Create a new row for the table.
    fluidRow(
      dataTableOutput(outputId="table")
    )    
  )
)

In the data table, price does not sort correctly when you click on the column header.  The problem is that price is a character vector, so $99 > $200.  

Is there an option in datatables to format with dollars?  




Yihui Xie

unread,
Jan 2, 2015, 11:02:13 PM1/2/15
to Dan, shiny-discuss
You may use the new package DT to solve this problem now. See the
section "Format Columns": http://rstudio.github.io/DT/

Regards,
Yihui
Reply all
Reply to author
Forward
0 new messages