LoginSignup
6
5

More than 3 years have passed since last update.

RStudio Package Managerからコンパイル済パッケージをR on Ubuntuにインストールする

Last updated at Posted at 2020-07-09

サマリ

  • RStudio Package Manager ( https://packagemanager.rstudio.com ) を使うと、Linux (Ubuntu, CentOS/RHEL, openSUSE/SLES) 上の R (3.4 or later) にコンパイル済のバイナリパッケージをインストールすることができる
  • 過去のバージョンのパッケージを導入することもでき、再現性の確保に役立つ(2017-10-10が一番古い模様)
  • RStudio上では、options(repos = "...") に生成されたURLを設定するだけで使用可能だった
  • RStudio以外で使用するためには、HTTP User-Agent を設定する必要がある

RStudio Package Manager ?

Rで解析を行う際に、Dockerなどを使って手元のPCにRStudio Serverを立てる方法は一見面倒ですが

  • Windowsの文字コード問題の回避:
    Shift-JIS(システム、パス)とUTF-8(スクリプト、Rmarkdown)の混在によるトラブルを回避できる
  • 再現性の確保:
    R本体やパッケージのバージョンを固定、共有することができる

といったメリットが個人の作業であっても得られます。
その際のベースとして便利な rocker project による rocker/tidyverse などの構築済みイメージでは、これまで MRAN (Microsoft R Application Network) のスナップショットをインストール元とすることでR本体やパッケージのバージョンが固定されていました。しかし、最近 rocker-org/rocker-versioned2 (Github) で開発されているR 4.0以降の新しいイメージでは、MRANは使用されておらず代わりに "RStudio Package Manager" というものが使われているようです。

'Public' RStudio Package Manager ?!

https://rstudio.com/products/package-manager/ を見る限りではエンタープライズ向けのようで、諦めて自分用に使い慣れたMRANを使ったDocker imageを作ることを検討していた矢先に、Twitterのタイムラインで次のようなtweetを見ました。

告知のBlog post によると、コミュニティ向けに

  • Access to pre-compiled packages on Linux via install.packages resulting in significantly faster package install times on Linux systems including cloud servers, CI/CD systems, and Docker containers.
  • Historical checkpoints for CRAN enabling reproducible work, and even time travel, by freezing package dependencies with a one-line repository option.
  • Expanded Windows support for older versions of R, allowing you to access the latest versions of packages on older versions of R without compiling from source.

が提供されるとのことです。特定時点のしかもLinux向けコンパイル済パッケージが利用できるということで、コンパイルが必要なMRAN snapshotからの乗り換えを検討することにしました。

Step 0. 検証環境

筆者の環境ではまだWSL2を導入できていないため、今回は Windows 10 Home (1909) にインストールした Docker Toolbox で検証しました。

RStudio Package Managerが対応しているLinuxディストリビューションは現時点で

  • Ubuntu 16.04 (Xenial)
  • Ubuntu 18.04 (Bionic)
  • Ubuntu 20.04 (Focal)
  • CentOS/RHEL 7
  • CentOS/RHEL 8
  • openSUSE 42/SLES 12
  • openSUSE 15/SLES 15

とのことで、Debian 10 (buster) がベースの rocker/tidyverse:3.6.3 などでは使えないようです。
今回は ubuntu:18.04 の公式イメージをベースに R 4.0.2RStudio server 1.3.959 をインストールしたイメージを準備しました。

Step 1. RStudio上での利用

https://packagemanager.rstudio.com/ の [Get Started] からweb clientに入り、[Setup] ページで

  • "CLIENT OS" を "Ubuntu 18.04 (Bionic)"
  • "Repository URL" の下で "Freeze to June 29, 2020" (latestからFreezeに変えたあとで日付を選択)
  • You can also choose to use source or binary packages."Binary"

とすると、https://packagemanager.rstudio.com/all/__linux__/bionic/297 というURLが表示されます。これを options(repos = "...") で指定すれば良いようです。
(今回は、[Setup] ページにある RStudio の Tools - General Option - Packages に入力する方法はうまく行かなかったので、~/.Rprofile で設定しました)

> install.packages("pROC")
Installing package into ‘/usr/local/lib/R/site-library’
(as ‘lib’ is unspecified)
 URL 'https://packagemanager.rstudio.com/all/__linux__/bionic/297/src/contrib/pROC_1.16.2.tar.gz' を試しています 
Content type 'binary/octet-stream' length 1340623 bytes (1.3 MB)
==================================================
downloaded 1.3 MB

* installing *binary* package ‘pROC’ ...
* DONE (pROC)

The downloaded source packages are in
    ‘/tmp/Rtmp1q0B5U/downloaded_packages’

きちんと installing *binary* package ... と表示されコンパイルもなしでインストールが完了しました。
([Setup] ページ中盤の Install System Prerequisites for the Repo’s Packages を参考に、必要なライブラリなどを予めインストールしておきます)

Step 2. RStudio外での利用

今回の最終目標は、Docker imageをビルドする際にコンパイル済パッケージを利用することです。
Step 1 で ~/.Rprofile にRStudio Package Managerのレポジトリ情報を記載しているので、Rscript -e "install.packages(...)" でもインストールができることを期待してRStudioを抜けてシェルから確認したところ、

$ Rscript -e "install.packages('pROC')"
 パッケージを ‘/usr/local/lib/R/site-library’ 中にインストールします
 (‘lib’ が指定されていないため)
 URL 'https://packagemanager.rstudio.com/all/__linux__/bionic/297/src/contrib/pROC_1.16.2.tar.gz' を試しています
Content type 'binary/octet-stream' length 371082 bytes (362 KB)
==================================================
downloaded 362 KB

* installing *source* package ‘pROC’ ...
**  パッケージ ‘pROC’ の解凍および MD5 サムの検証に成功しました
** using staged installation
** libs
g++ -std=gnu++11 -I"/usr/share/R/include" -DNDEBUG  -I'/usr/local/lib/R/site-library/Rcpp/include'
-fpic  -g -O2 -fdebug-prefix-map=/build/r-base-aGvNeb/r-base-4.0.0=. -fstack-protector-strong 
-Wformat -Werror=format-security -Wdate-time -D_FORTIFY_SOURCE=2 -g  -c RcppExports.cpp -o RcppExports.o
(以下略)

今度は installing *source* package ... と表示されコンパイルが始まってしまいました。

2-2. HTTPUserAgent の設定

改めて FAQ for Public RStudio Package Manager を読むと、

Note that your R environment may require additional configuration steps to use binary packages from RStudio Package Manager, particularly on Linux.

と注意が書かれていました。ドキュメントをたどっていくと、

Configuring the R User Agent Header

Package Manager serves the appropriate binary package based on the R version and OS in the user's User-Agent request header.
R users or RStudio Server admins may need to additionally configure the user agent in R if using:

  • R 3.6 or later
  • A non-default download method such as "curl" or "wget"

https://docs.rstudio.com/rspm/admin/serving-binaries/#binaries-r-configuration-linux

とのことで、User-Agent リクエストヘッダを修正する必要があるようです。修正内容は同じページに記載されており、

# Set the default HTTP user agent
options(HTTPUserAgent = sprintf("R/%s R (%s)", getRversion(), paste(getRversion(), R.version$platform, R.version$arch, R.version$os)))

これを指示通り ~/.Rprofile に追加したところ、RStudio外でもバイナリパッケージがインストールできるようになりました。littlerinstall2.r などの便利スクリプトも利用可能です。

options("HTTPUserAgent") の内容

環境 User-Agent 文字列
変更前の素の R 4.0.2 R (4.0.2 x86_64-pc-linux-gnu x86_64 linux-gnu)
変更前の R 4.0.2 on RStudio 1.3 RStudio Server (1.3.959); R (4.0.2 x86_64-pc-linux-gnu x86_64 linux-gnu)
変更後 (共通) R/4.0.2 R (4.0.2 x86_64-pc-linux-gnu x86_64 linux-gnu)"

問題なく設定できているかは、診断スクリプトが提供されています。

source("https://docs.rstudio.com/rspm/admin/check-user-agent.R")

上記を実行して Success! Your user agent is correctly configured. と表示されれば完了です。

使用感

同じような内容のDockerfileからのイメージ作成が早くなった(必要パッケージ等のダウンロード時間が多いのでネットワーク状況にも左右されますが)ことに加え、これまでは Docker Toolbox の初期設定(1 core, メモリ 1GiB)ではメモリ不足でコンパイルが止まってしまっていた Prophet などの大きなパッケージも簡単にインストールできるようになりました。

※ コミュニティ向けの "Public" Package Manager ということですが、ライセンスの記載などを見つけられませんでした。
※ 自分で試してみたことの備忘録的な内容ですが、誤りなどありましたらご指摘下さい。

6
5
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
6
5