lint()
, lint_text()
, lint_package()
, and lint_dir()
all produce a
data.frame containing the lints, their location, and potential fixes. The
only difference is in the input they take:
lint()
takes path to files or directorieslint_text()
takes some text inputlint_dir()
takes a path to one directorylint_package()
takes a path to the root of a package and looks at the following list of folders:R
,tests
,inst
,vignettes
,data-raw
,demo
,exec
.
Usage
lint(
path = ".",
linters = NULL,
exclude_path = NULL,
exclude_linters = NULL,
open = TRUE,
use_cache = TRUE,
verbose = TRUE
)
lint_dir(
path = ".",
linters = NULL,
open = TRUE,
exclude_path = NULL,
exclude_linters = NULL,
use_cache = TRUE,
verbose = TRUE
)
lint_package(
path = ".",
linters = NULL,
open = TRUE,
exclude_path = NULL,
exclude_linters = NULL,
use_cache = TRUE,
verbose = TRUE
)
lint_text(text, linters = NULL, exclude_linters = NULL)
Arguments
- path
A valid path to a file or a directory. Relative paths are accepted.
- linters
A character vector with the names of the rules to apply. See the entire list of rules with
list_linters()
.- exclude_path
One or several paths that will be ignored from the
path
selection.- exclude_linters
One or several linters that will not be checked. Values can be the names of linters (such as
"any_is_na"
) or its associated function, such asany_is_na_linter()
(this is mostly for compatibility withlintr
).- open
If
TRUE
(default) and if this is used in the RStudio IDE, lints will be shown with markers.- use_cache
Do not re-parse files that haven't changed since the last time this function ran.
- verbose
Show messages.
- text
Text to analyze.
Value
A dataframe where each row is a lint. The columns show the text, its location (both the position in the text and the file in which it was found) and the severity.
Ignoring lines
flint
supports ignoring single lines of code with # flint-ignore
. For
example, this will not warn:
# flint-ignore
any(duplicated(x))
However, this will warn for the second any(duplicated())
:
# flint-ignore
any(duplicated(x))
any(duplicated(y))
To ignore more than one line of code, use # flint-ignore-start
and
# flint-ignore-end
:
# flint-ignore-start
any(duplicated(x))
any(duplicated(y))
# flint-ignore-end
Examples
# `lint_text()` is convenient to explore with a small example
lint_text("any(duplicated(rnorm(5)))")
#> ::warning file=/tmp/RtmpL8aaaG/file178b15c5d8ab.R,line=1,col=1::file=/tmp/RtmpL8aaaG/file178b15c5d8ab.R,line=1,col=1,[any(duplicated(rnorm(5)))] anyDuplicated(x, ...) > 0 is better than any(duplicated(x), ...).
lint_text("any(duplicated(rnorm(5)))
any(is.na(x))
")
#> ::warning file=/tmp/RtmpL8aaaG/file178b3de88f1d.R,line=1,col=1::file=/tmp/RtmpL8aaaG/file178b3de88f1d.R,line=1,col=1,[any(duplicated(rnorm(5)))] anyDuplicated(x, ...) > 0 is better than any(duplicated(x), ...).
#> ::warning file=/tmp/RtmpL8aaaG/file178b3de88f1d.R,line=2,col=1::file=/tmp/RtmpL8aaaG/file178b3de88f1d.R,line=2,col=1,[any(is.na(x))] anyNA(x) is better than any(is.na(x)).
# Setup for the example with `lint()`
destfile <- tempfile()
cat("
x = c(1, 2, 3)
any(duplicated(x), na.rm = TRUE)
any(duplicated(x))
if (any(is.na(x))) {
TRUE
}
any(
duplicated(x)
)", file = destfile)
lint(destfile)
#> ℹ Going to check 1 file.
#> ✔ Found 5 lints in 1 file.
#> ℹ 5 of them can be fixed automatically.
#> ::warning file=/tmp/RtmpL8aaaG/file178b11a2c49a,line=2,col=1::file=/tmp/RtmpL8aaaG/file178b11a2c49a,line=2,col=1,[x = c(1, 2, 3)] Use <-, not =, for assignment.
#> ::warning file=/tmp/RtmpL8aaaG/file178b11a2c49a,line=3,col=1::file=/tmp/RtmpL8aaaG/file178b11a2c49a,line=3,col=1,[any(duplicated(x), na.rm = TRUE)] anyDuplicated(x, ...) > 0 is better than any(duplicated(x), ...).
#> ::warning file=/tmp/RtmpL8aaaG/file178b11a2c49a,line=5,col=1::file=/tmp/RtmpL8aaaG/file178b11a2c49a,line=5,col=1,[any(duplicated(x))] anyDuplicated(x, ...) > 0 is better than any(duplicated(x), ...).
#> ::warning file=/tmp/RtmpL8aaaG/file178b11a2c49a,line=7,col=5::file=/tmp/RtmpL8aaaG/file178b11a2c49a,line=7,col=5,[any(is.na(x))] anyNA(x) is better than any(is.na(x)).
#> ::warning file=/tmp/RtmpL8aaaG/file178b11a2c49a,line=11,col=1::file=/tmp/RtmpL8aaaG/file178b11a2c49a,line=11,col=1,[any(
#> duplicated(x)
#> )] anyDuplicated(x, ...) > 0 is better than any(duplicated(x), ...).