Skip to contents

testthat::expect_shape() exists specifically for testing the nrow(), ncol(), or dim() of an object. testthat::expect_equal() and testthat::expect_identical() can also be used for such tests, but it is better to use the tailored function instead.

Usage

expect_shape_linter()

See also

linters for a complete list of linters available in lintr.

Examples

# will produce lints
lint(
  text = "expect_equal(nrow(x), 4L)",
  linters = expect_shape_linter()
)
#> <text>:1:1: warning: [expect_shape_linter] expect_shape(x, nrow = n) is better than expect_equal(nrow(x), n)
#> expect_equal(nrow(x), 4L)
#> ^~~~~~~~~~~~~~~~~~~~~~~~~

lint(
  text = "expect_equal(dim(x), c(2L, 3L))",
  linters = expect_shape_linter()
)
#> <text>:1:1: warning: [expect_shape_linter] expect_shape(x, dim = d) is better than expect_equal(dim(x), d)
#> expect_equal(dim(x), c(2L, 3L))
#> ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

# okay
lint(
  text = "expect_shape(x, nrow = 4L)",
  linters = expect_shape_linter()
)
#>  No lints found.

lint(
  text = "expect_shape(x, dim = c(2L, 3L))",
  linters = expect_shape_linter()
)
#>  No lints found.