Skip to contents

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. If FALSE, <<- and ->> are not allowed.

allow_right_assign

Logical, default FALSE. If TRUE, -> and ->> are allowed.

allow_trailing

Logical, default TRUE. If FALSE then assignments aren't allowed at end of lines.

allow_pipe_assign

Logical, default FALSE. If TRUE, magrittr's %<>% assignment is allowed.

Examples

# will produce lints
lint(
  text = "x = mean(x)",
  linters = assignment_linter()
)
#> <text>:1:3: style: [assignment_linter] Use <-, not =, for assignment.
#> x = mean(x)
#>   ^

code_lines <- "1 -> x\n2 ->> y"
writeLines(code_lines)
#> 1 -> x
#> 2 ->> y
lint(
  text = code_lines,
  linters = assignment_linter()
)
#> <text>:1:3: style: [assignment_linter] Use <-, not ->, for assignment.
#> 1 -> x
#>   ^~
#> <text>:2:3: style: [assignment_linter] Replace ->> by assigning to a specific environment (with assign() or <-) to avoid hard-to-predict behavior.
#> 2 ->> y
#>   ^~~

lint(
  text = "x %<>% as.character()",
  linters = assignment_linter()
)
#> <text>:1:3: style: [assignment_linter] Avoid the assignment pipe %<>%; prefer using <- and %>% separately.
#> x %<>% as.character()
#>   ^~~~

# okay
lint(
  text = "x <- mean(x)",
  linters = assignment_linter()
)
#>  No lints found.

code_lines <- "x <- 1\ny <<- 2"
writeLines(code_lines)
#> x <- 1
#> y <<- 2
lint(
  text = code_lines,
  linters = assignment_linter()
)
#>  No lints found.

# 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)
)
#>  No lints found.

lint(
  text = "x <<- 1",
  linters = assignment_linter(allow_cascading_assign = FALSE)
)
#> <text>:1:3: style: [assignment_linter] Replace <<- by assigning to a specific environment (with assign() or <-) to avoid hard-to-predict behavior.
#> x <<- 1
#>   ^~~

writeLines("foo(bar = \n 1)")
#> foo(bar = 
#>  1)
lint(
  text = "foo(bar = \n 1)",
  linters = assignment_linter(allow_trailing = FALSE)
)
#> <text>:1:9: style: [assignment_linter] Assignment = should not be trailing at the end of a line.
#> foo(bar = 
#>         ^

lint(
  text = "x %<>% as.character()",
  linters = assignment_linter(allow_pipe_assign = TRUE)
)
#>  No lints found.