| 1 |
This is a special R script which can be used to generate a report. You can |
| 2 |
write normal text in roxygen comments. |
| 3 |
|
| 4 |
First we set up some options (you do not have to do this): |
| 5 |
|
| 6 |
```{r setup, include=FALSE} |
| 7 |
library(knitr) |
| 8 |
opts_chunk$set(fig.path = 'figure/silk-') |
| 9 |
``` |
| 10 |
|
| 11 |
The report begins here. |
| 12 |
|
| 13 |
```{r test-a, cache=FALSE} |
| 14 |
# boring examples as usual |
| 15 |
set.seed(123) |
| 16 |
x = rnorm(5) |
| 17 |
mean(x) |
| 18 |
``` |
| 19 |
|
| 20 |
You can use the special syntax {{code}} to embed inline expressions, e.g. |
| 21 |
`r mean(x) + 2` |
| 22 |
is the mean of x plus 2. |
| 23 |
The code itself may contain braces, but these are not checked. Thus, |
| 24 |
perfectly valid (though very strange) R code such as `{{2 + 3}} - {{4 - 5}}` |
| 25 |
can lead to errors because `2 + 3}} - {{4 - 5` will be treated as inline code. |
| 26 |
|
| 27 |
Now we continue writing the report. We can draw plots as well. |
| 28 |
|
| 29 |
```{r test-b, fig.width=5, fig.height=5} |
| 30 |
par(mar = c(4, 4, .1, .1)); plot(x) |
| 31 |
``` |
| 32 |
|
| 33 |
Actually you do not have to write chunk options, in which case knitr will use |
| 34 |
default options. For example, the code below has no options attached: |
| 35 |
|
| 36 |
```{r } |
| 37 |
var(x) |
| 38 |
quantile(x) |
| 39 |
``` |
| 40 |
|
| 41 |
And you can also write two chunks successively like this: |
| 42 |
|
| 43 |
```{r test-chisq5} |
| 44 |
sum(x^2) # chi-square distribution with df 5 |
| 45 |
``` |
| 46 |
```{r test-chisq4} |
| 47 |
sum((x - mean(x))^2) # df is 4 now |
| 48 |
``` |
| 49 |
|
| 50 |
Done. Call spin('knitr-spin.R') to make silk from sow's ear now and knit a |
| 51 |
lovely purse. |