【发布时间】:2013-07-30 04:40:02
【问题描述】:
我安装了多个版本的 R(2.15 和 3.0.1),我经常在它们之间切换。我想确保当我在一个版本中安装一个包时,它也会出现在另一个版本中(如果可能的话),所以我尝试设置以下系统:
- 安装软件包时(任一版本),写出一个 csv 文件 ~/.Rinstalled,其中包含所有已安装软件包的列表
- 打开新的 R 会话时,检查该文件是否存在。
- 如果文件存在,将该列表与安装在当前运行的 R 版本中的包进行比较。
- 尝试安装所有缺失的软件包。
为此,我的 .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