更改renderDataTable中的数字格式

时间:2015-01-21 20:56:32

标签: r shiny

如何在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列以仅显示两位小数?

1 个答案:

答案 0 :(得分:5)

只需使用round命令:

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

如果您坚持使用更多数字保存数据并且仅更改输出中的表示,您还可以在fnRowCallback函数中使用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,
      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) );}"))