for循环.Rprofile递归调用自身?

时间:2013-07-30 04:40:03

标签: r rprofile install.packages

我安装了多个版本的R(2.15和3.0.1),我经常在它之间切换。我想确保当我在一个版本中安装一个软件包时,它也会出现在另一个版本中(如果可能的话),所以我试图设置以下系统:

  1. 安装软件包时(在任一版本中)写出一个csv文件,〜/ .Rinstalled,其中包含所有已安装软件包的列表
  2. 打开新的R会话时,请检查该文件是否存在。
  3. 如果文件存在,请将该列表与正在运行的当前版本的R中安装的软件包进行比较。
  4. 尝试安装所有丢失的软件包。
  5. 为此,我在.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
    

    有什么想法吗?

1 个答案:

答案 0 :(得分: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中,并由用户手动调用。