Skip to contents

Usage like class(x) == "character" is prone to error since class in R is in general a vector. The correct version for S3 classes is inherits(): inherits(x, "character"). Often, class k will have an is. equivalent, for example is.character() or is.data.frame().

Usage

class_equals_linter()

Details

Similar reasoning applies for class(x) %in% "character".

See also

linters for a complete list of linters available in lintr.

Examples

# will produce lints
lint(
  text = 'is_lm <- class(x) == "lm"',
  linters = class_equals_linter()
)
#> ::warning file=<text>,line=1,col=10::file=<text>,line=1,col=10,[class_equals_linter] Instead of comparing class(x) with ==, use inherits(x, 'class-name') or is.<class> or is(x, 'class')

lint(
  text = 'if ("lm" %in% class(x)) is_lm <- TRUE',
  linters = class_equals_linter()
)
#> ::warning file=<text>,line=1,col=5::file=<text>,line=1,col=5,[class_equals_linter] Instead of comparing class(x) with %in%, use inherits(x, 'class-name') or is.<class> or is(x, 'class')

# okay
lint(
  text = 'is_lm <- inherits(x, "lm")',
  linters = class_equals_linter()
)

lint(
  text = 'if (inherits(x, "lm")) is_lm <- TRUE',
  linters = class_equals_linter()
)