forked from nhs-r-community/intro_r
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path10-intro-rmarkdown.Rmd
181 lines (112 loc) · 4.74 KB
/
10-intro-rmarkdown.Rmd
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
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
---
title: "Introduction to RMarkdown"
date: "`r format(Sys.time(), '%d %B %Y')`"
output:
html_document:
code_download: yes
toc: true
toc_depth: 4
toc_float: true
---
```{r setup, include = FALSE}
library(knitr)
library(tidyverse)
opts_chunk$set(
echo = FALSE,
eval = FALSE,
message = FALSE,
warning = FALSE,
fig.width = 7.252,
fig.height = 4,
dpi = 300,
dev.args = list(type = "cairo")
)
```
# Knit early. Knit often.
## The following section makes more sense in the code than in the html
Insert an R code chunk by using the **insert** button above. Shortcut keys Ctrl+Alt+i should create a chunk but if not, this can be set by choosing your own shortcut in Tools/Keyboard Shortcuts Help.
A code chunk looks like this:
```{r}
# This won't show in the output file. Naming chunks can help to locate sections.
```
You can name a chunk (optional), like so
```{r name-of-chunk}
# Just ensure the names are different or it won't run. Spaces are ok, but not underscores between words
```
The next chunk should be familiar; it's the code to import the beds data. Code has been copied across but code can be written directly into the chunk.
```{r load-data}
beds_data <- read_csv(url("https://raw.githubusercontent.com/nhs-r-community/intro_r_data/main/beds_data.csv"), col_types = cols(date = col_date(format = "%d/%m/%Y")), skip = 3)
```
## Knit the file
By clicking on the drop down menu beside the **Knit** button, a document will be generated (note: first time you do this, you'll be prompted to name your file. Try "test_rmarkdown"). As PDF requires additional installations, we will knit to HTML (web page) for now, which is the default and also better for accessbility.
### Text
# Markdown formatting
## You can play with headers that get smaller
### You can play with headers - and smaller
#### You can play with headers - and smaller
##### You can play with headers - and smallest
You can make a line make a line break
***
You can do all the normal *italics* or _italics_ and **bold** and ^superscript^ and ~~strike-though~~
> Things like block quotes look for quotes
> works really well if
> split over several lines
Other things that are useful to code:
* lists
* putting stuff in lists
+ putting stuff in sub lists
+ such as this
* did I say lists?
You can also do numbered lists
(@) Like this
(@) Fine example of a list
+ and do sub lists in a numbered list
i) and do sub sub lists
A. and sub sub sub list
Write some stuff in the middle of your list
(@) and then go back to the list
(@) NOTE: the numbers in the list are not specified, they are dynamic and if you go and edit one out it will adjust them automatically
<style>
div.blue { background-color:#e6f0ff; border-radius: 5px; padding: 20px;}
</style>
<div class = "blue">
You can colour in a block to assist with highlighting an area.
- This is my first conclusion
- This is my second conclusion
</div>
You can add footnotes[^1]
[^1]:This is the footnote from the footnote added way way up above
### Plotting Mental Health bed availabilty
If the chunk is run (not knitted), the plot will be displayed in line with the code (i.e. below the chunk).
Note the button to the left of "play" will run all the previous chunks above the current one.
```{r beds-plot}
beds_ts <- beds_data %>%
group_by(date) %>%
summarise(
mean_beds = mean(beds_av, na.rm = T))
ggplot(beds_ts) +
geom_line(aes(date, mean_beds))+
geom_point(aes(date, mean_beds))
```
You may want to show your code, either in the html reports or presentation slides. As the settings have been set globally you can set any chunk to deviate from the global:
```{r options, echo=TRUE, eval=FALSE}
# echo=TRUE means show this code
# eval=FALSE means don't check this code. As it is a chunk that won't run correctly, eval=TRUE would result in an error.
ggplot(beds_ts) +
geom_line(aes(date, mean_beds)) +
geom_point(aes(date, mean_beds)) +
# This code hasn't been finished.
```
```{r options1}
ggplot(beds_ts) +
geom_line(aes(date, mean_beds))+
geom_point(aes(date, mean_beds))+
ylim(0, 350)
```
This chunk refers to the previous chunk code and shows the code but not the chart.
Just note the name of the chunk is made into a string with "" quotes.
```{r ref.label="options1", echo=TRUE, eval=FALSE}
# Comments don't show up in the html
```
#### Acknowledgements
I combined Andrew Jones's original [RMarkdown](https://github.com/nhs-r-community/intro_r/blob/master/intro_rmarkdown.Rmd) training report with some of Simon Wellesley-Miller's code for this RMarkdown example report. I thoroughly recommend running Simon's more detailed [RMarkdown](https://github.com/SimonW-M/Markdown/blob/main/R-Training-v5.Rmd) to learn more, as this is just a brief introduction.