Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Set global thousand separator on knitr

I want all the numbers on my knitr report to be formatted as such by default:

format(num, digits = 2, big.mark = " ", decimal.mark = ",")

Defaulting the number of digits to 2 and the decimal mark to comma is easy, I just need to set options(digits = 2, OutDec = ",") in my first R chunk. However, I don't see how I can set the thousand separator to " " (or anything else, for that matter) in that format. I've also tried tweaking opts_chunk, but can't get it to work.

Of course, I'm trying to avoid having to insert format() inside every output, inline or otherwise. More intelligent formatting is one thing that drove me towards knitr from Sweave, after all.

How can I set default thousand separator marks on knitr?

like image 246
Waldir Leoncio Avatar asked Sep 23 '13 17:09

Waldir Leoncio


1 Answers

As Frank noted, setting a knitr hook such as the following solves the problem:

knit_hooks$set(inline = function(x) {
  prettyNum(x, big.mark=" ")
})

It turns out knitr hooks are a great way to tweak the output of R chunks on knitr. It's really worth it to take a look at http://yihui.name/knitr/hooks.

Source: https://groups.google.com/forum/#!msg/knitr/CnFwvk1Qn1E/WY-Xhf7Ph3AJ

like image 179
Waldir Leoncio Avatar answered Oct 23 '22 23:10

Waldir Leoncio