2013-07-21 38 views
7

Tôi đang cố gắng dỡ bỏ một gói với tất cả các phụ thuộc của nó. Vấn đề mà tôi đang gặp phải là thứ tự để dỡ bỏ các phụ thuộc. Bởi vì các phụ thuộc đệ quy, chúng chỉ có thể được nạp từ dưới lên trong cây phụ thuộc.Dỡ bỏ một gói và tất cả các phụ thuộc

Có cách nào dễ dàng hoặc bản địa trong R để thực hiện việc này không? Dưới một đi đầu tiên trong những gì tôi muốn đạt được:

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

Nhưng nó kết quả trong:

> 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

Tôi muốn 'killall R; Thay vào đó. Quy trình có giá rẻ. –

+0

Haha có, đây là một giải pháp cụ thể cho windows. Trên unix chúng tôi chỉ có thể sử dụng một ngã ba tạm thời thực sự. – Jeroen

+1

Đối với mỗi phút bạn dành kỹ thuật xung quanh một mụn cóc Windows, $ Deity giết chết một con mèo con. Chỉ cần không làm điều đó. –

Trả lời

1

này làm việc cho tôi - một chút dầu thô nhưng được công việc làm.

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

Tôi nghĩ rằng vòng lặp for trong lần thứ hai trong khi nên cho (i trong seq_along (tounload)) – PolBM

Các vấn đề liên quan