- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
R文档中stopifnot的描述
Description
If any of the expressions in ... are not all TRUE, stop is called, producing an error message indicating the first of the elements of ... which were not true.
Usage
stopifnot(...)
Arguments
... any number of (logical) R expressions, which should evaluate to TRUE.
a <- 1:10
stopifnot(
class(a) %in% c("numeric", "integer"),
sum(a) >= 100
)
> Error: sum(a) >= 100 is not TRUE
a <- letters
stopifnot(
class(a) %in% c("numeric", "integer"),
sum(a) >= 100
)
> Error in sum(a) : invalid 'type' (character) of argument
Error: class(a) %in% c("numeric", "integer") is not TRUE
最佳答案
stopifnot
评估您提供的每个表达式,然后检查它们是否均为TRUE
。如果不是这种情况,它将暂停并打印一条错误消息,指出哪些表达式失败。
在您的示例中,正在发生的事情是表达式本身正在触发错误。导致错误消息的是表达式sum(a) >= 100
的求值,而不是stopifnot
本身。
关于r - stopifnot()对于…中的一个表达式失败,并且不起作用,正如我期望的那样。我的期望正确吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37344004/
我正在使用 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 为示例打印出信息性测试错误的部分似乎相关)
我是一名优秀的程序员,十分优秀!