我正在尝试卸载一个包含所有依赖项的包。我遇到的问题是卸载依赖项的顺序。因为依赖是递归的,所以它们只能在依赖树中自下而上卸载。
R中是否有一种简单或原生的方式来实现这一点?下面首先介绍我想要完成的工作:
eval_current <- function(expr, envir=parent.frame(), timeout=60){
#set the timeout
setTimeLimit(elapsed=timeout, transient=TRUE);
#currently loaded packages
currentlyattached <- search();
currentlyloaded <- loadedNamespaces();
on.exit({
#reset time limit
setTimeLimit(cpu=Inf, elapsed=Inf, transient=FALSE);
#try to detach packages that were attached during eval
nowattached <- search();
todetach <- nowattached[!(nowattached %in% currentlyattached)];
for(i in seq_along(todetach)){
try(detach(todetach[i], unload=TRUE, character.only=TRUE, force=TRUE));
}
#try to unload packages that are still loaded
nowloaded <- loadedNamespaces();
tounload <- nowloaded[!(nowloaded %in% currentlyloaded)];
for(i in seq_along(tounload)){
try(unloadNamespace(tounload[i]));
}
});
eval(expr, envir)
}
但这会导致:
> eval_current({library(ggplot2); qplot(rnorm(100));})
Error in unloadNamespace(tounload[i]) :
namespace ‘colorspace’ is imported by ‘munsell’ so cannot be unloaded
Error in unloadNamespace(tounload[i]) :
namespace ‘dichromat’ is imported by ‘scales’ so cannot be unloaded
Error in unloadNamespace(tounload[i]) :
namespace ‘grid’ is imported by ‘gtable’ so cannot be unloaded
Error in unloadNamespace(tounload[i]) :
namespace ‘labeling’ is imported by ‘scales’ so cannot be unloaded
Error in unloadNamespace(tounload[i]) :
namespace ‘munsell’ is imported by ‘scales’ so cannot be unloaded