我安装了多个版本的R(2.15和3.0.1),我经常在它之间切换。我想确保当我在一个版本中安装一个软件包时,它也会出现在另一个版本中(如果可能的话),所以我试图设置以下系统:
为此,我在.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 :(得分: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中,并由用户手动调用。