A small gotcha when comparing lists using testthat

May 11, 2020
By

[This article was first published on R – Statistical Odds & Ends, and kindly contributed to R-bloggers]. (You can report issue about the content on this page here)
Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.

I recently encountered a small gotcha when using the testthat package to test the equality of two lists. Imagine I have the following two lists below:

list1 <- list(a = 1, b = 2, c = 3, d = 4)
list2 <- list(a = 0, b = 2, c = 3, d = 5)

Imagine that:

  1. I know that the value associated with a is going to be different, so I don’t want to test equality for it.
  2. The values associated with the other keys should be the same, so I want to test equality for them.

If I run the tests correctly, I should get an error message associated with the key d. The following code snippet using the testthat package looks like it does the job, BUT IT DOES NOT WORK:

library(testthat)

# WRONG!!!!
for (key in names(list1)) {
    if (key != "a") {
        expect_equal(list1$key, list2$key)
    }
}

The code above incorrectly reports that all the elements in list1 and list2 (ignoring the key a) are equal.

The correct way to test equality in this situation is as follows:

library(testthat)

# CORRECT
keys <- setdiff(names(list1), "a")
expect_equal(list1[keys], list2[keys])
# Error: list1[keys] not equal to list2[keys].
# Component “d”: Mean relative difference: 0.25

Update: As some of the commenters have rightly pointed out (thanks for contributing!), the bigger mistake in the first snippet is the use of $ to pull out the element instead of the double square braces [[. When using $, it is looking for a key in the list that is literally named "key". Not what I intended!

The code snippet above can be changed to use [[, but notice how the error message is less informative:

library(testthat)

# CORRECT, but error message less informative
for (key in names(list1)) {
    if (key != "a") {
        expect_equal(list1[[key]], list2[[key]])
    }
}
# Error: list1[[key]] not equal to list2[[key]].
# 1/1 mismatches
# [1] 4 - 5 == -1

To leave a comment for the author, please follow the link and comment on their blog: R – Statistical Odds & Ends.

R-bloggers.com offers daily e-mail updates about R news and tutorials about learning R and many other topics. Click here if you're looking to post or find an R/data-science job.
Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.



If you got this far, why not subscribe for updates from the site? Choose your flavor: e-mail, twitter, RSS, or facebook...

Comments are closed.

Search R-bloggers

Sponsors

Never miss an update!
Subscribe to R-bloggers to receive
e-mails with the latest R posts.
(You will not see this message again.)

Click here to close (This popup will not appear again)