README.md

lintr

Travis-CI Build Status codecov.io CRAN_Status_Badge Join the chat at https://gitter.im/jimhester-lintr/Lobby

Static code analysis for R

lintr is an R package offering static code analysis for R. It checks adherence to a given style, syntax errors and possible semantic issues, see the animation below. In this README find out

lintr

What to do with lintr output?

If you need a bit automatic help for re-styling your code, have a look at the styler package

Available linters

References

Most of the default linters are based on Hadley Wickham's R Style Guide.

Project Configuration

Lintr supports per-project configuration of the following fields. The config file (default file name: .lintr) is in Debian Control Field Format.

An example file that uses 120 character line lengths, excludes a couple of files and sets different default exclude regexs follows.

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"

With the following command, you can create a configuration file for lintr that ignores all linters that show at least one error:

# Create configuration file for lintr
# Source this file in package root directory

# List here files to exclude from lint checking, as a character vector
excluded_files <- c(
    list.files("data",      recursive = TRUE, full.names = TRUE),
    list.files("docs",      recursive = TRUE, full.names = TRUE),
    list.files("inst/doc",  recursive = TRUE, full.names = TRUE),
    list.files("man",       recursive = TRUE, full.names = TRUE),
    list.files("vignettes", recursive = TRUE, full.names = TRUE)
)

### Do not edit after this line ###

library(magrittr)
library(dplyr)

# Make sure we start fresh
if (file.exists(".lintr")) { file.remove(".lintr") }

# List current lints
lintr::lint_package() %>%
    as.data.frame %>%
    group_by(linter) %>%
    tally(sort = TRUE) %$%
    sprintf("linters: with_defaults(\n    %s\n    dummy_linter = NULL\n  )\n",
            paste0(linter, " = NULL, # ", n, collapse = "\n    ")) %>%
    cat(file = ".lintr")

sprintf("exclusions: list(\n    %s\n  )\n",
        paste0('"', excluded_files, '"', collapse = ",\n    ")) %>%
    cat(file = ".lintr", append = TRUE)

# Clean up workspace
remove(excluded_files)

The resulting configuration will contain each currently failing linter and the corresponding number of hits as a comment. Proceed by successively enabling linters, starting with those with the least number of hits. Note that this requires lintr 0.3.0.9001 or later.

If you are developing a package, you can add ^\.lintr$ to your .Rbuildignore file using usethis::use_build_ignore(".lintr").

Continuous integration

If you want to run lintr on Travis-CI in order to check that commits and pull requests don't deteriorate code style, you will need to have Travis install the package first. This can be done by adding the following line to your .travis.yml

r_github_packages:
  - jimhester/lintr

We recommend running lintr::lint_package() as an after_success step in your build process]

lintr-bot will then add comments to the commit or pull request with the lints found and they will also be printed on Travis-CI. If you want to disable the commenting you can set the environment variable LINTR_COMMENT_BOT=false.

Non-failing Lints

after_success:
  - R CMD INSTALL $PKG_TARBALL
  - Rscript -e 'lintr::lint_package()'

Live example of a package using this setup: hibpwned, lintr-bot commenting on a PR.

Installation of development version

To install the latest development version of lintr from GitHub

devtools::install_github("jimhester/lintr")

Editors setup

RStudio

lintr lints are automatically displayed in the RStudio Markers pane, Rstudio versions (> v0.99.206). RStudio Example

Installation

Install lintr, type install.packages("lintr") in the Console.

In order to show the "Markers" pane in RStudio: Menu "Tools" -> "Global Options...", a window with title "Options" will pop up. In that window: Click "Code" on the left; Click "Diagnostics" tab; check "Show diagnostics for R".

To lint a source file test.R type in the Console lintr::lint("test.R") and look at the result in the "Markers" pane.

This package also includes two addins for linting the current source and package. To bind the addin to a keyboard shortcut navigate to Tools > addins > Browse Addins > Keyboard Shortcuts. It's recommended to use Alt+Shift+L for linting the current source code and Ctrl+Shift+Alt+L to code the package. These are easy to remember as you are Alt+Shift+L(int) ;)

Emacs

lintr has built-in integration with flycheck versions greater than 0.23. Emacs Example

Installation

lintr is fully integrated into flycheck when using ESS. See the installalation 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))

Vim

lintr can be integrated with syntastic for on the fly linting.

Vim Example

Installation

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))"

Sublime Text 3

lintr can be integrated with Sublime Linter for on the fly linting.

Sublime Example

Installation

Simply install sublimeLinter-contrib-lintr using Package Control.

For more information see Sublime Linter Docs

Configuration

You can also configure what linters are used. e.g. disabling the assignment linter and using a different line length cutoff. In the SublimeLinter User Settings

{
  "linters": {
    "lintr": {
      "linters": "with_defaults(assignment_linter = NULL, line_length_linter(120))"
    }
  }
}

Atom

lintr can be integrated with Linter for on the fly linting.

Atom Example

Installation

Simply install linter-lintr from within Atom or on the command line with:

apm install linter-lintr

For more information and bug reports see Atom linter-lintr.



Try the lintr package in your browser

Any scripts or data that you put into this service are public.

lintr documentation built on Feb. 19, 2020, 9:10 a.m.