Notes On Style

CSI-MTH-190

Schwab

The Tidyverse Style Guide

Tidyverse style guide

1. files names

I will use underscores _, not spaces to name your files. You can also use -

good name

my_file.qmd

poor name

my file!.qmd

2. Syntax

syntax is how the code looks

  • variables should be in snake case and lower case

  • variables should be nouns

  • functions should be verbs (functions is an optional topic)

  • names should be meaningful and concise.

  • Don’t reuse built in R names (TRUE, mean, c, n, etc. )

# good
first_name = "Nic" 

# nmean is a built in function and should not be overwritten
mean = "Schwab"

<- or =

The R community likes to use the <- for variable assignment. However in most other languages = is used, and it works in R.

I will generally use = in this course. For us they are effectly the same.

first_name = "Nic"
# same as 
first_name <- "Nic"

2.5 More Syntax

Consider spacing and commas like in English writing.

More space for readability is ok. Don’t over do it.

# good
x <- 1
y <- 2
total <- sum(c(x, y))
  
# Not as good
new_total<-x+y-total 

# Maybe too much space
sum( c( x , y , total ) )
[1] 6

Pipes and plus

In general |> and + should end lines.

We will discuss these operators more later.