Debian Codegraph @

inst/examples/knitr-spin.R

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