R 函数“require”在“sapply”中无法按预期工作
我想检查一堆软件包是否已正确安装(以使 dockerfile 崩溃而不是默默地继续)。
我在包名称列表上尝试了 sapply,这就是发生的情况。 请注意:
- “not_available”未安装:)
- “testthat”已安装。
# to show the invisible output:
> show <- function(x) {cat(x, '\n')}
> show(require('not_available'))
Loading required package: not_available
FALSE
Warning message:
In library(package, lib.loc = lib.loc, character.only = TRUE, logical.return = TRUE, :
there is no package called ‘not_available’
到目前为止一切顺利,完全符合预期。
> show(require('testthat'))
TRUE
完美...
> sapply(c('not_available', 'testthat'), require, warn.conflicts = FALSE, quietly = TRUE)
not_available testthat
FALSE FALSE
Warning messages:
1: In if (!loaded) { :
the condition has length > 1 and only the first element will be used
2: In if (!loaded) { :
the condition has length > 1 and only the first element will be used
呜呜...
为什么它警告我们?为什么输出不正确?
> sapply(c('not_available', 'testthat'), function(x) {x})
not_available testthat
"not_available" "testthat"
好吧,所以 sapply 并没有背叛我...
> sapply(c('not_available', 'testthat'), function(pkg) {require(pkg, warn.conflicts = FALSE, quietly = TRUE)})
not_available testthat
FALSE FALSE
呃,现在警告消失了,但是答案...仍然错误...为什么?
有什么线索吗?预期的行为将是返回带有 [FALSE TRUE] 的值(与“not_available”和“testthat”相关)。
I want to check if a bunch of packages are correctly installed (to make a dockerfile crash and not silently continue).
I tried a sapply on a list of package names, and this is what happens.
Note that:
- 'not_available' is not installed :)
- 'testthat' is installed.
# to show the invisible output:
> show <- function(x) {cat(x, '\n')}
> show(require('not_available'))
Loading required package: not_available
FALSE
Warning message:
In library(package, lib.loc = lib.loc, character.only = TRUE, logical.return = TRUE, :
there is no package called ‘not_available’
So far so good, precisely as expected.
> show(require('testthat'))
TRUE
perfect...
> sapply(c('not_available', 'testthat'), require, warn.conflicts = FALSE, quietly = TRUE)
not_available testthat
FALSE FALSE
Warning messages:
1: In if (!loaded) { :
the condition has length > 1 and only the first element will be used
2: In if (!loaded) { :
the condition has length > 1 and only the first element will be used
Wuuut...
why does it warn us? And why is the output incorrect?
> sapply(c('not_available', 'testthat'), function(x) {x})
not_available testthat
"not_available" "testthat"
Oke, so sapply is not betraying me...
> sapply(c('not_available', 'testthat'), function(pkg) {require(pkg, warn.conflicts = FALSE, quietly = TRUE)})
not_available testthat
FALSE FALSE
Eeeh, now the warning is gone, but the answer.... still wrong... why?
Any clue? Expected behaviour would be a returned value with [FALSE TRUE] (relating to 'not_available' and 'testthat').
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
好吧,这是由于特殊的“库”和“要求”行为也可以使用未加引号的值。因此,以下内容将演示失败:
解决方案是提供“character.only”
即可正常工作。
Alright, it is due to the special 'library' and 'require' behaviour to also work with unquoted values. So the following will demonstrate the failure:
The solution is to provide 'character.only'
works just fine.