R programming for beginners (GV900)

Lesson 7: Coding Style and tips & tricks

Sunday, January 7, 2024

Video of Lesson 7

1 Setup

Code
```{r}
#| label: setup
library(tidyverse)
library(nycflights13)
library(palmerpenguins)
```

2 Variable Names

Code
```{r}
# Strive for:
short_flights <- flights |> 
  filter(air_time < 60)

# Avoid:

SHORTFLIGHTS <- flights |> 
  filter(air_time < 60)
```

3 Coding Spaces

Code
```{r}
flights |>
  mutate(
    speed      = distance / air_time,
    dep_hour   = dep_time %/% 100,
    # %/% find out the integer division or floor division.
    dep_minute = dep_time %%  100,
    # %% find out modulus or remainder.
    .keep = "used"
  )
```

4 Pipes: |> or %>%

Code
```{r}
# Strive for 
flights |>  
  filter(!is.na(arr_delay), !is.na(tailnum)) |> 
  count(dest)

# Avoid
flights|>filter(!is.na(arr_delay), !is.na(tailnum))|>count(dest)
```
Code
```{r}
# Strive for
flights |>  
  group_by(tailnum) |> 
  summarize(
    delay = mean(arr_delay, na.rm = TRUE),
    n     = n()
  )

# Avoid
flights |>
  group_by(
    tailnum
  ) |> 
  summarize(delay = mean(arr_delay, na.rm = TRUE), n = n())
```
Code
```{r}
# Strive for 
flights |>  
  group_by(tailnum) |> 
  summarize(
    delay = mean(arr_delay, na.rm = TRUE),
    n     = n()
  )

# Avoid
flights|>
  group_by(tailnum) |> 
  summarize(
             delay = mean(arr_delay, na.rm = TRUE), 
             n = n()
           )

# Avoid
flights|>
  group_by(tailnum) |> 
  summarize(
  delay = mean(arr_delay, na.rm = TRUE), 
  n = n()
  )
```

5 Useful shortcuts

5.1 Most frequently used shortcuts in R Markdown

  • tab: autocomplete
  • Insert assignment operator: CMD + 7 (default:Alt + -)
  • Insert pipe operator: CMD+ p (default: ⌘ + Shift + M or Ctrl + Shift + M)
  • Insert code chunk: CMD + [ (default: CMD + Option + I or Ctrl + Alt + I)
  • Run current chunk: Shift+ CMD + Return
  • Run current line: CMD + Return

5.2 Second frequently used shortcuts

  • Show Help for Current Function: F1
  • Copy current line or selection down or up: Option + CMD + Down (UP)
  • Move current line or selection down or up: Option + Down (UP)
  • Comment or uncomment current line or selection: CMD + Shift + C
Code
```{r}
mtcars |> 
  head()
```
Code
```{r}
iris |> 
  head()
```
  • Expand selection: CMD + Shift + '
Code
```{r}
flights|>
  group_by(tailnum) |> 
  summarize(
             delay = mean(arr_delay, na.rm = TRUE), 
             n = n()
  )
```
  • Rename Symbol in Scope: After selecting a symbol, press Shift + Option + CMD +m. to rename all instances of that symbol in the current chunk.
Code
```{r}
mtcars |> 
  select(cyl) |> 
  filter(cyl > 6)
```
  • Zoom source: Ctrl + Shift + 1

5.3 Multicursor Support: Option + CMD + Click

Code
```{r}
penguins |> 
  filter(sex == "female",
         year == 2007) |> 
  select(island, species, bill_length_mm, sex, year) |> 
  head()
```

5.4 Reformat Current Selection

Code
```{r}
ggplot(penguins, aes(x = bill_length_mm,
y = bill_depth_mm,
color = species)) +
geom_point() +
geom_smooth(method = "lm",
se = FALSE)
```

5.5 Reindent selection

Code
```{r}
ggplot(penguins, aes(x = bill_length_mm,
y = bill_depth_mm,
color = species)) +
geom_point() +
geom_smooth(method = "lm",
se = FALSE)
```

6 Switching between tabs and panes

  • Switch to the next tab: Ctrl + Tab (Like you do in browers)

  • Switch to the previous tab: Ctrl + Shift + Tab

  • Zoom Source: Ctrl + Shift + 1

7 Bonus tips: creating string vectors with addins

This might not be used frequently, but it is very useful when you need to create a string vector.

If you are interested in this tip, you need to install the package hrbraddins first. However, this package is not available on CRAN, you need to install it from GitHub. If you are interested in this package, please follow the instructions below. It is a little bit complicated, but it is worth it.

Code
```{r}
# first install the package `remotes` if you don't have it.
# install.packages("remotes")
# then install the package `hrbraddins` from GitHub
# remotes::install_github("hrbrmstr/hrbraddins")
```

Now you can find the addin hrbraddins in the addin menu, as I have demonstrated in the video.


Thank you!