8

我正在尝试卸载一个包含所有依赖项的包。我遇到的问题是卸载依赖项的顺序。因为依赖是递归的,所以它们只能在依赖树中自下而上卸载。

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
4

1 回答 1

2

这对我有用——有点粗糙,但可以完成工作。

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)]);
  }
});
于 2015-01-13T16:05:33.130 回答