在需要/加载软件包后,是否可以覆盖阻止软件包的重新安装? [英] Possible to override the blocking of a package's (re-)installation after it has been required/loaded?
问题描述
是否有可能在需要/加载软件包后覆盖对软件包(重新)安装的阻止?
Is it possible to override the blocking of a package's (re-)installation after it has been required/loaded?
我了解到,一旦使用了软件包,阻止对加载软件包的相同库进行真正的" 重新安装是很有意义的.但是我的用例有些不同
I understand that blocking a "true" re-installation to the same library that the package was loaded from makes perfect sense once the package is in use. But my use case is a bit different
我喜欢使用沙盒库"来测试自己的程序包的想法.除了要测试的软件包之外,这些沙箱库还包含干净基础R安装的所有基本软件包 plus ,这些contrib软件包对于使我的整个软件包构建和测试框架正常工作是必需的(例如stringr
等).但是,我的框架从标准库加载了后一个程序包,然后然后需要将它们安装到沙箱库中-该库被阻止了.因此,我的问题是是否有可能将其覆盖,因为我并没有真正实现人们在实现该块时的想法.
I like the idea of having a "sandbox library" to test own packages. Besides the package(s) to test, those sandbox libraries contain all base packages of a clean base R installation plus some contrib packages necessary in order for my whole package building and testing framework to work (e.g. digest
, stringr
etc.). However, my framework loads the latter packages from the standard lib and then needs to install them to the sandbox lib - which is blocked. Hence my question if it is possible to override this as I'm not really doing what people had in mind when implementing the block.
指定两个库的路径
lib <- file.path(R.home(), "library")
lib.sandbox <- file.path(tempdir(), "library")
创建沙箱库
dir.create(lib.sandbox, showWarnings=FALSE)
将软件包安装到标准库中并加载
Install a package to the standard library and load it
install.packages("digest", lib=lib)
require("digest", lib.loc=lib)
然后还将其安装到沙箱库中
Then also install it to the sandbox library
> install.packages("digest", lib=lib.sandbox)
Warning: package 'digest' is in use and will not be installed
我的框架 之后也加载了一些应该安装到沙箱库中的软件包,因此我无法将"install-to-sandbox-lib"步骤放在初始加载步骤.
My framework figures certain stuff out after certain packages that should also be installed to the sandbox lib are loaded, so I can't put the "install-to-sandbox-lib" step before the initial loading step.
推荐答案
以下是您在答案中建议的一般版本.这将在安装之前先卸载软件包,然后再从同一位置重新加载软件包.
Here's a general version of what you suggest in your answer. This will unload the package before installing and reload the package from the same location afterward.
install.packages.sandbox <- function(pkgs, lib, repos=getOption("repos"), ...) {
if (is.null(repos))
stop("Can't install from source. Need package name.")
pkg.pos <- grep(pkgs, search())
pkg.path <- searchpaths()[grep(pkgs, searchpaths())]
in.use <- length(pkg.pos) > 0
# detach
if (in.use) do.call(detach,
list(pkg.pos),
envir=.GlobalEnv)
# install
utils::install.packages(pkgs, lib, repos, ...)
# re-attach from original library location
if (in.use) library(pkgs,
character.only=TRUE,
lib.loc=.libPaths()[sapply(.libPaths(), grepl, pkg.path)])
}
这篇关于在需要/加载软件包后,是否可以覆盖阻止软件包的重新安装?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!