R 函数“require”在“sapply”中无法按预期工作

发布于 2025-01-10 17:03:32 字数 1507 浏览 1 评论 0原文

我想检查一堆软件包是否已正确安装(以使 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

电影里的梦 2025-01-17 17:03:32

好吧,这是由于特殊的“库”和“要求”行为也可以使用未加引号的值。因此,以下内容将演示失败:

pkgs <- 'testthat'
require(pkgs)
Loading required package: pkgs
Warning message:
In library(package, lib.loc = lib.loc, character.only = TRUE, logical.return = TRUE,  :
  there is no package called ‘pkgs’

解决方案是提供“character.only”

sapply(c('not_available', 'testthat'), require, character.only = TRUE)
not_available      testthat 
        FALSE          TRUE

即可正常工作。

Alright, it is due to the special 'library' and 'require' behaviour to also work with unquoted values. So the following will demonstrate the failure:

pkgs <- 'testthat'
require(pkgs)
Loading required package: pkgs
Warning message:
In library(package, lib.loc = lib.loc, character.only = TRUE, logical.return = TRUE,  :
  there is no package called ‘pkgs’

The solution is to provide 'character.only'

sapply(c('not_available', 'testthat'), require, character.only = TRUE)
not_available      testthat 
        FALSE          TRUE

works just fine.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文