在 Shiny 的 DataTable 实现中控制数字格式 [英] Control number formatting in Shiny's implementation of DataTable
问题描述
目标
默认情况下,在闪亮的 DataTable 中使用千位分隔符呈现大数字.
问题
文档读起来(对我来说)就像它应该已经显示千位分隔符一样.我是否遇到了兼容性问题,或者我是否需要向我的代码添加更多内容?如果是这样,该怎么办?
[UPDATE] 还有另一个
<块引用>默认情况下,DataTables 将使用指定的字符language.thousandsDT(反过来,默认情况下,它是一个逗号)作为千位分隔符.
在 formatNumber 中,它有一个可以添加的函数,但是,这对我来说很新,所以我不确定如何在现有代码的上下文中使用该函数.
$('#example').dataTable( {格式编号":函数( toFormat ){返回 toFormat.toString().replace(/B(?=(d{3})+(?!d))/g, "'");};});
浏览器详情
Chrome:版本 39.0.2171.71 m
我知道这是一个很老的帖子,但是今天也在努力解决这个问题,所以认为其他人可能会从这个解决方案中获得价值.
自从提出问题以来,DT
包由 RStudio 发布(Github 或 CRAN).乍一看,如何格式化数字并不是很明显,因为它们不是相应的 formatNumber
函数,就像它们的 formatCurrency
一样.解决方法是使用 formatCurrency
并将货币参数设置为空.示例如下.
图书馆(闪亮)图书馆(DT)运行应用程序(列表(用户界面 = 流体页面(DT::dataTableOutput("mytable")),服务器 = 函数(输入、输出、会话){输出$mytable <- DT::renderDataTable(DT::datatable(虹膜*1000,选项 = 列表(页面长度 = 50,columnDefs = list(list(className = 'dt-left',目标 = 0:4)))) %>%格式货币(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.
By default DataTables will use the character specified in language.thousandsDT (in turn, that, by default, is a comma) as the thousands separator.
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屋!