+ - 0:00:00
Notes for current slide
Notes for next slide

Beautify your R code

💅💄💇 💻

Saskia Freytag

2019/05/07

1 / 18

logo

2 / 18

😢

if (y < 0 && debug)
message("Y is negative")
if (y == 0) {
log(x)
}
else {
y ^ x
}

😊

if (y < 0 && debug) {
message("Y is negative")
}
if (y == 0) {
log(x)
} else {
y ^ x
}
3 / 18

So what encompasses code style?

  • Names

    e.g. for functions 😢 addition() 😊 add()

  • Spacing

    e.g. for indexing 😢 x[, 1] 😊 x[,1]

  • Indenting

    e.g. 😢 tabs 😊 2/4 spaces

  • Organization

    e.g. 😢 loading libraries sequentially 😊 load all libraries at the start

  • Documentation

    e.g. 😢 commented code 😊 use comments to record important findings and analysis decisions

4 / 18

5 / 18

Why care?

  • provides consistency
  • makes code easier to read
  • makes code easier to write
  • makes it easier to collaborate

Good coding style is like using correct punctuation. You can manage without it, but it sure makes things easier to read.

Hadley Wickham, Chief Scientist RStudio

However all style guides are fundamentally opinionated.

6 / 18

Different style guides

The Known

  • tidyverse style guide

    most comprehensive, underscore for naming conventions

  • Advanced R style guide

    fairly comprehensive, underscore for naming conventions

  • Google style guide

    first of its kind, camelCase for naming conventions

The Esoteric

7 / 18

What the f*ck, do I have to read?

Hell no

8 / 18

R's little stylists

tidyverse

  • lintr performs automated checks to confirm that you conform to the style guide.

  • styler allows you to interactively restyle selected text, files, or entire projects. It includes an RStudio add-in, the easiest way to re-style existing code.

Advanced R

  • formatR was designed to reformat R code to improve readability; reorganizing long lines of code, addition of spaces and indents, cleaning if else statements, etc.
9 / 18

Using lintr

Let's look at some bad code saved in a script called bad.R:

feet=c(12,11,12,18)
inches=c(4,3,2,1)
average<-mean(feet/12+inches,na.rm=TRUE)

Can you name two stylistic mistakes?

10 / 18

Using lintr

Let's run the function lint on the bad.R script:

library(lintr)
lint("bad.r")

This is the output:

11 / 18

Using styler

Install styler and head to Tools -> Addins -> Browse Addins -> style active file or:

12 / 18

Using styler

Remember bad.R:

😢

feet=c(12,11,12,18)
inches=c(4,3,2,1)
average<-mean(feet/12+inches,na.rm=TRUE)

After application of styler:::style_selection:

😊

feet <- c(12, 11, 12, 18)
inches <- c(4, 3, 2, 1)
average <- mean(feet / 12 + inches, na.rm = TRUE)
13 / 18

RStudio can also help

After application:

feet <- c(12, 11, 12, 18)
inches <- c(4, 3, 2, 1)
average <- mean(feet / 12 + inches, na.rm = TRUE)
14 / 18

RStudio can also help

Adjust tab-space conversion width:

Insert margin at 80 characters:

15 / 18

Still not good enough?

Ask a friend to do a code review!

16 / 18

My experience

Find credibly curious where ever you get podcasts or soundcloud!

17 / 18

Thanks!

Slides created via the R package xaringan.

The chakra comes from remark.js, knitr, and R Markdown.

18 / 18

logo

2 / 18
Paused

Help

Keyboard shortcuts

, , Pg Up, k Go to previous slide
, , Pg Dn, Space, j Go to next slide
Home Go to first slide
End Go to last slide
Number + Return Go to specific slide
b / m / f Toggle blackout / mirrored / fullscreen mode
c Clone slideshow
p Toggle presenter mode
t Restart the presentation timer
?, h Toggle this help
Esc Back to slideshow