Steve McQueen, your (R) style guru
=======
Good coding style is like correct punctuation:
you can manage without it,
butitsuremakesthingseasiertoread.
======
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
…
=======
Try to develop a consistent style
=======
=======
Syntax
Files
Functions
Code documentation
========
Object names.
Spacing.
Argument names.
Indenting.
Long lines.
Assignment.
Semicolons.
Quotes.
========
R is case-sensitive … other software (M$, …) is not.
x
my_sqrt
myNewFunc()
calcTemp()
=======
Put a space:
=, +, -, <-
, etc…).
1 + 3
x / y
seq(from = 1, to = 10)
:
, ::
, :::
.
1:10
new_var <- 1:10
x <- 10:1
()
or []
, unless there is a ,
.
seq(1, 10)
x[1, ]
=======
Two kinds of arguments:
seq(1, 10)
mean(1:10)
dir.create("my_new_file.r", recursive = TRUE)
========
# an example of a loop (we will do this later in the course)
for i in 1:10 {
x <- 1 + 1
}
dir.create(file.path("testdir2", "testdir3"),
recursive = TRUE)
=======
)
.
seq(from = 1,
to = 100,
by = 5)
=======
Use <-
for assignment.
Never use =
.
R was redesigned early on to permit use of =
in most cases but not all.
YMMV.
x <- 1:10
# No, no, no, no, no, nooooooooooo ...
x = 1:10
=======
=======
" "
and single quotes ' '
are usually interchangable.simonSays <- "Great work, folks!"
=======
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
=======
average<-mean(feet/12+inches,na.rm=TRUE)
x <- 1 : 10
x[1,]
mean(x = 1:10, , FALSE)
mean(, TRUE, x = c(1:10, NA))
do_something_very_complicated("that", requires, many, arguments,
"some of which may be long"
)
x=5
Updated: 2017-09-14