Jer*_*oen 8 r devtools package cran
我试图卸载包含所有依赖项的包.我遇到的问题是卸载依赖项的顺序.因为依赖项是递归的,所以它们只能在依赖关系树中从底部向上卸载.
在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)
}
Run Code Online (Sandbox Code Playgroud)
但它导致:
> 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
Run Code Online (Sandbox Code Playgroud)
这对我有用——有点粗糙,但可以完成工作。
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)];
while( ! length(todetach) == 0 ){
for(i in seq_along(todetach)){
suppressWarnings(tryCatch(detach(todetach[i], unload=TRUE, character.only=TRUE, force=TRUE),error = function(x) return(NA)))
}
nowattached <- search();
todetach <- sample(nowattached[!(nowattached %in% currentlyattached)]);
}
#try to unload packages that are still loaded
nowloaded <- loadedNamespaces();
tounload <- nowloaded[!(nowloaded %in% currentlyloaded)];
while( ! length(tounload) == 0 ){
for(i in seq_along(todetach)){
suppressWarnings(tryCatch(unloadNamespace(tounload[i]),error = function(x) return(NA)))
}
nowloaded <- loadedNamespaces();
tounload <- sample(nowloaded[!(nowloaded %in% currentlyloaded)]);
}
});
Run Code Online (Sandbox Code Playgroud)