R Style Guide I: Syntax

Steve McQueen, your (R) style guru

1. Why have a code style?

a. Coding styles help communication

Good coding style is like correct punctuation:

you can manage without it,

butitsuremakesthingseasiertoread.

b. Your code has 1 author; but many readers

you, tomorrow

you, on Friday

you, next week

you, next year

your friend

your friend’s friend

your mum

your reviewer

your third reviewer

your revising your manuscript

me

the dog

c. There are many different styles of writing code

Try to develop a consistent style

d. Consistent style is important, because …

e. Style can be applied to:

  1. Syntax

  2. Files

  3. Functions

  4. Code documentation

2. Syntax Style

The arrangement of words and phrases

  1. Object names.

  2. Spacing.

  3. Argument names.

  4. Indenting.

  5. Long lines.

  6. Assignment.

  7. Semicolons.

  8. Quotes.

========

1. Object names

R is case-sensitive … other software (M$, …) is not.

Principles

Best Practice

2. Spacing

Principle

Best Practice

Put a space:

3. Argument names

Two kinds of arguments:

Principle

Best Practice

4. Indenting

Principle

Best Practice

=======

5. Long lines

Principle

Best Practice

6. Assignment

Principle

Best Practice

x <- 1:10

# No, no, no, no, no, nooooooooooo ...
x = 1:10

See here for lots of gory details and discussion.

7. Semicolons

Principle

Best Practice

8. Quotes

Principle

Best Practice

simonSays <- "Great work, folks!"

R help Quotes

Links to various R style guides

Google: One of the first.

Hadley Wickham: Hadley WIckham built on Google’s guide …

Hadley’s Tidyverse … and eventually re-organised everything.

Microsoft A slightly different approach

Rchaeology of Style Learning R style by looking at the R source code

C S Gillespie R style and teaching


Updated: 2018-09-29