4

How can I get R chunk code to produce the same number of digits as r inline code in RStudio RMarkdown file?

For example:

```{r}
x=1:30
sigma.sq=sum((x-mean(x))^2)/30
sigma.sq    
```

Thus, $\sigma^2=`r sigma.sq`$.

The chunk produces the number 74.92, but the inline code produces the number 74.9167. I'd like both to be the same.

Suggestions?

D.

P.S. Here's a screen shot of what the above code produces.

enter image description here

David
  • 981
  • 1
  • 15
  • 27
  • OK, how? For example, how do you set it for the entire document? Secondly, how do you override it in a particular chunk? – David Nov 11 '13 at 02:41
  • 2
    The default hook function for inline output in knitr is approximately `round(x, getOption('digits'))`; it is different from R's default printing behavior, which is kind of "mysterious" (or I just confess I do not understand how `print()` exactly works; it is affected by a number of factors). You can customize the `inline` hook in knitr to mimic the `print()` function, if you can figure out how it works. Also see http://stackoverflow.com/q/15313224/559676 and http://stackoverflow.com/a/11065102/559676 – Yihui Xie Nov 11 '13 at 06:10

1 Answers1

3

round() function gives you an answer. For example, the below inline code in R Markdown would display the second decimal place of the number of the object.

`r round(sigma.sq, 2)`
maech
  • 85
  • 9