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.
更多推荐
发布评论