30

Is there a way to avoid the function round() when using Sweave/knitr? It's really annoying to write round(,2) in every \Sexpr{}. I would like to have two decimal places through the whole document.

Thanks for your help
Macs

beginneR
  • 3,207
  • 5
  • 30
  • 52
  • IMHO you could set a [hook](http://yihui.name/knitr/hooks) for that. Or try [pander](http://daroczig.github.com/pander/) which does that automatically :) – daroczig Jun 16 '12 at 10:22
  • 1
    Do you want to limit the output to two digits max, or do you always want two digits, even if they are zero (`1.00`)? The first you should be able to achieve with setting `options( digits = 2 )` in your first chunk. – vaettchen Jun 16 '12 at 11:06
  • @vaettchen: setting `digits` to `2`, you would get `pi*100` as `314` which is a quite rough estimate. – daroczig Jun 16 '12 at 11:17
  • possible duplicate of [Programming R/Sweave for proper \Sexpr output](http://stackoverflow.com/questions/2486130/programming-r-sweave-for-proper-sexpr-output) – Waldir Leoncio Jul 29 '13 at 20:56

3 Answers3

41

If you have read the motivation of the knitr package, you probably know I'm the person who hates round() inside \Sexpr{} most. I suggested this to R core long time ago for Sweave but it was ignored, so I started my own package.

Although the answer by Josh O'Brien is absolutely correct, I should say you really only need options(digits = 2) with knitr, and there is no need to reset the default inline hook because the default hook respects this option.

Yihui Xie
  • 28,913
  • 23
  • 193
  • 419
  • 3
    Thanks for this hint. It's really a great relief. I'm quite a beginner in the work with R and LaTeX, but nevertheless the advantages over Sweave are fairly obvious. I think in a short time most people will have changed to knitr. – beginneR Jun 18 '12 at 14:52
15

In knitr, the inline hook can be used to process the output of \Sexpr{} statements. So, if you want to print just 2 digits after the decimal for inline code (while leaving the overall digits option alone) you can do so like this:

## First have a look at the default inline hook function
knit_hooks$get("inline")

## Then customize it to your own liking
inline_hook <- function(x) {
    if(is.numeric(x)) x <- round(x, 2)
    paste(as.character(x), collapse=", ")
}
knit_hooks$set(inline = inline_hook)
Josh O'Brien
  • 159,210
  • 26
  • 366
  • 455
  • 1
    +1 - neat, as this adds the abbility to distinguish between inline and chunk appearance ! – petermeissner May 21 '13 at 17:28
  • 1
    @PeterM -- Absolutely! [See baptiste's answer here](http://stackoverflow.com/questions/16405536/knitr-inline-chunk-options-no-evaluation-or-just-render-highlighted-code) for another (very useful) example of a customized inline hook. – Josh O'Brien May 21 '13 at 17:32
6

First, you perhaps want to use formatC instead of round to get two digits even when they are zero. There's not a great way to do this in Sweave; the best option is probably simply to make a new function with a short name that does the formatting you want; something like

p <- function(x) {formatC(x, format="f", digits=2)}

That at least saves a little bit of typing:

The answer is $\Sexpr{p(x)}$.

Unfortunately, without mucking about with a new Sweave driver, I don't think there's anything else to do (perhaps this is another reason to try knitr, which seems to be gaining ground fast). Sweave is doing this on what's inside your Sexpr statement, and then replacing the Sexpr call with the result.

as.character(eval(parse(text = cmd), envir = .GlobalEnv))

However, you can't just write a new version of as.character; aside from the issue that it might change behavior in other unexpected ways, because of the namespacing, it always calls the version in base even if there's another version in the global environment.

Aaron left Stack Overflow
  • 36,704
  • 7
  • 77
  • 142
  • It is so natural to think of dark voodoos with Sweave :) You know exactly where Sweave should change, and this is also what I suggested to R core (they can do better not only using `as.character`) and ignored by them. – Yihui Xie Jun 17 '12 at 15:51
  • Yes, dark voodoos indeed. I have a couple variant drivers sitting around that add certain functionality I need but they're a pain to write and maintain. I keep hearing good things about `knitr` and how it makes a lot of things easier. Hope to try it soon. Thanks for your work on it, @Yihui. – Aaron left Stack Overflow Jun 17 '12 at 18:48