在R中Assert

cgvd09ve  于 2023-04-27  发布在  其他
关注(0)|答案(1)|浏览(102)

我尝试在几个要测试的条件下使用assertthat::assert_that(),这些条件收集在“pairlist”中。
我的pairlist看起来像这样:

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

print(pairlist_1)

[[1]]

is.null(list())

[[2]]

is.na(list())

现在,我检查pairlist及其元素的类型:

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

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

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

我尝试使用assert_that并检查它是否工作:

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

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

问题

然而,当我尝试直接在pairlist元素上使用assert_that时,我得到了...

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

...而不是上面的Error: list() is not NULL,这意味着assert_that()函数无法执行。
我尝试eval评估pairlist中的表达式,然后Assert返回值,但如果我执行assertthat::assert_that(eval(pairlist_1[[1]])),我会得到Error: eval(expr = pairlist_1[[1]]) is not TRUE。我想得到与assertthat::assert_that(is.null(list())), which is: Error: list() is not NULL相同的错误。
如何直接在pairlist元素上应用testthat::test_that()?或者,如何提取pairlist元素并成功应用testthat::test_that()?
谢谢。

im9ewurl

im9ewurl1#

只需要计算语言:

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

相关问题