默认情况下,使用闪亮的DataTable中的数千个分隔符渲染大数字。
文档读(对我来说)应该已经显示了千位分隔符。我是否遇到兼容性问题,或者我是否需要在代码中添加更多内容,如果是,那又怎么样?
[更新] 还有另一个SO question尝试解决闪亮的预格式化数据的排序。问题尚未解决,但建议另一条路线 - 事先格式化。这是一条可取的路线,如果是这样,如何正确解决其他OP的排序问题?
我正在努力改善表格的输出以提高可读性。我已成功使用另一个SO question来冻结标题,但我正在努力使数字格式正确。
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
} );
}")
)
)
})
)
查看DataTable文档,看起来就像设置语言一样。数千就足够了:
数据表&#39;内置数字格式化程序(formatNumberDT)用于 格式化表信息中使用的大数字。
默认情况下,DataTables将使用中指定的字符 language.thousandsDT(反过来,默认情况下,这是一个逗号)作为 千位分隔符。
在formatNumber中它有一个可以添加的功能,但是,这对我来说是一个新手,所以我不确定如何在现有代码的上下文中使用这个函数。
$('#example').dataTable( {
"formatNumber": function ( toFormat ) {
return toFormat.toString().replace(
/\B(?=(\d{3})+(?!\d))/g, "'"
);
};
} );
Chrome:版本39.0.2171.71 m
答案 0 :(得分:10)
我知道这是一个相当古老的帖子,但是今天也在努力解决这个问题,所以认为其他人可能会从这个解决方案中获得价值。
自提出问题以来,DT
包由RStudio(Github或CRAN)发布。
乍一看,如何格式化数字并不是很明显,因为它们不是像formatNumber
那样的formatCurrency
函数。解决它的方法是使用formatCurrency
并将货币参数设置为空。示例如下。
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, '')
)
}
)
)
答案 1 :(得分:6)
问题format
和prettyNum
是这些函数产生字符而不是数字的事实。因此,无法为渲染表设置样式。
本例中的解决方案可能如下:
output$tab <- renderDataTable({
datatable(mytable) %>%
formatCurrency(columns = ..., currency = "", interval = 3, mark = ",") %>%
formatStyle(
columns = ...,
color = styleInterval(myThreshold, c("black", "red"))) })
答案 2 :(得分:2)
尝试将传递给renderDatatable()
的df格式化为数千格式
df$x <- format(df$x, big.mark=',', scientific=FALSE)
或者
df$x<-prettyNum(df$x, big.mark = ",")