Static Code Analysis for R

Defensive R Programming

Dr. Colin Gillespie

Jumping Rivers

The lintr Package

  • lintr is an R package offering static code analysis for R
    • It checks adherence to a
      • given style
      • syntax errors
      • possible semantic issues

Similar to how spell checkers work

Defensive R Programming

Using lintr

To use lintr

  • We store the code in a file
  • Pass the code to the lint() function
Defensive R Programming

lintr in Action

Suppose I have the following code

my_bad<-function(x, y) {
  x+y
}

saved in the file code.R.

  • Running lintr::lint("code.R") highlights two issues
Defensive R Programming

Issue 1

my_bad<-function(x,y) {
  x+y
}
r[[1]]
tmp.R:1:7: style: Put spaces around all infix operators.
my_bad<-function(x,y) {
     ~^~~
my_bad <- function()
Defensive R Programming

Issue 2

my_bad<-function(x,y) {
  x+y
}
r[[3]]
tmp.R:2:4: style: Put spaces around all infix operators.
  x+y
  ~^~
 x + y
Defensive R Programming

Let's see Lintr in Action

Defensive R Programming

Preparing Video For Download...