Learn R Programming

lintr (version 3.3.0-1)

coalesce_linter: Encourage usage of the null coalescing operator %||%

Description

The x %||% y is equivalent to if (is.null(x)) y else x, but more expressive. It is exported by R since 4.4.0, and equivalents have been available in other tidyverse packages for much longer, e.g. 2008 for ggplot2.

Usage

coalesce_linter()

Arguments

Tags

best_practices, consistency, readability

See Also

linters for a complete list of linters available in lintr.

Examples

Run this code
# will produce lints
lint(
  text = "if (is.null(x)) y else x",
  linters = coalesce_linter()
)

lint(
  text = "if (!is.null(x)) x else y",
  linters = coalesce_linter()
)

lint(
  text = "if (is.null(x[1])) x[2] else x[1]",
  linters = coalesce_linter()
)

# okay
lint(
  text = "x %||% y",
  linters = coalesce_linter()
)

lint(
  text = "x %||% y",
  linters = coalesce_linter()
)

lint(
  text = "x[1] %||% x[2]",
  linters = coalesce_linter()
)


Run the code above in your browser using DataLab