-
Notifications
You must be signed in to change notification settings - Fork 4
/
session-quarto.qmd
218 lines (157 loc) · 6 KB
/
session-quarto.qmd
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
---
title: "Introduction to R and Rstudio"
subtitle: "Session - quarto reporting"
---
```{r}
#| echo: false
#| eval: true
#| label: "libs"
#| include: false
library(countdown)
```
## R Scripts v RMarkdown v Quarto
::: incremental
- Scripts are very quick files of code with comments but RMarkdown and Quarto mix written text with code.
- Quarto is a newer version of RMarkdown (2022) and is very similar for R users.
- Quarto allows the possibility of working with colleagues who use other languages like Python.
:::
## Open a new Quarto file
<img src="img/session-quarto/open-file-quarto.png" alt="Screenshot of File/New File/Quarto Document..." class="center"/>
## Render
Will open up a wizard to force the file to be saved.
As Quarto produces an output file it must have rights to save to the location.
::: notes
Virtual workspaces like the modern desktop used by NHS England may have issues with this as the working directory cannot be overridden with a project file or setwd(). See the prework for help or contact the NHSER group through NHS Futures/NHSR Slack.
:::
## Have a go!
1. Change the title and then render.
1. Go to the bottom of the existing code and in Visual mode create a header `Introduction code` and add a small table.
1. Change the view to Source from Visual - what does the table look like in code?.
1. Click on the wheel icon next to Render and in that menu select Preview in `View Pane`.
1. Render the qmd.
::: notes
For Visual mode in Quarto:
Type / then return for the visual mode to get a short list of code like chunks and headers.
The table can be created through Insert menu or Table and it's the same.
Type in the Source code table to mess up the structure, go to Visual and show how this can be moved with a mouse, then back to Source and it will be neatened automatically.
:::
```{r}
#| eval: true
#| echo: false
countdown::countdown(minutes = 8,
color_border = "#005EB8",
color_text = "#005EB8",
color_running_text = "white",
color_running_background = "#005EB8",
color_finished_text = "#005EB8",
color_finished_background = "white",
margin = "0.9em",
font_size = "2em")
```
## Code chunks
The code sections in Quarto (and RMarkdown) are called chunks.
These are like smaller R scripts.
## New code chunks
The default is for R chunks in R Studio but chunks can also be Python
- Create a new R chunk either with the green button with a <img src="img/icons/notebook-insert-chunk.png" alt="A picture of the green square with c button from RStudio to create a new chunk" width="5%" height="5%"/>
- Using `Ctrl+Alt+i`
- In Quarto Visual type `/` and a drop down menu will appear, R chunk is the first so press Return
```{r}
#| echo: fenced
37+9
```
## Chunk options
Each `chunk` is a part of a longer script and needs to be run in order to render.
- **Switch off chunks**
To stop code running in any of the chunks code:
```{r}
#| echo: fenced
#| eval: false
This will not run and break your code
```
:::incremental
- **Showing your code**
To include code in a report or presentation use `#| echo: true`
:::
::: notes
Direct to [Quarto documentation](https://quarto.org/docs/computations/execution-options.html) for more chunk option information.
Point out the spaces between #| and option then again between : and the true/false is needed or won't Render
:::
## Have a go!
Let's try to show code in a report:
1. Create an R code chunk and type a sum like `37 * 6`
1. In the chunk add `#| echo: false` under the `{r}` and Render
1. Change the code to `true` and Render
Extra time: Add another line for `#| eval: false`,
Render, change to `true` and Render
::: notes
If you show how this works and the chunk has `echo: true` and `eval: true` then this will highlight how the global is overwritten in the chunk in the next section.
:::
```{r}
#| echo: false
#| eval: true
countdown::countdown(minutes = 7,
color_border = "#005EB8",
color_text = "#005EB8",
color_running_text = "white",
color_running_background = "#005EB8",
color_finished_text = "#005EB8",
color_finished_background = "white",
margin = "0.9em",
font_size = "2em")
```
## Global options settings
The default {r} settings are for `echo: true` and `eval: true`.
To switch these globally in YAML (yet another markup language) use execute and set the options to `false`
```{r}
#| code-line-numbers: "3|4|5"
title: "My report"
format: html
execute:
echo: true
eval: true
```
## Self contained
To create an html output that can be emailed without a folder with images and libraries:
```{r}
#| code-line-numbers: "6"
title: "My report"
format: html
execute:
echo: true
eval: true
embed-resources: true
```
::: notes
Note that self-contained from RMarkdown also works as Quarto is backwards compatible.
:::
## Report output table of contents
Code can be added to the YAML to create an interactive table of contents (in html)
```{r}
#| code-line-numbers: "2|3|4"
title: "My report"
format:
html:
toc: true
```
Note that spaces in the YAML matter!
## Naming chunks
::: columns
::: {.column width="40%"}
Chunks can be named which makes them easier to navigate from the bottom left hand menu in the Editor.
`#| label: my-chunk-name`
:::
::: {.column width="60%"}
<img src="img/session-quarto/chunk-names.PNG" alt="Screenshot of RStudio bottom left corner of Editor with the table of contents and a the label code from a chunk behind the menu" class="center"/>
:::
:::
# Navigating headers in RStudio
::: columns
::: {.column width="40%"}
A headers outline can be switched on/off from the Outline icon in the top right of the Editor pane.
:::
::: {.column width="60%"}
<img src="img/session-quarto/outline-toc.PNG" alt="Screenshot of RStudio with the outline button and menu highlighted" class="center"/>
:::
:::
## End session