Shiny的DataTable实现中的控制数字格式 [英] Control number formatting in Shiny's implementation of DataTable

查看:204
本文介绍了Shiny的DataTable实现中的控制数字格式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

目标



默认情况下,使用闪亮的数据表中的数千个分隔符渲染大量数字。



问题



文档对我来说就像应该已经显示了千个分隔符一样。我是否正在遇到兼容性问题,还是需要在代码中添加更多内容?如果是,怎么办?如何?



[UPDATE] 另一个


默认情况下,DataTables将使用
语言中指定的字符.thousandsDT(反过来,默认情况下是逗号)作为
千位分隔符。


formatNumber



在formatNumber中,它具有可以添加的功能,但是,这对我来说是很陌生的,因此我不确定在现有代码的上下文中如何使用此功能。

  $('#example')。dataTable({
formatNumber:function(toFormat){
return toFormat.toString()。replace(
/ \B(?= (\d {3})+(?! \d))/ g,'
);
};
});



浏览器详细信息



Chrome浏览器:版本39.0 .2171.71 m

解决方案

我知道这是一篇很老的文章,但是今天也为此而苦苦挣扎,以为其他人可能会从此解决方案中获取价值。



由于提出了问题, DT 软件包由RStudio发布( Github CRAN )。
乍一看,如何格式化数字不是很明显,因为它们不是相应的 formatNumber 函数,就像它们是 formatCurrency 。解决方法是使用 formatCurrency 并将货币参数设置为空。下面的示例。

 库(发光)
库(DT)
runApp(
列表(ui = fluidPage(
DT :: dataTableOutput( mytable)
),
服务器=函数(输入,输出,会话){
output $ mytable<-DT :: renderDataTable(
DT :: datatable(iris * 1000,
options = list(pageLength = 50,
columnDefs = list(list(className ='dt-left',
目标= 0:4))))%>%
formatCurrency(1:4,'')

}



Aim

Render large numbers as with thousands separators in a shiny DataTable by default.

Problem

The documentation reads (to me) like it should already be showing thousand separators. Am I suffering compatibility issues or do I need to add more to my code, and if so what and how?

[UPDATE] There is another SO question that attempts to resolve sorting of pre-formatted data in shiny. The question is unresolved but suggests another route - formatting beforehand. Is this an advisable route and if so, how to resolve correctly the other OP's issues with sorting?

Background

I am trying to improve the output of a table in shiny to improve readability. I have successfully used another SO question to freeze the headers but am struggling to get numbers formatted correctly.

MWE

library(shiny)
runApp(
  list(ui = fluidPage(
    tagList(
      singleton(tags$head(tags$script(src='//cdn.datatables.net/fixedheader/2.1.2/js/dataTables.fixedHeader.min.js',type='text/javascript'))),
      singleton(tags$head(tags$link(href='//cdn.datatables.net/fixedheader/2.1.2/css/dataTables.fixedHeader.css',rel='stylesheet',type='text/css')))
    ), 

    dataTableOutput("mytable")
  )
  , server = function(input, output, session){
    output$mytable <- renderDataTable(iris*1000,
                                      options = list(
                                        pageLength = 50,
                                        language.thousands=",",
                                        initComplete = I("function(settings, json){
                                          new $.fn.dataTable.FixedHeader(this, {
                                            left:   true
                                          } );
                                        }")
                                      )
    )
  })
)

Documentation

Looking at the DataTable documentation it appears like setting the language.thousands should be sufficient:

DataTables' built in number formatter (formatNumberDT) is used to format large numbers that are used in the table information.

language.thousands

By default DataTables will use the character specified in language.thousandsDT (in turn, that, by default, is a comma) as the thousands separator.

formatNumber

In formatNumber it has a function that could be added, however, this is very new to me so I'm unsure exactly how this function would be utilised in the context of the existing code.

$('#example').dataTable( {
  "formatNumber": function ( toFormat ) {
    return toFormat.toString().replace(
      /\B(?=(\d{3})+(?!\d))/g, "'"
    );
  };
} );

Browser details

Chrome: Version 39.0.2171.71 m

解决方案

I know this is quite an old post, however was struggling with this today too so thought that someone else may get value from this solution.

Since the question was asked the DT package was released by RStudio (Github or CRAN). At first glance it isn't too obvious how to format numbers as their isn't a corresponding formatNumber function like their is formatCurrency. The way around it is to use the formatCurrency and just set the currency parameter to nothing. Example below.

library(shiny)
library(DT)
runApp(
  list(ui = fluidPage(
    DT::dataTableOutput("mytable")
  ),
  server = function(input, output, session) {
    output$mytable <- DT::renderDataTable(
      DT::datatable(iris*1000, 
                    options = list(pageLength = 50, 
                                   columnDefs = list(list(className = 'dt-left', 
                                                          targets = 0:4)))) %>% 
        formatCurrency(1:4, '')
    )
  }
  )
)

这篇关于Shiny的DataTable实现中的控制数字格式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆