1. Static Code Analysis for R
Static code analysis at it's most basic, is simply the computer checking your
code
2. The lintr Package
In R, we can easily perform static code analysis with the help of the lintr package.
This package will check your code follows a given style
It determines possible syntax errors
And highlights potential semantic issues
Basically, the computer automatically does the checking. It's very similar
idea to using a spell checker in Word. It's doesn't change anything, it just
makes suggestions.
3. Using lintr
Using lintr is easy peasy
We simply save the code and pass the file name to the lint function
4. lintr in Action
Suppose we have the following badly formatted code saved in the poorly named file, code dot R
Running lint highlights two issues
5. Issue 1
The first issue is the spacing around the assignment operator.
There should be spaces around the arrow.
6. Issue 2
The second issue is also spacing. This time around the plus. As the warning suggests, we
should add a space around the plus.
Both issues are minor. But I've found that these little suggestions nudge me into
writing better code. Also, lintr and be incorporated directly into tools such as travis.
7. Let's see Lintr in Action
Now your turn to use lintr.