SchlossLab / schtools

Schloss Lab Tools for Reproducible Microbiome Research 💩
http://www.schlosslab.org/schtools
Other
29 stars 11 forks source link

Round R Markdown numbers to 1 decimal place when >= 1 #24

Closed kelly-sovacool closed 3 years ago

kelly-sovacool commented 3 years ago

By request of @pschloss.

If a number is "nearly whole" (abs(x - round(x)) < .Machine$double.eps^0.5), it is rounded to zero decimal places. Otherwise, it is rounded to contain two significant digits if it is less than 1, or rounded to one decimal place if greater than or equal to 1.

Examples:

> inline_hook(0.0256)
[1] "0.026"
> inline_hook(.Machine$double.eps^0.5)
[1] "0.000000015"
> inline_hook(100000.08)
[1] "100,000.1"
> inline_hook(1.00000000000000000001)
[1] "1"
> inline_hook("this is a string")
[1] "this is a string"

I also refactored the code so the function that used to be called inline_hook() is now named format_numbers() and a new inline_hook() function simply calls both format_numbers() and paste_oxford_list().

courtneyarmour commented 3 years ago

these functions are soooooo useful!