Change number format in renderDataTable

How to determine the number format for datatable in Shiny? I would like to display 2 decimal places only for some columns, but I do not understand where this should be defined in my application. In server.R or ui.R ? In server.R this is 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 the 2nd and 3rd columns to display only two decimal digits?

+5
source share
1 answer

just use the 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 the fnRowCallback option in renderDataTable if you insist on storing data with a lot of digits and just change the output view:

  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 have to 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) );}")) 
+5
source

Source: https://habr.com/ru/post/1211654/


All Articles