assertthat in R with pairlist

115 views Asked by At

I am trying to use assertthat::assert_that() on several conditions to be tested, which are collected in a `pairlist'.

The pairlist that I have a looks like this:

pairlist_1 <- as.pairlist(alist(is.null(list()), is.na(list())))

print(pairlist_1)

[[1]]

is.null(list())

[[2]]

is.na(list())

Now, I check the type of the pairlist and its elements:

print(typeof(pairlist_1))
[1] "pairlist"

print(typeof(pairlist_1[[1]]))
[1] "language"

print(typeof(pairlist_1[[2]]))
[1] "language"

I try to use assert_that and check it is working:

assertthat::assert_that(2 + 2 == 4)
[1] TRUE

assertthat::assert_that(is.null(list()))
Error: list() is not NULL

PROBLEM

However, when I try to use assert_that on the pairlist element directly, I get ...

assertthat::assert_that(pairlist_1[[1]])
Error: assert_that: assertion must return a logical value

... instead of Error: list() is not NULL as above, meaning that the assert_that() function cannot be executed.

I tried to evaluate the expressions in the pairlist and then assert the return value, but if I do assertthat::assert_that(eval(pairlist_1[[1]])) I get Error: eval(expr = pairlist_1[[1]]) is not TRUE. I want to get the same error that I get with assertthat::assert_that(is.null(list())), which is: Error: list() is not NULL .

How can I apply testthat::test_that() directly on my pairlist elements? Alternatively, how can I extract the pairlist elements and successfully apply testthat::test_that() on them?

Thanks.

1

There are 1 answers

0
Roland On

Just compute on the language:

eval(substitute(assertthat::assert_that(x), 
                list(x = pairlist_1[[1]])))
#Error: list() is not NULL