How to set tolerance of expect_equal in testthat when comparing data_frames?
Asked Answered
S

1

8

I'm having trouble using testthat's tolerance parameter when I check whether two data_frames are equal. Here's a simple example with two data frames:

library(dplyr)
library(testthat)
d1 = data_frame(a = c(1, 1),
                b = c(2, 2))
d2 = data_frame(a = c(1, 1.00001),
                b = c(2, 2))

When I check for equality, testthat throws an error, even with a sufficiently high tolerance value:

expect_equal(d1, d2,
             tolerance = 0.01)

# Error: d1 not equal to d2
# Rows in y but not x: 2. Rows with difference occurences in x and y: 1

No error is thrown when I compare the individual vectors:

expect_equal(d1$a, d2$a,
             tolerance = 0.01)
expect_equal(d1$b, d2$b,
             tolerance = 0.01)

Any suggestions? I assume I'm misusing the expect_equal function, but am not sure how to resolve other than running expect_equal on individual columns of the data frame.

Here's the package versions I'm using:

packageVersion("dplyr")
# [1] ‘0.4.3’
packageVersion("testthat")
# [1] ‘0.11.0’
Syllabi answered 10/6, 2016 at 0:42 Comment(1)
I can reproduce your error. Without dplyr (Just use data.frame) all tests pass as expected.Cele
E
2

It looks like this issue has since been resolved.

library(dplyr)
library(testthat)

d1 = data_frame(a = c(1, 1), b = c(2, 2))
d2 = data_frame(a = c(1, 1.00001), b = c(2, 2))

expect_equal(d1, d2, tolerance = 0.01)

The test comes out clean now.

Emetine answered 15/3, 2021 at 17:54 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.