Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
800 views
in Technique[技术] by (71.8m points)

format - Add comma to numbers every three digits in datatable (R)

Suppose my data looks like this:

df1 = data.frame(A=c(1000000.51,5000.33), B=c(0.565,0.794))

I want to use DataTables and have column A be (1,000,001 ; 5,000)

library(DT)
datatable(df1)  %>%  formatPercentage('B', 2) %>%
  formatRound('A',digits = 0)

I know i can use scales

library(scales)    
comma_format()(1000000)

but I'm not sure how to combine that with DataTables

Thanks!

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

Had this same issue:

Try This:

require(DT)
require(dplyr)

df1 = data.frame(A=c(1000000.51,5000.33, 2500, 251), B=c(0.565,0.794, .685, .456))

df1 <- df1 %>% mutate(A=round(A,digits=0))

datatable(df1)  %>%  formatPercentage('B', 2) %>%
  formatCurrency('A',currency = "", interval = 3, mark = ",")

enter image description here


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

2.1m questions

2.1m answers

60 comments

57.0k users

...