私は R の複数のバージョン (2.15 と 3.0.1) をインストールしており、頻繁に切り替えています。パッケージを 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()
ただし、このコードを実行すると、再帰的mirrorSetup()
に onが呼び出されますが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
何か案は?