2013-07-21 22 views
7

मैं अपनी सभी निर्भरताओं के साथ एक पैकेज को अनलोड करने का प्रयास कर रहा हूं। जिस समस्या में मैं चल रहा हूं वह वह क्रम है जिसमें निर्भरता को उतारना है। क्योंकि निर्भरता रिकर्सिव हैं, वे केवल निर्भरता पेड़ में नीचे-अप से अनलोड किए जा सकते हैं।पैकेज और सभी निर्भरताओं को अनलोड करें

क्या इसे पूरा करने के लिए आर में कोई आसान या मूल तरीका है? क्या में पहली बार जाने के नीचे मैं पूरा करने के लिए करना चाहते हैं:

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

मैं 'हत्यारा आर; इसके बजाए आर '। प्रक्रियाएं सस्ते हैं। –

+0

हाहा हां, यह पहले से ही एक विंडोज विशिष्ट समाधान है। यूनिक्स पर हम वास्तव में केवल एक अस्थायी कांटा का उपयोग कर सकते हैं। – Jeroen

+1

हर मिनट के लिए आप एक विंडोज़ वार्ट के आसपास इंजीनियरिंग खर्च करते हैं, $ 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

मुझे लगता है कि दूसरी तरफ लूप के लिए होना चाहिए (मैं seq_along (tounload) में) – PolBM

संबंधित मुद्दे