2013-07-21 55 views
7

我想卸載一個軟件包及其所有的依賴關係。我遇到的問題是卸載依賴關係的順序。因爲依賴關係是遞歸的,所以它們只能從依賴關係樹中的自底向下卸載。卸載一個軟件包和所有依賴關係

在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 
+3

我會'killall R; R'來代替。進程很便宜。 –

+0

哈哈是的,這已經是一個特定於Windows的解決方案。在unix上,我們可以確實使用臨時叉。 – Jeroen

+1

對於每一分鐘你花在一個Windows疣周圍的工程,$ Deity殺死一隻小貓。只是不要這樣做。 –

回答

1

這對我的作品 - 一個有點粗糙,但能夠完成任務。

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)]); 
    } 
}); 
+0

我認爲for循環應該是for(我在seq_along(tounload)中) – PolBM

相關問題