DT in Shiny and R: User Number Formatting

I have a brilliant application that displays data using a DT package. I want to be able to format columns in my own way. For example, I want the currency value to be displayed as follows: € 1,234.50 instead of DT -way, which displays it as it is $ 1,234.5 (note the change in the symbol, the position of the currency symbol, and also the numbers after the decimal point).

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

However, it works well, but when you change the currency symbol to symbol disappears. When you insert another character of type "E", the character is still displayed at the beginning, not the end. In addition, a numeric value does not receive a “big label”.

Any ideas?

+5
source share
1 answer

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

Edit the line of the DTWidget.formatCurrency function

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

just

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

in the DT / htmlwidgets / datatables.js file in your R-libraries directory.

As for the symbol €,

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

works for me, here's what you tried and you don't see any character?

+2
source

Source: https://habr.com/ru/post/1233868/


All Articles