-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathexample-notebook.R
126 lines (104 loc) · 3.93 KB
/
example-notebook.R
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
#' ---
#' title: "The title --- example notebook"
#' author: "A. N. Other"
#' date: '`r format(Sys.Date(), "%B %d %Y")`'
#' output: html_document
#' ---
#' Extra commands:
#'
#' - output: pdf_document
#' - output: html_document
#'
#' allow you to lock in the pdf or html format so you don't need to click
#' when you build the notebook.
#'
#' Also note that three dashes --- gives a long text dash, as above,
#' and two dashes -- give a shortish one. You probably only want to
#' use one - to mean minus.
#'
#' Biggest Header
#' ==============
#' You can write any text you like after the #\' comment characters to describe
#' your program. If you're using *RStudio*, you can just press *Enter* every
#' time you get to the dotted vertical line in the window so that your R text
#' doesn't scroll off the page unnecessarily. You automatically get the right
#' comment characters so you can just carry on typing. However, there are lots
#' of things you can do to format the text.
#'
#' For instance, you can *italicise* and **bold** text easily if you want to.
#' Information on the some of the basic rules for writing text (not R) is at
#' http://daringfireball.net/projects/markdown/syntax though most stuff is
#' explained below too. This format is called **Markdown**, and it describes
#' everything that happens after the #\' symbols, though those symbols are
#' nothing to do with markdown, they are just how to get *Markdown* into *R*.
#'
#' You need to press enter twice to start a new paragraph, and when you're
#' starting a new style of text, like a list or a header you need to have a
#' blank comment line before it to make sure that the words don't appear in the
#' previous paragraph by accident
#'
#' A list: second level header
#' ---------------------------
#' 1. Lists are started by numbers (note that any number will do) or `-`,
#' `+` or `*`
#' 1. Write an equation by encapsulating an expression in `$`
#' 1. For instance:
#' - `$D = sin(q)$` results in $D = sin(q)$; and
#' - `$E = sin(q/10)$` gives $E = sin(q/10)$
#' 4. Note that you need three tabs to have a sub-list as above.
#'
#' ## A different second level: actually doing some R!
x <- 2
y <- (3 + x)^2
# R comments just appear as normal
y
# but outputs appear with ## before them.
#' Comments can also include R, so $y = (3+x)^2$ =
{{y}}
#' gives you the number calculated in R.
#'
#' ### Now some simple figures: third level header
q <- 0:100
D <- sin(q)
plot(q, D)
#' ### And a sub-figure of a specific size
#'
#+ fig.width=5, fig.height=5, fig.cap="Figure 2: some sine curves"
E <- sin(q/10)
plot(q, E, type='l')
lines(q, D, col=2, lty=2)
#' Sadly, figure and other options are poorly documented,
#' but we've provided some help sheets if you want to use them (not compulsory!)
#'
#' __________________________________________
#'
#' # Now some more complex (optional) maths
n <- 5
x = rnorm(n)
mean(x)
var(x)
# Here we calculate a couple of simple equations using the data
# We want to stop writing out the strange ## code before outputs
#+ comment=""
sum(x) / n
sum((x - mean(x))^2) / (n - 1)
#' And here are the equations we just calculated written out neatly:
#'
#' - mean(x) = $1/n \sum_{i=1}^n x_i$
#' - and for a bigger equation on its own line:
#' $$var(x) = \frac{1}{n-1} \sum_{j=1}^n (x_j - \frac{\sum_{i=1}^n x_i}{n})^2$$
#' $$\sum_{i=1}^n x_i$$
#'
#' We can also write stuff in greek and with accents if we need to:
#'
#' - $\hat{\gamma} = \bar{\alpha} \times \ddot{\beta}$
#'
#' Note that this complex syntax for equations is LaTeX -- or \LaTeX\ as
#' it's officially written -- if you haven't seen it before, you don't need
#' to use it, but there is lots of information here on writing formulae at
#' https://en.wikibooks.org/wiki/LaTeX/Mathematics, and there are some specific
#' things you can put in a formula
#' [here](https://en.wikipedia.org/wiki/Help:Displaying_a_formula#Formatting_using_TeX).
#'
# Note that the uncommented output stops after the next text block
mean(x) / var(x)