- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
使用 stopifnot(),如何测试列表中的元素,同时检查元素本身是否存在?
例如 li 是一个列表,其中包含一个项目。我想确保该项目等于 0。
li <- list()
li$item <- 1
stopifnot(li$item == 0)
# Error: l$x == 0 is not TRUE
返回了一个错误,这就是我想要的。但是,如果我输入错误并输入:
stopifnot(li$tem == 0)
R 不会返回错误。即使该元素不存在于列表中:
li$tem
# NULL
为什么 R 不告诉我在列表中找不到元素“tem”?
li$tem == 0
评估为逻辑(0)。
最佳答案
这里的问题与其说是 stopifnot()
函数,不如说是使用 ==
进行比较:
# returns an empty logical vector, not TRUE or FALSE:
li$tem == 0 # logical(0)
# according to ?`==` we should use identical() instead of ==:
identical(li$tem, 0) # returns FALSE
# this now works as it should, i.e. it throws an error:
stopifnot(identical(li$tem, 0))
来自 ?"=="
:
Do not use == and != for tests, such as in if expressions, where you must get a single TRUE or FALSE. Unless you are absolutely sure that nothing unusual can happen, you should use the identical function instead.
关于r - 如何让 stopifnot() 在调用列表的缺失 (NULL) 元素时返回错误?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33670060/
我正在使用 stopifnot我知道它只返回第一个不是 TRUE 的值.如果这是一些奇怪的动态表达式,那么不熟悉自定义函数的人无法真正从中做出一些贡献。所以我很想添加一个自定义错误消息。有什么建议?
R 中是否有无操作函数,以便即使评估参数很昂贵,它也会立即返回?我正在寻找 stopifnot 的有条件替换功能。 > noop(runif(1e20)) # returns immediately
我想知道 stopifnot() 之间有什么区别和 assertError()是: assertError()默认情况下未找到(您必须先加载“工具”包),但是 stopifnot()是。 更重要的是,
今天发现我的一些stopifnot()测试失败,因为传递的参数评估为空的逻辑向量。 下面是一个例子: stopifnot(iris$nosuchcolumn == 2) # passes witho
使用 stopifnot(),如何测试列表中的元素,同时检查元素本身是否存在? 例如 li 是一个列表,其中包含一个项目。我想确保该项目等于 0。 li <- list() li$item <- 1
R文档中stopifnot的描述 Description If any of the expressions in ... are not all TRUE, stop is called, prod
我想要提供信息丰富的 stopifnot() 错误。 我读过: http://r-pkgs.had.co.nz/tests.html (最后关于使用 NSE 为示例打印出信息性测试错误的部分似乎相关)
我是一名优秀的程序员,十分优秀!