STAT 513/413: Lecture 2 R in style
(There is almost always more than one way to do it)
Catching up where we left
So, why don’t like it? What is wrong with it?
1
A simple answer
Does code in any published book that features R look like that? Really, does it?
2
A simple answer
Does code in any published book that features R look like that? Really, does it?
Why not? Because… most of the code out there is typically: • monospaced
• properly styled
• in R spirit
• and commented
But remember: nothing is a dogma here
and there is almost always more than one way to do it
2
Monospaced
3
Online
A growing list of R books (without any preferences whatsoever)
H. Wickham: Advanced R
G. Grolemund: Hands-on Programming with R
G. Grolemund and H. Wickham: R for Data Science
C. Gillespie, R. Lovelace: Efficient R Programming
N. D. Phillips: YaRrr! The Pirate’s Guide to R
Printable, lecture notes, etc.
W. N. Venables, R. M. Smith and the R Core Team: An Introduction to R
E. Paradis: R for beginners
T. Martin: The Undergraduate Guide to R
N. Matloff: The Art of R Programming
N. J. Horton, R. Pruim, D. T. Kaplan: A Student’s Guide to R
4
Some peculiar aspects
• Functions always must have parentheses
• Workspace stays, if you wish
• Fancy assignments
• Parentheses () are for functions, brackets [] for indexing • And braces {} for commands
5
Parentheses
retina:513 mizera$ R
R version 3.5.3 (2019-03-11) — “Great Truth”
… – THIS WILL INDICATE OMITTED PART OF THE OUTPUT, I.M.
>q
function (save = “default”, status = 0, runLast = TRUE) .Internal(quit(save, status, runLast))
> ls()
character(0)
> x=1
> ls()
[1] “x”
> q()
Save workspace image? [y/n/c]: y
retina:513 mizera$
6
Workspace stays
retina:513 mizera$ open -a R .
In a separate window:
R version 3.5.3 (2019-03-11) — “Great Truth”
Copyright (C) 2019 The R Foundation for Statistical Computing
Platform: x86_64-apple-darwin15.6.0 (64-bit)
…
[R.app GUI 1.70 (7632) x86_64-apple-darwin15.6.0]
[History restored from /Users/mizera/.Rhistory]
[Workspace restored from /Users/mizera/work/Lessons/513/.RData]
> ls() [1] “x” >x
[1] 1
7
> ??knn
> apropos(“ls”)
> example(ls)
> demo()
# same as help.search()
# quotes again needed
# quotes not needed
Getting help
> help(ls)
> help(“ls”)
> help(’ls’)
> ?ls
> help.search(“knn”) # quotes obligatory now
> help.search(’knn’) # (whatever comes after # is a comment)
# usually works without quotes
# those are needed only in special cases
# uppercase and lowercase quotes are equivalent
# and this is the short way for all of the above
8
Fancy assignments
> x=2 # this is what most of us do > x = 2 # OK, at least this is
>x
[1] 2
> x <- 3 # this is what stylish people encourage
>x
[1] 3
> 3 -> x # this is also possible, but rather discouraged >x
[1] 3
9
And now: style
Roughly this:
• code inside braces should be indented
• indent is two or four spaces (consistently throughout though)
• …unless you continue a function: then you return where it started • because you should break long lines into nicer shorter ones
• closing brace } should have its own line
• there should be spaces…
• …but not excessively many of them (no function ( x , y ) , say)
Some refined ones:
• use <-, not =, and certainly not -> • use TRUE, FALSE, not merely T, F
Finally, comments: you should use, but not abuse; use taste
10
More precisely here:
https://style.tidyverse.org http://adv-r.had.co.nz/Style.html https://google.github.io/styleguide/Rguide.xml R Code Style R Bloggers (RStudio)
References
11
The best way is…
… when the programming editor does it for you automagically (that is why it is important your files have extension .R)
Some of those are
• ESS with Emacs
• RStudio (configurable!), ATOM, …
It is also possible to run your code through R packages:
• styler • formatR
12
In proper style?
13
Ah, spacing!
14
Or this?
15
And this, if you wish…
16