8

How do I define number format for datatable in Shiny? I would like to display 2 decimal digits only for some columns, but do not understand where it should be defined in my application. In server.R or ui.R? In server.R, this what I have in renderDataTable:

  output$woeTable <- renderDataTable({
    input$tryTree
    input$threshold
    # The following returns data frame with numeric columns
    get(input$dfDescr)[['variables']][[input$columns]][['woe']]
    },
    options=list(
      paging = FALSE,
      searching = FALSE)
  ) 

How do I format 2nd and 3rd column to display only two decimal digits?

Tomas Greif
  • 21,685
  • 23
  • 106
  • 155
  • See the answer here http://stackoverflow.com/a/31028061/2643104 using datatable() %>% formatRound() – queise Feb 05 '17 at 11:50

1 Answers1

6

just use round command:

  output$woeTable <- renderDataTable({
    input$tryTree
    input$threshold
    # The following returns data frame with numeric columns
    A = get(input$dfDescr)[['variables']][[input$columns]][['woe']]
    A[,2] = round(x = A[,2],digits = 2)
    A[,3] = round(x = A[,3],digits = 2)
    A
    },
    options=list(
      paging = FALSE,
      searching = FALSE)
  ) 

you can also use fnRowCallback option in renderDataTable function if you insist to keep data with more digit and just change representation in output:

 output$woeTable <- renderDataTable({
    input$tryTree
    input$threshold
    # The following returns data frame with numeric columns
    get(input$dfDescr)[['variables']][[input$columns]][['woe']]
    },
    options=list(
      paging = FALSE,
      searching = FALSE,
      fnRowCallback = I("function( nRow, aData, iDisplayIndex, iDisplayIndexFull ) {ind = 2; $('td:eq('+ind+')', nRow).html( (aData[ind]).toFixed(2) );}"))
  ) 

update for DT 1.1:

you should change

fnRowCallback = I("function( nRow, aData, iDisplayIndex, iDisplayIndexFull ) {ind = 2; $('td:eq('+ind+')', nRow).html( (aData[ind]).toFixed(2) );}"))

to

rowCallback = I("function( nRow, aData) {ind = 2; $('td:eq('+ind+')', nRow).html( parseFloat(aData[ind]).toFixed(2) );}"))
Mahdi Jadaliha
  • 1,947
  • 1
  • 14
  • 22
  • 1
    please can you update your second version to use the new DT 1.10 function rowCallback as this is no longer working for me. – nstjhp Mar 16 '15 at 18:44
  • Please follow the upgrade guide https://datatables.net/upgrade/1.10-convert to change your DataTables parameter names. It seems you need to change `fnRowCallback` to `rowCallback`. I don't have working example to change and test. – Mahdi Jadaliha Mar 16 '15 at 18:58
  • I tried that from the guide, also getting rid of the `iDisplay...` arguments and changing nRow to row and aData to data. Unfortunately still didn't work for me :-( – nstjhp Mar 16 '15 at 19:10
  • change the call back function according my update. you should include `parseFloat` function in java script callback. apparently data is passed as text not numbers in new version. – Mahdi Jadaliha Mar 16 '15 at 19:35
  • Thanks that works fine when you remember to change ind after you add extra columns....oops – nstjhp Mar 16 '15 at 20:10
  • For the latest version the code again needs an update. Here is what worked for me: `rowCallback = JS("function(nRow, aData) { ind = 2; $('td:eq('+ind+')', nRow).html(parseFloat(aData[colIndex]).toLocaleString('en', { maximumFractionDigits: 2 })); }")` – Angel Naydenov Jan 22 '16 at 13:08