Max Wen
Max Wen

Reputation: 767

Formatting values in knitr output

My script runs in RStudo and the output shows up in the console window with the number formatted like this: 13367566000

However when I run KnitHTML the numbers are formatted like this: 1.337e+10

How do I set formatting for knitr output so that I can get the result formatted as a dollar amount? (eg, $13,367,566,000). If this isn't simple, I would at least like it expanded like it printed out in RStudio

Thank you

Upvotes: 1

Views: 800

Answers (1)

Kun Ren
Kun Ren

Reputation: 4993

You can try my package formattable which has a built-in function currency to make a formattable object as a wrapper of numeric vector.

> # devtools::install_github("renkun-ken/formattable")
> library(formattable)
> currency(c(123456,2345,12345))
[1] $123,456.00 $2,345.00   $12,345.00 

The currency symbol can be changed:

> currency(c(123456,2345,12345), "HK$")
[1] HK$123,456.00 HK$2,345.00   HK$12,345.00 

Upvotes: 2

Related Questions