Linters in R
Samantha L Wilson, Hoffman Lab
February 5, 2020
Lab meeting: Tech Talk
1
https://xkcd.com/1513/
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
Installation 1: Rstudio
> install.packages("lintr")
## Install the same as all CRAN packages
> library(“lintr”)
> lintr::lint("test.R")
3
Jim Hester GitHub
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
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
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.
Example
Jim Hester GitHub
7
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
Useful links
Jim Hester GitHub
If you have any issues, bugs can be submitted here
The reference manual
Hadley Wickham’s style guide
9

More Related Content

PPTX
Tuples, Dicts and Exception Handling
PDF
AVTOKYO2013.5 Detail of CVE-2013-4787 (Master Key Vulnerability)
PPTX
Ifi7107 lesson4
PDF
Presentation
DOC
Lex tool manual
PDF
Perl Programming - 01 Basic Perl
PDF
7986-lect 7.pdf
PDF
Extensible markup language attacks
Tuples, Dicts and Exception Handling
AVTOKYO2013.5 Detail of CVE-2013-4787 (Master Key Vulnerability)
Ifi7107 lesson4
Presentation
Lex tool manual
Perl Programming - 01 Basic Perl
7986-lect 7.pdf
Extensible markup language attacks

Similar to Linters in R (20)

PPT
LEX lexical analyzer for compiler theory.ppt
PDF
Filters & Vi Editor
PPTX
Learn Python The Hard Way Presentation
PDF
Chapter 1: Introduction to Command Line
DOCX
CS 23001 Computer Science II Data Structures & AbstractionPro.docx
PDF
Android coding standard
PDF
Rust All Hands Winter 2011
ODP
Nithi
PPTX
Chapter 1: Introduction to Command Line
PDF
A Peek into TFRT
PPTX
Python Session - 2
PDF
Linux intro 3 grep + Unix piping
PPT
Kavitha_python.ppt
PDF
Why Drupal is Rockstar?
PPT
Python ppt
PPTX
R Introduction
PPTX
Best Coding Practices in Java and C++
PDF
Project lexical analyser compiler _1.pdf
PPTX
OCA_1Z0-808_Module00_Introduction_Java.pptx
PPT
ENGLISH PYTHON.ppt
LEX lexical analyzer for compiler theory.ppt
Filters & Vi Editor
Learn Python The Hard Way Presentation
Chapter 1: Introduction to Command Line
CS 23001 Computer Science II Data Structures & AbstractionPro.docx
Android coding standard
Rust All Hands Winter 2011
Nithi
Chapter 1: Introduction to Command Line
A Peek into TFRT
Python Session - 2
Linux intro 3 grep + Unix piping
Kavitha_python.ppt
Why Drupal is Rockstar?
Python ppt
R Introduction
Best Coding Practices in Java and C++
Project lexical analyser compiler _1.pdf
OCA_1Z0-808_Module00_Introduction_Java.pptx
ENGLISH PYTHON.ppt
Ad

More from Hoffman Lab (20)

PPTX
Miller: A command-line tool for querying, shaping, and reformatting data files
PDF
GNU Parallel: Lab meeting—technical talk
PDF
TCRpower
PPTX
Efficient querying of genomic reference databases with gget
PPTX
WashU Epigenome Browser
PPTX
Wireguard: A Virtual Private Network Tunnel
PPTX
Plotting heatmap with matplotlib/seaborn
PPTX
Go Get Data (GGD)
PPTX
fastp: the FASTQ pre-processor
PPTX
R markdown and Rmdformats
PPTX
File searching tools
PPTX
Better BibTeX (BBT) for Zotero
PPTX
Awk primer and Bioawk
PPTX
Terminals and Shells
PPTX
BioRender & Glossary/Acronym
PPTX
BioSyntax: syntax highlighting for computational biology
PPTX
Get Good With Git
PDF
Tech Talk: UCSC Genome Browser
PDF
MultiQC: summarize analysis results for multiple tools and samples in a singl...
PPTX
dreamRs: interactive ggplot2
Miller: A command-line tool for querying, shaping, and reformatting data files
GNU Parallel: Lab meeting—technical talk
TCRpower
Efficient querying of genomic reference databases with gget
WashU Epigenome Browser
Wireguard: A Virtual Private Network Tunnel
Plotting heatmap with matplotlib/seaborn
Go Get Data (GGD)
fastp: the FASTQ pre-processor
R markdown and Rmdformats
File searching tools
Better BibTeX (BBT) for Zotero
Awk primer and Bioawk
Terminals and Shells
BioRender & Glossary/Acronym
BioSyntax: syntax highlighting for computational biology
Get Good With Git
Tech Talk: UCSC Genome Browser
MultiQC: summarize analysis results for multiple tools and samples in a singl...
dreamRs: interactive ggplot2
Ad

Recently uploaded (20)

PDF
Five Habits of High-Impact Board Members
PDF
From MVP to Full-Scale Product A Startup’s Software Journey.pdf
PDF
sustainability-14-14877-v2.pddhzftheheeeee
PPTX
Benefits of Physical activity for teenagers.pptx
PDF
Assigned Numbers - 2025 - Bluetooth® Document
PDF
A contest of sentiment analysis: k-nearest neighbor versus neural network
PDF
TrustArc Webinar - Click, Consent, Trust: Winning the Privacy Game
PPTX
Tartificialntelligence_presentation.pptx
PPTX
observCloud-Native Containerability and monitoring.pptx
PDF
1 - Historical Antecedents, Social Consideration.pdf
PPTX
Chapter 5: Probability Theory and Statistics
PDF
Microsoft Solutions Partner Drive Digital Transformation with D365.pdf
PDF
DP Operators-handbook-extract for the Mautical Institute
PDF
CloudStack 4.21: First Look Webinar slides
PDF
DASA ADMISSION 2024_FirstRound_FirstRank_LastRank.pdf
PPTX
Group 1 Presentation -Planning and Decision Making .pptx
PDF
Architecture types and enterprise applications.pdf
PDF
Zenith AI: Advanced Artificial Intelligence
PDF
NewMind AI Weekly Chronicles – August ’25 Week III
PPTX
Web Crawler for Trend Tracking Gen Z Insights.pptx
Five Habits of High-Impact Board Members
From MVP to Full-Scale Product A Startup’s Software Journey.pdf
sustainability-14-14877-v2.pddhzftheheeeee
Benefits of Physical activity for teenagers.pptx
Assigned Numbers - 2025 - Bluetooth® Document
A contest of sentiment analysis: k-nearest neighbor versus neural network
TrustArc Webinar - Click, Consent, Trust: Winning the Privacy Game
Tartificialntelligence_presentation.pptx
observCloud-Native Containerability and monitoring.pptx
1 - Historical Antecedents, Social Consideration.pdf
Chapter 5: Probability Theory and Statistics
Microsoft Solutions Partner Drive Digital Transformation with D365.pdf
DP Operators-handbook-extract for the Mautical Institute
CloudStack 4.21: First Look Webinar slides
DASA ADMISSION 2024_FirstRound_FirstRank_LastRank.pdf
Group 1 Presentation -Planning and Decision Making .pptx
Architecture types and enterprise applications.pdf
Zenith AI: Advanced Artificial Intelligence
NewMind AI Weekly Chronicles – August ’25 Week III
Web Crawler for Trend Tracking Gen Z Insights.pptx

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