Skip to contents

Force library calls to all be at the top of the script.

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.

See also

linters for a complete list of linters available in lintr.

Examples

# will produce lints
lint(
  text = "
    library(dplyr)
    print('test')
    library(tidyr)
  ",
  linters = library_call_linter()
)
#> ::warning file=<text>,line=4,col=5::file=<text>,line=4,col=5,[library_call_linter] Move all library calls to the top of the script.

lint(
  text = "
    library(dplyr)
    print('test')
    library(tidyr)
    library(purrr)
  ",
  linters = library_call_linter()
)
#> ::warning file=<text>,line=4,col=5::file=<text>,line=4,col=5,[library_call_linter] Move all library calls to the top of the script.
#> ::warning file=<text>,line=5,col=5::file=<text>,line=5,col=5,[library_call_linter] Move all library calls to the top of the script.

# okay
lint(
  text = "
    library(dplyr)
    print('test')
  ",
  linters = library_call_linter()
)

lint(
  text = "
    # comment
    library(dplyr)
  ",
  linters = library_call_linter()
)