Injustice - Developers Among Us (SciFiDevCon 2024)
Linters in R
1. Linters in R
Samantha L Wilson, Hoffman Lab
February 5, 2020
Lab meeting: Tech Talk
1
https://xkcd.com/1513/
2. R Package: ‘lintr’
Lintr
Description: Checks adherence to a given style, syntax errors and possible semantic issues.
Supports on the fly checking of R code edited with 'RStudio IDE', 'Emacs','Vim', 'Sublime Text'
and 'Atom'.
Available in CRAN
lintr reference manual
Much of the information and images from this presentation are from Jim Hester GitHub
2
3. Installation 1: Rstudio
> install.packages("lintr")
## Install the same as all CRAN packages
> library(“lintr”)
> lintr::lint("test.R")
3
Jim Hester GitHub
4. Jim Hester GitHub
Installation 2: Emacs
lintr is fully integrated into flycheck when using ESS.
See the installation documentation for those packages
for more information.
Configuration
You can also configure what linters are used. e.g.
using a different line length cutoff.
● M-x customize-option -> flycheck-lintr-linters ->
with_defaults(line_length_linter(120))
4
5. Installation 3: Vim
Put the file syntastic/lintr.vim in syntastic/syntax_checkers/r. If you are using pathogen this directory is
~/.vim/bundles/syntastic/syntax_checkers/r.
You will also need to add the following lines to your .vimrc.
let g:syntastic_enable_r_lintr_checker = 1
let g:syntastic_r_checkers = ['lintr']
Configuration
You can also configure what linters are used. e.g. using a different line length cutoff.
let g:syntastic_r_lintr_linters = "with_defaults(line_length_linter(120))"
5
Jim Hester GitHub
6. What it looks for
6
A full list of what lintr looks for available here: Jim Hester GitHub
● Syntax errors.
● Proper bracket closure () {} []
● Check that no absolute paths are used (e.g. "/var", "C:System", "~/docs").
● Check that file.path() is used to construct safe and portable paths.
● Check that each step in a pipeline is on a new line, or the entire pipe fits on one line.
● <- is always used for assignment
● Check that objects are not in camelCase.
● Check that closed curly braces should always be on their own line unless they are followed by an else.
● Check that all commas are followed by spaces, but do not have spaces before them.
● Check that there is no commented code outside of roxygen comments.
● Check for overly complicated expressions.
● Check that the [[ operator is used when extracting a single element from an object, not [ (subsetting) nor
$ (interactive use).
● Check that all left parentheses in a function call do not have spaces before them.
● Check that integers are explicitly typed using the form 1L instead of 1.
● Check that all infix operators have spaces around them.
● Check the line length of both comments and code is less than length.
● Check that only spaces are used, never tabs.
● Check that function and variable names are not more than length characters.
8. Project Configuration
● Lintr uses Hadley Wickham’s style guide to coding as the default.
○ You can customize lintr to specify style of your code, if it differs from Wickham’s
■ Make a ‘.lintr’ file
Project Configuration
The config file (default file name: .lintr) is in Debian Control Field Format.
● linters - see ?with_defaults for example of specifying only a few non-default linters.
● exclusions - a list of filenames to exclude from linting. You can use a named item to exclude only
certain lines from a file.
● exclude - a regex pattern for lines to exclude from linting. Default is "# nolint"
● exclude_start - a regex pattern to start exclusion range. Default is "# nolint start"
● exclude_end - a regex pattern to end exclusion range. Default is "# nolint end"
Example:
linters: with_defaults(line_length_linter(120))
exclusions: list("inst/doc/creating_linters.R" = 1, "inst/example/bad.R", "tests/testthat/exclusions-test")
exclude: "# Exclude Linting"
exclude_start: "# Begin Exclude Linting"
exclude_end: "# End Exclude Linting"
8
<- Can also add changes to code style you want here
} Can specify line(s) of code to have lintr skip
9. Useful links
Jim Hester GitHub
If you have any issues, bugs can be submitted here
The reference manual
Hadley Wickham’s style guide
9