library_call_linter: Library call linter

View source: R/library_call_linter.R

library_call_linterR Documentation

Library call linter

Description

This linter covers several rules related to library() calls:

  • Enforce such calls to all be at the top of the script.

  • Block usage of argument character.only, in particular for loading packages in a loop.

  • Block consecutive calls to suppressMessages(library(.)) in favor of using suppressMessages() only once to suppress messages from all library() calls. Ditto suppressPackageStartupMessages().

Usage

library_call_linter(allow_preamble = TRUE)

Arguments

allow_preamble

Logical, default TRUE. If FALSE, no code is allowed to precede the first library() call, otherwise some setup code is allowed, but all library() calls must follow consecutively after the first one.

Tags

best_practices, configurable, readability, style

See Also

linters for a complete list of linters available in lintr.

Examples

# will produce lints

code <- "library(dplyr)\nprint('test')\nlibrary(tidyr)"
writeLines(code)
lint(
  text = code,
  linters = library_call_linter()
)

lint(
  text = "library('dplyr', character.only = TRUE)",
  linters = library_call_linter()
)

code <- paste(
  "pkg <- c('dplyr', 'tibble')",
  "sapply(pkg, library, character.only = TRUE)",
  sep = "\n"
)
writeLines(code)
lint(
  text = code,
  linters = library_call_linter()
)

code <- "suppressMessages(library(dplyr))\nsuppressMessages(library(tidyr))"
writeLines(code)
lint(
  text = code,
  linters = library_call_linter()
)

# okay
code <- "library(dplyr)\nprint('test')"
writeLines(code)
lint(
  text = code,
  linters = library_call_linter()
)

code <- "# comment\nlibrary(dplyr)"
lint(
  text = code,
  linters = library_call_linter()
)

code <- paste(
  "foo <- function(pkg) {",
  "  sapply(pkg, library, character.only = TRUE)",
  "}",
  sep = "\n"
)
writeLines(code)
lint(
  text = code,
  linters = library_call_linter()
)

code <- "suppressMessages({\n  library(dplyr)\n  library(tidyr)\n})"
writeLines(code)
lint(
  text = code,
  linters = library_call_linter()
)


lintr documentation built on July 16, 2026, 1:08 a.m.