Learn R Programming

lintr

{lintr} provides static code analysis for R. It checks for adherence to a given style, identifying syntax errors and possible semantic issues, then reports them to you so you can take action. Watch lintr in action in the following animation:

{lintr} is complementary to the {styler} package which automatically restyles code, eliminating some of the problems that {lintr} can detect.

Installation

Install the stable version from CRAN:

install.packages("lintr")

Or the development version from GitHub:

# install.packages("remotes")
remotes::install_github("r-lib/lintr")

Usage

And then you can create a configuration file and run selected linters:

lintr::use_lintr(type = "tidyverse")

# in a project:
lintr::lint_dir()

# in a package:
lintr::lint_package()

To see a list of linters included for each configuration:

# tidyverse (default)
names(lintr::linters_with_defaults())

# full
names(lintr::all_linters())

Setting up GitHub Actions

{usethis} provides helper functions to generate lint workflows for GitHub Actions:

# in a project:
usethis::use_github_action("lint-project")

# in a package:
usethis::use_github_action("lint")

You can also run lintr during continuous integration or within your IDE or text editor. See vignette("continuous-integration") and vignette("editors") for more details.

Without further configuration, this will run the default linters. See vignette("lintr") to learn how to modify these defaults.

Code of Conduct

Please note that the lintr project is released with a Contributor Code of Conduct. By contributing to this project, you agree to abide by its terms.

Copy Link

Version

Install

install.packages('lintr')

Monthly Downloads

78,962

Version

3.1.2

License

MIT + file LICENSE

Issues

Pull Requests

Stars

Forks

Maintainer

Last Published

March 25th, 2024

Functions in lintr (3.1.2)

configurable_linters

Configurable linters
deprecated_linters

Deprecated linters
exclude

Exclude lines or files from linting
consistency_linters

Consistency linters
equals_na_linter

Equality check with NA linter
cyclocomp_linter

Cyclomatic complexity linter
efficiency_linters

Efficiency linters
correctness_linters

Correctness linters
empty_assignment_linter

Block assignment of {}
default_linters

Default linters
expect_identical_linter

Require usage of expect_identical(x, y) where appropriate
conjunct_test_linter

Force && conditions to be written separately where appropriate
duplicate_argument_linter

Duplicate argument linter
consecutive_assertion_linter

Force consecutive calls to assertions into just one when possible
executing_linters

Code executing linters
expect_length_linter

Require usage of expect_length(x, n) over expect_equal(length(x), n)
expect_comparison_linter

Require usage of expect_gt(x, y) over expect_true(x > y) (and similar)
expect_named_linter

Require usage of expect_named(x, n) over expect_equal(names(x), n)
expect_not_linter

Require usage of expect_false(x) over expect_true(!x)
extraction_operator_linter

Extraction operator linter
expect_type_linter

Require usage of expect_type(x, type) over expect_equal(typeof(x), type)
expect_s4_class_linter

Require usage of expect_s4_class(x, k) over expect_true(is(x, k))
function_argument_linter

Function argument linter
function_left_parentheses_linter

Function left parentheses linter
expect_s3_class_linter

Require usage of expect_s3_class()
expect_null_linter

Require usage of expect_null for checking NULL
implicit_assignment_linter

Avoid implicit assignment in function calls
length_levels_linter

Require usage of nlevels over length(levels(.))
indentation_linter

Check that indentation is consistent
implicit_integer_linter

Implicit integer linter
expect_true_false_linter

Require usage of expect_true(x) over expect_equal(x, TRUE)
get_source_expressions

Parsed sourced file from a filename
expect_lint_free

Test that the package is lint free
expect_lint

Lint expectation
inner_combine_linter

Require c() to be applied before relatively expensive vectorized functions
is_lint_level

Is this an expression- or a file-level source object?
all_undesirable_functions

Default undesirable functions and operators
default_settings

Default lintr settings
is_numeric_linter

Redirect is.numeric(x) || is.integer(x) to just use is.numeric(x)
function_return_linter

Lint common mistakes/style issues cropping up from return statements
get_r_string

Extract text from STR_CONST nodes
ids_with_token

Get parsed IDs by token
fixed_regex_linter

Require usage of fixed=TRUE in regular expressions where appropriate
literal_coercion_linter

Require usage of correctly-typed literals over literal coercions
make_linter_from_xpath

Create a linter from an XPath
lint

Lint a file, directory, or package
linters

Available linters
if_not_else_linter

Block statements like if (!A) x else y
ifelse_censor_linter

Block usage of ifelse() where pmin() or pmax() is more appropriate
for_loop_index_linter

Block usage of for loops directly overwriting the indexing variable
lengths_linter

Require usage of lengths() where possible
lintr-package

Lintr
library_call_linter

Library call linter
linters_with_tags

Create a tag-based linter configuration
nested_ifelse_linter

Block usage of nested ifelse() calls
object_length_linter

Object length linter
keyword_quote_linter

Block unnecessary quoting in calls
numeric_leading_zero_linter

Require usage of a leading zero in all fractional numerics
infix_spaces_linter

Infix spaces linter
outer_negation_linter

Require usage of !any(x) over all(!x), !all(x) over any(!x)
pipe_call_linter

Pipe call linter
linters_with_defaults

Create a linter configuration based on defaults
namespace_linter

Namespace linter
package_development_linters

Package development linters
lintr-deprecated

Deprecated functions in lintr
missing_package_linter

Missing package linter
modify_defaults

Modify lintr defaults
length_test_linter

Check for a common mistake where length is applied in the wrong place
parse_exclusions

read a source file and parse all the excluded lines from it
object_usage_linter

Object usage linter
lint-s3

Create a lint object
object_name_linter

Object name linter
redundant_ifelse_linter

Prevent ifelse() from being used to produce TRUE/FALSE or 1/0
nonportable_path_linter

Non-portable path linter
line_length_linter

Line length linter
normalize_exclusions

Normalize lint exclusions
routine_registration_linter

Identify unregistered native routines
sarif_output

SARIF Report for lint results
repeat_linter

Repeat linter
paren_body_linter

Parenthesis before body linter
package_hooks_linter

Package hooks linter
strings_as_factors_linter

Identify cases where stringsAsFactors should be supplied explicitly
robustness_linters

Robustness linters
regex_subset_linter

Require usage of direct methods for subsetting strings via regex
seq_linter

Sequence linter
undesirable_function_linter

Undesirable function linter
undesirable_operator_linter

Undesirable operator linter
paste_linter

Raise lints for several common poor usages of paste()
readability_linters

Readability linters
sort_linter

Check for common mistakes around sorting vectors
trailing_blank_lines_linter

Trailing blank lines linter
pipe_continuation_linter

Pipe continuation linter
sprintf_linter

Require correct sprintf() calls
pipe_consistency_linter

Pipe consistency linter
pkg_testthat_linters

Testthat linters
string_boundary_linter

Require usage of startsWith() and endsWith() over grepl()/substr() versions
system_file_linter

Block usage of file.path() with system.file()
unreachable_code_linter

Block unreachable code and comments following return statements
trailing_whitespace_linter

Trailing whitespace linter
unused_import_linter

Check that imported packages are actually used
quotes_linter

Character string quote linter
matrix_apply_linter

Require usage of colSums(x) or rowSums(x) over apply(x, ., sum)
unnecessary_concatenation_linter

Unneeded concatenation linter
scalar_in_linter

Block usage like x %in% "a"
unnecessary_lambda_linter

Block usage of anonymous functions in iteration functions when unnecessary
read_settings

Read lintr settings
missing_argument_linter

Missing argument linter
whitespace_linter

Whitespace linter
semicolon_linter

Semicolon linter
redundant_equals_linter

Block usage of ==, != on logical vectors
spaces_inside_linter

Spaces inside linter
todo_comment_linter

TODO comment linter
unnecessary_nested_if_linter

Avoid unnecessary nested if conditional statements
unnecessary_placeholder_linter

Block usage of pipeline placeholders if unnecessary
use_lintr

Use lintr in your project
style_linters

Style linters
spaces_left_parentheses_linter

Spaces before parentheses linter
vector_logic_linter

Enforce usage of scalar logical operators in conditional statements
xml_nodes_to_lints

Convert an XML node or nodeset into a Lint
yoda_test_linter

Block obvious "yoda tests"
xp_call_name

Get the name of the function matched by an XPath
available_linters

Get Linter metadata from a package
T_and_F_symbol_linter

T and F symbol linter
Linter

Create a linter closure
assignment_linter

Assignment linter
best_practices_linters

Best practices linters
any_duplicated_linter

Require usage of anyDuplicated(x) > 0 over any(duplicated(x))
backport_linter

Backport linter
absolute_path_linter

Absolute path linter
all_linters

Create a linter configuration based on all available linters
any_is_na_linter

Require usage of anyNA(x) over any(is.na(x))
commented_code_linter

Commented code linter
clear_cache

Clear the lintr cache
brace_linter

Brace linter
checkstyle_output

Checkstyle Report for lint results
commas_linter

Commas linter
boolean_arithmetic_linter

Require usage of boolean operators over equivalent arithmetic
condition_message_linter

Block usage of paste() and paste0() with messaging functions using ...
class_equals_linter

Block comparison of class with ==
common_mistakes_linters

Common mistake linters