【问题标题】:For loop in .Rprofile calls itself recursively?.Rprofile 中的 for 循环递归调用自身?
【发布时间】:2013-07-30 04:40:02
【问题描述】:

我安装了多个版本的 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()

但是,当这段代码运行时,它会在utils::install.packages(notInstalled[i]) 上递归调用mirrorSetup(),我不知道为什么。

这是一些示例输出,表明它反复尝试安装它找到的第一个包 (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

有什么想法吗?

【问题讨论】:

  • 你只得到两个“Recursive Statement?”还是更多?如果只有两个,我会尝试用invisible(mirrorSetup()) 替换mirrorSetup()
  • 与其如此,为什么不将两个版本的 R 都指向一个公共库位置,以便您只安装一组软件包?在几个答案中查看建议here
  • @flodel 我得到无限,它不断重复,直到我用 ^C 终止命令。
  • @Thomas 我不认为从一个版本的 R 安装的库可以在另一个版本中工作?
  • @Manetheran 这可能取决于软件包,其中一些会向您发出有关更新的警告。您可以通过将一个版本中的整个库目录换成另一个版本来进行试验,看看会发生什么。

标签: r rprofile install.packages


【解决方案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 中,由用户手动调用。

【讨论】:

  • 查看将代码移入.First 函数是否有效。 .First 在启动时被自动调用,但在不同的时间点(在全局工作区文件.RData 被加载之后)。
  • 您也可以unlink("~/.Rinstalled") 第一次阅读它,这应该可以解决您的无限递归问题。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-12-06
  • 2016-07-18
  • 2017-03-13
  • 1970-01-01
相关资源
最近更新 更多