testthat包的expect

编程入门 行业动态 更新时间:2024-10-12 08:18:09
testthat包的expect_is和expect_type有什么区别?(What is the difference between expect_is and expect_type of the testthat package?)

R包testthat包含两个我会说类似的函数来测试对象是否是某种类型(整数,字符等): expect_is()和expect_type() 。 这两个功能有什么区别? 文档说明expect_is()是一个旧形式,但expect_type()呢? 也许您可以提供一些何时使用其中一个的示例。

The R package testthat contains two I would say similar functions to test whether an object is of a certain type (integer, character, etc.): expect_is() and expect_type(). What is the difference between these two functions? The documentation states that expect_is() is an older form, but what about expect_type()? Maybe you could provide some examples of when to use one over the other.

最满意答案

expect_is测试类, expect_type测试类型。 文档说明expect_type与R的基函数typeof的输出进行比较, expect_is与class的输出进行比较。

因此,如果我创建一个整数矩阵,它的类型为“integer”,类为“matrix”:

> typeof(matrix(1:10,2,5)) [1] "integer" > class(matrix(1:10,2,5)) [1] "matrix" >

有关更多信息,请参阅typeof和class的帮助,R的大多数介绍应讨论类型和类。

另请参见mode和storage.mode 。

expect_is tests the class, and expect_type tests the type. The documentation explains that expect_type compares with the output of R's base function typeof, and expect_is compares with the output from class.

So if I make a matrix of integers it has type "integer" and class "matrix":

> typeof(matrix(1:10,2,5)) [1] "integer" > class(matrix(1:10,2,5)) [1] "matrix" >

See the help for typeof and class for more information, and most introductions to R should discuss types and classes.

See also mode and storage.mode.

更多推荐

本文发布于:2023-08-02 08:31:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1372730.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:testthat   expect

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!