forked from uqlibrary/technology-training
-
Notifications
You must be signed in to change notification settings - Fork 0
/
reports.Rmd
280 lines (176 loc) · 12.2 KB
/
reports.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
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
---
title: "R reproducible reports with R Markdown and knitr"
author: "Stéphane Guillou"
date: "`r Sys.Date()`"
output: github_document
---
```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE)
```
## Setting up
> If you don't have R and RStudio installed already, we have [installation instructions](/R/Installation.md#r--rstudio-installation-instructions)
* If you are using your own laptop please open RStudio
+ Make sure you have a working Internet connection
* On the Library's training computers:
+ Log in with your UQ username and password
+ Make sure you have a working Internet connection
+ Open the ZENworks application
+ Look for "RStudio"
+ Double click on RStudio, which will install both R and RStudio
With RStudio open, let's make sure we have the necessary packages installed by running this command (this might take a few minutes):
```{r install, eval=FALSE}
install.packages(c("tidyverse", "knitr", "plotly", "htmltools"))
```
This will install the Tidyverse packages, the knitr package to render reproducible reports, the plotly packages for interactive visualisations, and the htmltools for having all bases covered regarding rendering HTML documents.
> If R asks about installing a binary or building from source, **pick the binary option**: it will be faster!
## What are we going to learn?
R is a great tool to go from importing to reporting. Today, we focus on the "reporting" part.
Using R, RStudio, the R Markdown syntax and the knitr package, we can create **reproducible reports** that mix code and prose. If the underlying data changes, we only need to replace the original data file and "knit" the report once more, which updates all its contents in one click.
## Create a project and an R Markdown file
Use the project menu (top right) to **create a "New project..."**. Let's name this one "reports".
We also need to create a **new R Markdown file**: "File > New File > R Markdown...". We can change the title of the report, and the author as well. Let's stick to "HTML document" as an output for now.
## R Markdown and knitting
See how the document is already populated with a template? Scroll through and have a look at how it is structured. The three main elements are:
* a **YAML header** at the top, between the `---` tags;
* **Markdown** sections, where we can write prose, format text and add headers;
* and **code chunks**, in between ```` ``` ```` where we can write R code.
But before we edit this document, let's go straight to the **"knit" button** at the top of the source panel. Clicking that button will **compile** a document from the R Markdown file. You should see the process unfolding in the R Markdown tab, and the HTML document pop up in a separate window when it is finished.
See how the document contains a title, headers, code input and output, and explanations?
## Editing the document
Let's remove everything below our YAML header, and start writing our own report!
### Markdown syntax
To add a **header**, we can start a line with `##`: this will be a header of level 2. The number of hash symbols corresponds to the level of the header. See how the highlighting changes in the source editor?
We are going to deal with greenhouse gas emissions for Australia, so let's add a header and some text about the source of the data and importing it. For example:
```
## National Greenhouse Gas Inventory data
Our data comes from the NGA, and is released under a [CC-BY](https://creativecommons.org/licenses/by/4.0/) licence. The latest release can be found on [this page](https://www.industry.gov.au/data-and-publications/national-inventory-reports)
The values are reported in Mt CO<sub>2</sub>-e.
```
Notice how we used a `[text](link)` syntax to add a link to a website?
#### Challenge 1
We can also style our text by surrounding with other tags:
* `**` for **bold**
* `*` for *italic*
Try to style your text, and add a header of level 3 for a section on "importing the data". Knit the document to see if it works!
### R code chunks
We can now add a **code chunk** to include some R code inside our reproducible document. To add a code chunk, click the "Insert" button at the top of the source panel, and click "R". You can see that the language of the code chunk is defined at the top, with `{r}`.
Let's import the Tidyverse, by including this code in the chunk:
```{r load_packages, message=FALSE, warning=FALSE}
library(tidyverse)
```
Notice that you can **run your chunks of code** one by one by clicking the green "play" button at the right of the chunk: you don't have to knit the whole document every time you want to test your code.
Now, try to knit the document and see what it looks like.
#### Challenge 2
Inside a new chunk, add some code to import the dataset located [here](https://raw.githubusercontent.com/uqlibrary/technology-training/master/R/reports/aus_ghg_2017.csv) into an object called `ghg`.
```{r import, message=FALSE, warning=FALSE}
ghg <- read_csv("https://raw.githubusercontent.com/uqlibrary/technology-training/master/R/reports/aus_ghg_2017.csv")
```
> Clicking "Knit" will automatically save your .Rmd file as well as the HTML output.
Now, we can add a chunk to show the data, by including this code in it:
```{r view_data}
ghg
```
### Working directory
Note that the **working directory** for an R Markdown document will be the .Rmd file's location by default (and not necessarily the working directory of the R project your are in). That is why it is a good idea to save your R Markdown file at the top of your R Project directory if you want consistency between your scripts and your R Markdown file.
In our example, we load a CSV file from the Internet, but if we had a data file stored locally, it is important to keep that in mind.
> You can change the default behaviour by using the Knit dropdown menu and choosing an option in "Knit directory".
### Chunk options
Notice how our two first chunks show some messages as an output? We might want to remove that if it is not important and we don't want to include it in the report. At the top of your chunk, you can **modify the options** like so:
```
{r message=FALSE}
```
The code will be executed and the output (if there is any) will be shown, but the messages won't!
There are many options to choose from, depending on what you want to do and show with your chunk of code. For example, to hide both messages and warnings, and only show the output of the code (without showing the underlying code), you can use these options, separated by commas:
```
{r message=FALSE, warning=FALSE, echo=FALSE}
```
It also is a good idea to **label your chunks**, especially in longer documents, so you can spot issues more easily. It won't be shown in the report, but will be used in the R Markdown console and can be used to navigate your script (with the dropdown menu at the bottom of the source panel). For example, for our first chunk:
```
{r load_packages, message=FALSE}
```
It is also possible to include a chunk at the top of your document, that will detail the default options you want to use for all you chunks. That is particularly useful if you want to define a default size for all your figures, for example.
Here is an example of a chunk you might use to change default options:
````
```{r setup, include=FALSE}`r ''`
knitr::opts_chunk$set(echo = TRUE, message = FALSE, warning = FALSE)
```
````
That would make sure that, by default:
* The code is shown, but
* the messages and warnings are hidden.
## Errors when knitting
It should be straight forward to find where an issue comes from when knitting a report does not work.
### Challenge 3
Try changing a chunk code so the code is not valid. What can you see in the R Markdown console?
Double-click on the error message to jump the problem.
## Tidy the data
Let's keep populating our report with more code. Our data is not respecting the tidy data principles, so let's fix that first with a tidyr function:
```{r tidy}
ghg_tidy <- pivot_longer(ghg,
-year,
names_to = "sector",
values_to = "emissions")
```
## Inline code
We can also **include code that will be executed *inside* Markdown text**. For example, you can write the following sentence:
> The dataset contains GHG emissions for the period `` `r knitr::inline_expr("min(ghg$year)")` `` to `` `r knitr::inline_expr("max(ghg$year)")` ``. The maximum GHG emissions recorded for the mining sector is `` `r knitr::inline_expr('round(max(ghg$Mining), 2)')` ``.
We can also use this feature to **auto-update the date** of your report every time it is knitted. Replace the `date` line in the YAML header with this one:
```
date: "`r knitr::inline_expr("Sys.Date()")`"
```
Now, try knitting the report again.
## Visualisation
We can also include a visualisation using, for example, ggplot2:
```{r viz}
ggplot(ghg_tidy, aes(x = year, y = emissions, colour = sector)) +
geom_line() +
ylab("emissions (Mt CO2e)")
```
> If you want to hide the code that created an output, like for this plot, you can add the option `echo=FALSE` to it.
Finally, let's create an interactive version of our plot:
```{r interactive, eval=FALSE}
library(plotly)
p <- ggplot(ghg_tidy, aes(x = year, y = emissions, colour = sector)) +
geom_line() +
ylab("emissions (Mt CO2e)")
ggplotly(p)
```
This will work in a HTML document, but will most likely fail in other output formats.
If you want to change the size of your visualisations, you can tweak the width and height with chunk options. However, you make that consistent for all your figures, by using an extra default option in the setup chunk (the one that contains the `{r setup, include=FALSE}` header, at the top of the document). For example:
```
knitr::opts_chunk$set(fig.width = 8)
```
## Update the report
We have an updated version of the dataset. The only thing we need to do to update the whole report is point the data import code to the new file, at the top of our document, changing the year to "2019":
```{r import_new, echo=TRUE, eval=FALSE}
ghg <- read_csv("https://raw.githubusercontent.com/uqlibrary/technology-training/master/R/reports/aus_ghg_2019.csv")
```
Knitting again will update all the objects and visualisations for us! This is the power of reproducible reports in R.
With reproducible reports, you can potentially structure and write (most of) a report even before you have your research project's final dataset. (Well, at least the data analysis part, maybe not so much the conclusions!)
## Output formats
### HTML documents
The benefits of using HTML documents are multiple:
* figures won't break the flow of the document by jumping to the next page and leaving a large blank space;
* you can include interactive visualisations making use of the latest HTML features;
* they can be directly integrated into a website.
However, other output formats are available. Here are some examples:
* `pdf_document` for a non-editable, widespread, portable format
* `word_document` and `odt_document` to open and edit with Microsoft Word and LibreOffice Writer
* `md_document` for a Markdown file that can easily be published on GitHub or GitLab
* and more, including for creating slides.
### Knitting to PDF
In some cases, you might be required to share your report as a PDF. Knitting your document to PDF can generate very professional-looking reports, but it will require having extra software on your computer.
You can install the necessary LaTeX packages with an R package called TinyTeX, which is a great alternative to very big LaTeX distributions that can be several gigabytes-big.
```{r tinytex, eval=FALSE}
install.packages("tinytex")
tinytex::install_tinytex()
```
After this, try to change your YAML header's `output` value to `pdf_document` and knit it.
## Useful links
Related to R Markdown and knitr:
* _[R Markdown Cookbook](https://bookdown.org/yihui/rmarkdown-cookbook/)_, by Yihui Xie and Christophe Dervieux
* [Official R Markdown website](https://rmarkdown.rstudio.com/) by RStudio
* [Tutorial](https://rmarkdown.rstudio.com/lesson-1.html)
* [Documentation](https://rmarkdown.rstudio.com/docs/)
* [R Markdown cheatsheet](https://raw.githubusercontent.com/rstudio/cheatsheets/main/rmarkdown.pdf)
We also have a [list of recommended R resources](https://github.com/uqlibrary/technology-training/blob/master/R/usefullinks.md#what-next).