hughjonesd / huxtable

An R package to create styled tables in multiple output formats, with a friendly, modern interface.
http://hughjonesd.github.io/huxtable
Other
321 stars 28 forks source link

Formating number with decimal mark = "," and percent #209

Closed agustinggza closed 3 years ago

agustinggza commented 3 years ago

Before using this form, please read ?"huxtable-FAQ". In particular:

PLEASE DON'T USE GITHUB ISSUES FOR HELP REQUESTS.

Github issues are for bug reports and feature requests only. If you have a help request, post it to e.g. stackoverflow or community.rstudio.com, and drop me an email with the link. I will try to help out, and this will benefit other users. Help requests found here will be closed with a short response, which is not guaranteed to be polite.

OK? On with the show.

Describe what is missing What problem does this feature solve? Or, what would this feature enable the user to do that they can't do currently? Or, how would this feature make using huxtable easier?

Describe your proposed solution A clear and concise description of what you want to happen. Give code examples if that helps.

Target audience What set of people would this feature benefit?

Additional context Add any other context about the feature request here.

agustinggza commented 3 years ago

At first, thank you for your work; huxtable is very usefull.

Well, I'd like formating a number with % and decimal.mark=",".

I tried this:

Can you solve this one?

hughjonesd commented 3 years ago

You need to set options(OutDec=",") .

Which then reveals a real bug:

options(OutDec=",")
hux(1.51) %>% set_number_format("%2.3f %%")
##                                 1.000 %,51.000 %  
## 
## Column names: 1.51

hux(1.51) %>% set_number_format(fmt_percent(digits=2))
##                                 100,00%,5100,00%  
## 
## Column names: 1.51
hughjonesd commented 3 years ago

Working with OutDec turned on is pretty screwed up. In huxtable and possibly in R in general.

My best workaround for you is:

fmt_pct_comma <- list(function (x) paste0(prettyNum(x, decimal.mark=","), "%"))
hux(1.51) %>% set_number_format(fmt_pct_comma)
##                                                        1,51%  
## 
## Column names: 1.51
hughjonesd commented 3 years ago

Ok, using github master could you try

hux(1.51) %>% set_number_format(fmt_percent(decimal.mark = ","))

That should do what you want. See documentation for fmt_percent()