-
Notifications
You must be signed in to change notification settings - Fork 4
/
session-styling.qmd
103 lines (76 loc) · 2.96 KB
/
session-styling.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
---
title: "Introduction to R and Rstudio"
subtitle: "Session - styling code"
---
## Code style
```{r}
#| echo: false
#| label: "libs"
#| include: false
library(readr)
library(countdown)
```
```{r}
#| echo: false
beds_data <- read_csv("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)
```
An updated version of R4DS now includes a section on [code style](https://r4ds.hadley.nz/workflow-style.html)
It particularly mentions the package {styler} which, when run becomes an addin to RStudio, and automatically applies the [tidyverse style](https://style.tidyverse.org/) to code
```{r}
install.packages("styler")
```
## Indentation
RStudio also automatically indents according to the function bracket positions
Highlighting line of code using the keyboard shortcut `Ctrl+i` applies indents
## Have a go - look at automatic indentation
- `beds_data` then press return, where does the cursor go?
- `beds_data |> ` then return, where does the cursor go now?
- `beds_data |> select(org_name, org_code)` and put the select on a new line, then put `org_code` onto a new line.
- Copy the following to the Editor and indent using `Ctrl+i`:
```{r}
beds_data |>
select(org_code,
everything)
```
```{r}
#| echo: false
#| eval: true
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")
```
## Space code
Using spaces helps with readability
The code below will run as R doesn't need spaces to work:
```{r}
beds_dataset=beds_data|>summarise(total_beds=sum(beds_av,na.rm=TRUE),total_occupancy=sum(occ_av,na.rm=TRUE),.by=org_name)|>mutate(perc_occ=total_occupancy/total_beds)|>arrange(desc(perc_occ))
```
Putting the spaces in can be effort after it's been written
## Have a go - {styler}
Firstly copy the code without spaces to the document
```{r}
beds_dataset=beds_data|>summarise(total_beds=sum(beds_av,na.rm=TRUE),total_occupancy=sum(occ_av,na.rm=TRUE),.by=org_name)|>mutate(perc_occ=total_occupancy/total_beds)|>arrange(desc(perc_occ))
```
With {styler} loaded `library(styler)` go to the Add in drop down menu and select `Style Active File`
```{r}
#| echo: false
#| eval: true
countdown::countdown(minutes = 5,
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")
```
## End session