【问题标题】:Change number format in renderDataTable更改 renderDataTable 中的数字格式
【发布时间】:2015-01-21 20:56:31
【问题描述】:

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

【问题讨论】:

标签: r shiny


【解决方案1】:

只需使用圆形命令:

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

【讨论】:

  • 请您更新您的第二个版本以使用新的 DT 1.10 函数 rowCallback,因为这不再适用于我。
  • 请按照升级指南datatables.net/upgrade/1.10-convert 更改您的DataTables 参数名称。看来您需要将fnRowCallback 更改为rowCallback。我没有可以更改和测试的工作示例。
  • 我从指南中尝试过,也摆脱了 iDisplay... 参数并将 nRow 更改为行并将 aData 更改为数据。不幸的是仍然没有为我工作:-(
  • 根据我的更新更改回调函数。您应该在 java 脚本回调中包含 parseFloat 函数。显然数据在新版本中作为文本而不是数字传递。
  • 谢谢,当您记得在添加额外列后更改 ind 时效果很好....oops
猜你喜欢
  • 2020-07-01
  • 1970-01-01
  • 1970-01-01
  • 2021-02-15
  • 1970-01-01
  • 1970-01-01
  • 2023-04-08
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多