Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Change number format in renderDataTable

Tags:

r

shiny

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?

like image 821
Tomas Greif Avatar asked Jan 21 '15 20:01

Tomas Greif


1 Answers

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) );}"))
like image 62
Mahdi Jadaliha Avatar answered Sep 27 '22 18:09

Mahdi Jadaliha