Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Unload a package and all dependencies

I am trying to unload a package with all of its dependencies. The problem that I am running into is the order in which to unload dependencies. Because dependencies are recursive, they can only be unloaded from bottom-up in the dependency tree.

Is there an easy or native way in R to accomplish this? Below a first go in what I would like to accomplish:

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) 
}

But it results in:

> 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
like image 851
Jeroen Ooms Avatar asked Jul 21 '13 13:07

Jeroen Ooms


People also ask

How do I unload a package in R?

To detach a package in R, we can simply use the detach function. But we need to remember that once the package will be detached there is no way to use any of the functions of that particular package.

How do I see loaded packages in R?

1 Answer. You can use the packageVersion() function to print version information about the loaded packages. To print the version information about R, the OS and attached or loaded packages, use the sessionInfo() function.


1 Answers

This works for me -- a bit crude but gets the job done.

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)]);
  }
});
like image 141
mlegge Avatar answered Oct 22 '22 11:10

mlegge