2

私は R の複数のバージョン (2.15 と 3.0.1) をインストールしており、頻繁に切り替えています。パッケージを 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()

ただし、このコードを実行すると、再帰的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

何か案は?

4

1 に答える 1

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 に残しておいて、ユーザーが手動で呼び出すことができます。

于 2013-07-30T06:48:44.653 に答える