DT in Shiny and R:自定义数字格式 [英] DT in Shiny and R: Custom number formatting

查看:92
本文介绍了DT in Shiny and R:自定义数字格式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个闪亮的应用程序,它使用 DT -软件包显示数据表。我想要的是能够以自定义方式设置列格式。例如,我希望这样显示货币值:1,234.50€,而不是 DT -way,它会像这样显示$ 1,234.5(请注意,符号货币符号的位置以及小数点后的数字。)

I have a shiny-app that displays a datatable using the DT-package. What I want is to be able to format columns in a custom way. For example I want a currency value to be displayed like this: 1,234.50€ instead of the DT-way, which displays it like this $1,234.5 (notice the change in the symbol, the position of the currency-symbol as well as the numbers after the decimal-point).

MWE看起来像这样:

An MWE looks like this:

library(shiny)
library(DT)

shinyApp(
  # UI
  ui = fluidPage(DT::dataTableOutput('tbl')),

  # SERVER
  server = function(input, output) {
    dat <- data.frame(cur = 1234.5, # supposed to be displayed as: 1,234.50€ | Bad! 
                                         # displayed as $1,234.5
                      perc = 0.123456, # 12.34% | Good!
                      num = 1000) # 1,000 | Bad! displayed as 1000

    # render DT
    output$tbl = DT::renderDataTable(
      datatable(dat) %>%
        formatCurrency(c('cur'), "$") %>%
        formatPercentage('perc', 2) %>%
        formatRound('num', digits = 0)
    )
  }
)

它做得相当不错,但是,当将货币符号更改为时,该符号消失了。当插入另一个字符,如 E时,该字符仍显示在开头而不是结尾。此外,数值不会获得大标记。

It does a fairly good job, however, when changing the currency-symbol to , the symbol disappears. When inserting another character like "E", the character is still displayed at the beginning not at the end. Furthermore, the numeric value does not get a "big-mark".

有什么想法吗?

推荐答案

您可以从数据表包中更改货币符号在.js文件中的位置。

You can change the position of the currency symbol in the .js file from the datatable package.

编辑DTWidget.formatCurrency行函数

Edit the line of the DTWidget.formatCurrency function

 $(thiz.api().cell(row, col).node()).html(currency + markInterval(d, interval, mark));

简单地

 $(thiz.api().cell(row, col).node()).html(markInterval(d, interval, mark) + currency);

至于€符号,

formatCurrency(c('cur'), currency = "\U20AC", interval = 3, mark = ",", digits = 2)

对我有用吗,那就是您尝试过的事情,您没有看到任何符号吗?

does work for me, thats what you tried and you don't see any symbol?

这篇关于DT in Shiny and R:自定义数字格式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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