2

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

  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

有任何想法吗?

4

1 回答 1

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 中,由用户手动调用。

于 2013-07-30T06:48:44.653 回答