更改 renderDataTable 中的数字格式

Change number format in renderDataTable

如何在 Shiny 中定义数据表的数字格式?我只想为某些列显示 2 个十进制数字,但不明白应该在我的应用程序中定义它的位置。在 server.Rui.R 中?在 server.R 中,这是我在 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)
  ) 

如何设置第 2 列和第 3 列的格式以仅显示两位小数?

只需使用圆形命令:

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

如果您坚持要保留更多数字的数据并且只是更改输出中的表示形式,您也可以在 renderDataTable 函数中使用 fnRowCallback 选项:

 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) );}"))
  ) 

DT 1.1 更新:

你应该改变

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

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