Check that <-
is always used for assignment.
Usage
assignment_linter(
allow_cascading_assign = TRUE,
allow_right_assign = FALSE,
allow_trailing = TRUE,
allow_pipe_assign = FALSE
)
Arguments
- allow_cascading_assign
Logical, default
TRUE
. IfFALSE
,<<-
and->>
are not allowed.- allow_right_assign
Logical, default
FALSE
. IfTRUE
,->
and->>
are allowed.- allow_trailing
Logical, default
TRUE
. IfFALSE
then assignments aren't allowed at end of lines.- allow_pipe_assign
Logical, default
FALSE
. IfTRUE
, magrittr's%<>%
assignment is allowed.
See also
linters for a complete list of linters available in lintr.
Examples
# will produce lints
lint(
text = "x = mean(x)",
linters = assignment_linter()
)
#> ::warning file=<text>,line=1,col=3::file=<text>,line=1,col=3,[assignment_linter] Use <-, not =, for assignment.
code_lines <- "1 -> x\n2 ->> y"
writeLines(code_lines)
#> 1 -> x
#> 2 ->> y
lint(
text = code_lines,
linters = assignment_linter()
)
#> ::warning file=<text>,line=1,col=3::file=<text>,line=1,col=3,[assignment_linter] Use <-, not ->, for assignment.
#> ::warning file=<text>,line=2,col=3::file=<text>,line=2,col=3,[assignment_linter] ->> can have hard-to-predict behavior; prefer assigning to a specific environment instead (with assign() or <-).
lint(
text = "x %<>% as.character()",
linters = assignment_linter()
)
#> ::warning file=<text>,line=1,col=3::file=<text>,line=1,col=3,[assignment_linter] Avoid the assignment pipe %<>%; prefer using <- and %>% separately.
# okay
lint(
text = "x <- mean(x)",
linters = assignment_linter()
)
code_lines <- "x <- 1\ny <<- 2"
writeLines(code_lines)
#> x <- 1
#> y <<- 2
lint(
text = code_lines,
linters = assignment_linter()
)
# customizing using arguments
code_lines <- "1 -> x\n2 ->> y"
writeLines(code_lines)
#> 1 -> x
#> 2 ->> y
lint(
text = code_lines,
linters = assignment_linter(allow_right_assign = TRUE)
)
lint(
text = "x <<- 1",
linters = assignment_linter(allow_cascading_assign = FALSE)
)
#> ::warning file=<text>,line=1,col=3::file=<text>,line=1,col=3,[assignment_linter] <<- can have hard-to-predict behavior; prefer assigning to a specific environment instead (with assign() or <-).
writeLines("foo(bar = \n 1)")
#> foo(bar =
#> 1)
lint(
text = "foo(bar = \n 1)",
linters = assignment_linter(allow_trailing = FALSE)
)
#> ::warning file=<text>,line=1,col=9::file=<text>,line=1,col=9,[assignment_linter] Assignment = should not be trailing at the end of a line.
lint(
text = "x %<>% as.character()",
linters = assignment_linter(allow_pipe_assign = TRUE)
)