Two things I’ve really missed from Excel, when I moved to using R, were conditional formatting and being able to format numbers as currency or percentages without losing data.
I wrote an
as.currency() function a while ago, but it turns numbers to strings, which means that I can no longer math those strings without coercing them back into numbers. And coercion is always scare. Do you want to lose data? Because that’s how you lose data.
The formattable package solves both these problems.
It’s got a couple functions,
currency() that only affect how numbers are PRINTED, so they come out looking proper even on the terminal, but they stay numeric so you can continue to use them as numbers.
It also has a host conditional formatting options, so you can change the text color of a column based on its values, add data bars (ie. built-in sparklines within the column itself) or format one column based on the values of another (something I’m pretty sure you can do in Excel, but I’ve never been able to figure out how).
Straight out of the vignette (because I want to get back to playing with this stuff, instead of writing this post):
<span class="kw">formattable</span>(products, <span class="kw">list</span>(
<span class="dt">price =</span> <span class="kw">color_tile</span>(<span class="st">"transparent"</span>, <span class="st">"lightpink"</span>),
<span class="dt">rating =</span> <span class="kw">color_bar</span>(<span class="st">"lightgreen"</span>),
<span class="dt">market_share =</span> <span class="kw">color_bar</span>(<span class="st">"lightblue"</span>),
<span class="dt">revenue =</span> sign_formatter,
<span class="dt">profit =</span> sign_formatter))