Check that object names conform to a naming style. The default naming styles are "snake_case" and "symbols".
Arguments
- styles
A subset of ‘symbols’, ‘CamelCase’, ‘camelCase’, ‘snake_case’, ‘SNAKE_CASE’, ‘dotted.case’, ‘lowercase’, ‘UPPERCASE’ . A name should match at least one of these styles. The
"symbols"
style refers to names containing only non-alphanumeric characters; e.g., defining%+%
from ggplot2 or%>%
from magrittr would not generate lint markers, whereas%m+%
from lubridate (containing both alphanumeric and non-alphanumeric characters) would.- regexes
A (possibly named) character vector specifying a custom naming convention. If named, the names will be used in the lint message. Otherwise, the regexes enclosed by
/
will be used in the lint message. Note that specifyingregexes
overrides the defaultstyles
. So if you want to combineregexes
andstyles
, both need to be explicitly specified.
Details
Quotes (`"'
) and specials (%
and trailing <-
) are not considered part of the object name.
Note when used in a package, in order to ignore objects imported
from other namespaces, this linter will attempt getNamespaceExports()
whenever an import(PKG)
or importFrom(PKG, ...)
statement is found
in your NAMESPACE file. If requireNamespace()
fails (e.g., the package
is not yet installed), the linter won't be able to ignore some usages
that would otherwise be allowed.
Suppose, for example, you have import(upstream)
in your NAMESPACE,
which makes available its exported S3 generic function
a_really_quite_long_function_name
that you then extend in your package
by defining a corresponding method for your class my_class
.
Then, if upstream
is not installed when this linter runs, a lint
will be thrown on this object (even though you don't "own" its full name).
The best way to get lintr to work correctly is to install the package so that it's available in the session where this linter is running.
See also
linters for a complete list of linters available in lintr.
Examples
# will produce lints
lint(
text = "my_var <- 1L",
linters = object_name_linter(styles = "CamelCase")
)
#> <text>:1:1: style: [object_name_linter] Variable and function name style should match CamelCase.
#> my_var <- 1L
#> ^~~~~~
lint(
text = "xYz <- 1L",
linters = object_name_linter(styles = c("UPPERCASE", "lowercase"))
)
#> <text>:1:1: style: [object_name_linter] Variable and function name style should match UPPERCASE or lowercase.
#> xYz <- 1L
#> ^~~
lint(
text = "MyVar <- 1L",
linters = object_name_linter(styles = "dotted.case")
)
#> <text>:1:1: style: [object_name_linter] Variable and function name style should match dotted.case.
#> MyVar <- 1L
#> ^~~~~
lint(
text = "asd <- 1L",
linters = object_name_linter(regexes = c(my_style = "F$", "f$"))
)
#> <text>:1:1: style: [object_name_linter] Variable and function name style should match my_style or /f$/.
#> asd <- 1L
#> ^~~
# okay
lint(
text = "my_var <- 1L",
linters = object_name_linter(styles = "snake_case")
)
#> ℹ No lints found.
lint(
text = "xyz <- 1L",
linters = object_name_linter(styles = "lowercase")
)
#> ℹ No lints found.
lint(
text = "my.var <- 1L; myvar <- 2L",
linters = object_name_linter(styles = c("dotted.case", "lowercase"))
)
#> ℹ No lints found.
lint(
text = "asdf <- 1L; asdF <- 1L",
linters = object_name_linter(regexes = c(my_style = "F$", "f$"))
)
#> ℹ No lints found.