我有多個版本的R安裝(2.15和3.0.1),我經常切換。我想確保當我在一個版本的安裝包,它也將出現在其他(如果可能),所以我試圖建立以下制度:For循環.Rprofile以遞歸方式調用自身?
- 當安裝包(任一版本)寫出一個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()
然而,此代碼運行時,它遞歸調用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
任何想法?
你只能得到僅有的兩個「'遞歸聲明?'」或者多一點呢?如果只有兩個,我會嘗試用'invisible(mirrorSetup())'替換'mirrorSetup()'。 – flodel
與此不同,爲什麼不將R的兩個版本都指向一個通用的庫位置,以便只安裝一組軟件包?請參閱幾個答案中的建議[此處](http://stackoverflow.com/questions/1401904/painless-way-to-install-a-new-version-of-r-on-windows)。 – Thomas
@flodel我得到無限的,它不斷重複,直到我用^ C殺死命令。 –