2013-07-30 26 views
2

我有多個版本的R安裝(2.15和3.0.1),我經常切換。我想確保當我在一個版本的安裝包,它也將出現在其他(如果可能),所以我試圖建立以下制度:For循環.Rprofile以遞歸方式調用自身?

  1. 當安裝包(任一版本)寫出一個CSV文件〜/ .Rinstalled,包含所有已安裝的軟件包
  2. 當一個新的R會話打開的列表,檢查該文件是否存在。
  3. 如果文件存在,請將該列表與正在運行的當前版本的R中安裝的軟件包進行比較。
  4. 嘗試安裝所有缺少的軟件包。

爲此,我有以下的代碼在我.Rprofile:

mirrorSetup <- function() { 
    cat("Recursive Statement?\n") 
    require(utils) 
    if(file.exists("~/.Rinstalled")) { 
    packages <- as.vector(read.csv("~/.Rinstalled")[,2]) 
    notInstalled <- packages[!(packages %in% rownames(installed.packages()))] 
    # Remove file on exit if we're in a different version of R. 
    if (length(notInstalled) > 0) { 
     on.exit({ 
     unlink("~/.Rinstalled") 
     }) 
    } 

    for (i in seq_along(notInstalled)) { 
     # Check if package installed via previous dependency in for loop 
     updated <- rownames(installed.packages()) 
     if (notInstalled[i] %in% updated) { 
     next 
     } 

     # Try to install via Cran first, then Bioconductor if that fails 
     tryCatch({ 
     utils::install.packages(notInstalled[i]) 
     }, error = function(e) { 
     try({ 
      source("http://bioconductor.org/biocLite.R") 
      biocLite(notInstalled[i]) 
     }, silent = TRUE) 
     }) 
    } 
    } 
} 

mirrorSetup() 

然而,此代碼運行時,它遞歸調用mirrorSetup()utils::install.packages(notInstalled[i]),我不知道爲什麼。

下面是一些示例輸出,顯示出更是多次試圖安裝它找到的第一個包(ade4

Recursive Statement? 
Loading required package: utils 
Trying to install ade4 from Cran... 
trying URL 'http://cran.ms.unimelb.edu.au/src/contrib/ade4_1.5-2.tar.gz' 
Content type 'application/x-tar' length 1375680 bytes (1.3 Mb) 
opened URL 
================================================== 
downloaded 1.3 Mb 

Recursive Statement? 
Loading required package: utils 
Trying to install ade4 from Cran... 
trying URL 'http://cran.ms.unimelb.edu.au/src/contrib/ade4_1.5-2.tar.gz' 
Content type 'application/x-tar' length 1375680 bytes (1.3 Mb) 
opened URL 
================================================== 
downloaded 1.3 Mb 

任何想法?

+0

你只能得到僅有的兩個「'遞歸聲明?'」或者多一點呢?如果只有兩個,我會嘗試用'invisible(mirrorSetup())'替換'mirrorSetup()'。 – flodel

+0

與此不同,爲什麼不將R的兩個版本都指向一個通用的庫位置,以便只安裝一組軟件包?請參閱幾個答案中的建議[此處](http://stackoverflow.com/questions/1401904/painless-way-to-install-a-new-version-of-r-on-windows)。 – Thomas

+0

@flodel我得到無限的,它不斷重複,直到我用^ C殺死命令。 –

回答

1

所以,我已經有大約一齣戲,它是不可能做什麼,我想。函數install.packages在調用時會重新加載.Rprofile。例如,如果我做到以下幾點:

創建臨時.Rprofile:

cat(".Rprofile loaded!\n") 

負載R:

R version 3.0.0 (2013-04-03) -- "Masked Marvel" 
Copyright (C) 2013 The R Foundation for Statistical Computing 
Platform: x86_64-apple-darwin12.3.0 (64-bit) 

R is free software and comes with ABSOLUTELY NO WARRANTY. 
You are welcome to redistribute it under certain conditions. 
Type 'license()' or 'licence()' for distribution details. 

    Natural language support but running in an English locale 

R is a collaborative project with many contributors. 
Type 'contributors()' for more information and 
'citation()' on how to cite R or R packages in publications. 

Type 'demo()' for some demos, 'help()' for on-line help, or 
'help.start()' for an HTML browser interface to help. 
Type 'q()' to quit R. 

.Rprofile loaded 
> install.packages("ade4") 
--- Please select a CRAN mirror for use in this session --- 
trying URL 'http://cran.ms.unimelb.edu.au/src/contrib/ade4_1.5-2.tar.gz' 
Content type 'application/x-tar' length 1375680 bytes (1.3 Mb) 
opened URL 
================================================== 
downloaded 1.3 Mb 

.Rprofile loaded 

顯示該.Rprofile在軟件包安裝再次讀入。

雖然包鏡像的過程中不能以這種方式被自動化,功能仍可以留在.Rprofile,和由用戶手動調用。

+1

查看是否將代碼移到'.First'函數中。 '。第一個'在啓動時自動調用,但在不同的位置(全局工作區文件'.RData'加載後)。 –

+1

你也可以在第一次讀取它時立即解除鏈接(「〜/ .Rinstalled」),這應該解決你的無限遞歸。 – flodel