gpt4 book ai didi

r - 不可重现的 R 包可用性检查

转载 作者:行者123 更新时间:2023-12-02 20:09:28 24 4
gpt4 key购买 nike

在检查软件包向量是否需要安装时,我遇到了一个有趣的错误。要求并卸载 lme4 命名空间会在第二次执行时出现错误,但仅当按特定顺序检查其他一些包时才会出现错误。

isInstalled <- function(package)    # is a package installed and usable?
{
loaded <- package %in% .packages()
out <- requireNamespace(package, quietly=F)
if(!loaded) try(unloadNamespace(package), silent=F)
out
}

isInstalled("car") # All return TRUE
isInstalled("nnet")
isInstalled("pbkrtest")
isInstalled("lme4")
isInstalled("nloptr")
isInstalled("lme4") # FALSE (only after commands above)
# no such symbol NLoptR_Optimize in package C:/__Rlibrary/nloptr/libs/x64/nloptr.dll
library(nloptr) # now fails, too

# Problem solved if nnet is checked before car (but not again after car)

我在 isInstalled 中做错了什么吗?

这可能与car的依赖结构有关。简化版: simplified car dependency graph

#install.packages(c("miniCRAN","igraph"))
d <- miniCRAN::makeDepGraph(c("car", "nnet", "pbkrtest", "lme4","nloptr"), suggests=FALSE)
plot(d) # for full dependency graph

最佳答案

R 的设计初衷并不是为了允许随意加载和卸载包。您可以尝试这样做,但不能保证成功(尽管经常会成功)。

来自?分离:

If a package has a namespace, detaching it does not by default unload the namespace (and may not even with unload = TRUE), and detaching will not in general unload any dynamically loaded compiled code (DLLs). Further, registered S3 methods from the namespace will not be removed. If you use library on a package whose namespace is loaded, it attaches the exports of the already loaded namespace. So detaching and re-attaching a package may not refresh some or all components of the package, and is inadvisable.

来自 ?devtools::unload:

This function attempts to cleanly unload a package, including unloading its namespace, deleting S4 class definitions and unloading any loaded DLLs. Unfortunately S4 classes are not really designed to be cleanly unloaded, and so we have to manually modify the class dependency graph in order for it to work - this works on the cases for which we have tested but there may be others. Similarly, automated DLL unloading is best tested for simple scenarios (particularly with useDynLib(pkgname) and may fail in other cases. If you do encounter a failure, please file a bug report at http://github.com/hadley/devtools/issues.

关于r - 不可重现的 R 包可用性检查,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45145722/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com