forked from nhs-r-community/intro-quarto
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsession-inline-code.qmd
74 lines (51 loc) · 1.72 KB
/
session-inline-code.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
---
title: "Code within text"
subtitle: "Session - inline code"
execute:
eval: true
---
```{r}
#| label: packages
#| eval: true
#| include: false
library(NHSRdatasets)
library(dplyr)
library(lubridate)
```
## Inline text
A code chunk:
```{r}
#| echo: fenced
NHSRdatasets::covid19 |>
filter(countries_and_territories == "United_Kingdom") |>
summarise(total = n())
```
As it would appear in a sentence:
. . .
There are **`r NHSRdatasets::covid19 |> filter(countries_and_territories == "United_Kingdom") |> summarise(total = n())`** records available for United Kingdom in the Covid19 dataset from {NHSRdatasets}.
## Inline text formatting
Code requires no breaks in lines:
``` `r knitr::inline_expr('NHSRdatasets::covid19 |> filter(countries_and_territories == "United_Kingdom") |> summarise(total = n())')` ```
. . .
:::{.callout-note collapse=false appearance='default' icon=true}
## Seeing the output
With the cursor in the inline text use `Ctrl + Enter` to run the code to get a sneak preview of the output...
... and test it works!
:::
## Avoid long inline code
The example is very long code which makes it hard to debug when inline so a better way is to create an object:
```{r}
#| echo: fenced
total_records <- NHSRdatasets::covid19 |>
filter(countries_and_territories == "United_Kingdom") |>
summarise(total = n())
```
. . .
Then refer to that inline:
``` `r knitr::inline_expr('total_records')` ```
:::{.callout-tip collapse=false appearance='default' icon=true}
## The `pull()` function
Sometimes the code you produce won't show as part of a sentence because it's a data frame and needs to be a single vector for inline.
To extract a single column pipe `|>` to `pull()` at the end of your code
:::
## Next Section