I am trying to write a test_that function in R where I test if the dimensions of a data frame is either equal to one of two combinations of rows and columns using the "||" operator. But, it is returning the error that the "types are not compatible: integer is not logical." Is it even possible to use an "||" operator in an expect_equal()?
See below for example code.
test_that('data frame has correct number of rows and columns'){
expect_equal(dim(data_frame), c(60, 2) || dim(data_frame), c(60,1))
})
numeric
and logical operators like ||
are not a clear choice for using together; while ==0 ~ FALSE
and !=0 ~ TRUE
(try if (-1) 1 else 2
and same with (0)
), I wouldn't rely on that.
You're mixing functionality. expect_equal
takes two arguments and either passes or fails. For this, you are passing dim(data_frame)
as the first argument and while you intend c(60,2)
to be the second argument, your second argument is being parsed as c(60, 2) || dim(data_frame)
, which is wrong for a couple of reasons, namely that ||
is not vectorized, it must compare length-1 with length-1. And you're sending a third argument, c(60,1)
, which is being passed directly to the underlying call to compare
... but it isn't being used as a comparison value.
What I'm inferring is that you expect dim(data_frame) same as c(60,2)
or (dim(data_frame) same as c(60,1)
, but your choice of syntax is not legal for R at all, much less in expect_equal
.
I suggest as an alternative:
expect_true(identical(dim(data_frame), c(60L,2L)) ||
identical(dim(data_frame), c(60L,1L)))
Yet another way, with two tests:
expect_equal(nrow(data_frame), 60L)
expect_true(ncol(data_frame) %in% 1:2)