Code within text

Session - inline code

Zoë Turner

Inline text

A code chunk:

```{r}
NHSRdatasets::covid19 |>
  filter(countries_and_territories == "United_Kingdom") |>
  summarise(total = n())
```
# A tibble: 1 × 1
  total
  <int>
1   350

As it would appear in a sentence:

There are 350 records available for United Kingdom in the Covid19 dataset from {NHSRdatasets}.

Inline text formatting

Code requires no breaks in lines:

`r NHSRdatasets::covid19 |> filter(countries_and_territories == "United_Kingdom") |> summarise(total = n())`

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}
total_records <- NHSRdatasets::covid19 |>
  filter(countries_and_territories == "United_Kingdom") |>
  summarise(total = n())
```

Then refer to that inline:

`r total_records`

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