[英]Operator does not work in test_that function
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 "||"我正在尝试在 R 中编写一个 test_that function 测试数据框的尺寸是否等于使用“||”的行和列的两种组合之一operator.操作员。 But, it is returning the error that the "types are not compatible: integer is not logical."但是,它返回“类型不兼容:integer 不合逻辑”的错误。 Is it even possible to use an "||"甚至可以使用“||” operator in an expect_equal()? 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 ||
numeric
和逻辑运算符,如||
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. while ==0 ~ FALSE
和!=0 ~ TRUE
(尝试if (-1) 1 else 2
和(0)
相同),我不会依赖它。
You're mixing functionality.您正在混合功能。 expect_equal
takes two arguments and either passes or fails. expect_equal
需要两个 arguments 并通过或失败。 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)
为此,您将dim(data_frame)
作为第一个参数传递,虽然您打算将c(60,2)
作为第二个参数,但您的第二个参数被解析为c(60, 2) || dim(data_frame)
c(60, 2) || dim(data_frame)
, which is wrong for a couple of reasons, namely that ||
c(60, 2) || dim(data_frame)
,这是错误的有几个原因,即||
is not vectorized, it must compare length-1 with length-1.未矢量化,它必须将长度 1 与长度 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.而且您正在发送第三个参数c(60,1)
,它被直接传递给底层调用compare
... 但它没有被用作比较值。
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
.我的推断是您希望dim(data_frame) same as c(60,2)
或 ( dim(data_frame) same as c(60,1)
,但您选择的语法对于 R 根本不合法,很多在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)
seems like expect_true(c(dim(data_frame), c(60, 2)) || c(dim(data_frame), c(60,1)))
worked!似乎expect_true(c(dim(data_frame), c(60, 2)) || c(dim(data_frame), c(60,1)))
有效!
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.