はじめに
MacでのR開発環境整備記録
WindowsでのR環境は整備していたが、Macでの環境整備はできていなかったので整備することに。
Renvでのインストールで少してこずったので記録。
anyenv
anyenvでバージョン管理できる状態を整えた前提で進める。
複数のプログラミング言語を扱う場合にはanyenvで一括管理すると便利。
以前はこちらの旧レポジトリ参照だったが、
git clone https://github.com/riywo/anyenv ~/.anyenv
新レポジトリが作成されていたので、新たにインストールする場合はこちらから
git clone https://github.com/anyenv/anyenv ~/.anyenv
パスを通す
bashで通す場合
echo 'export PATH="$HOME/.anyenv/bin:$PATH"' >> ~/.bash_profile
zshで通す場合
echo 'export PATH="$HOME/.anyenv/bin:$PATH"' >> ~/.zshrc
セットアップ
~/.anyenv/bin/anyenv init
anyenv updateコマンドでenv系まとめてアップデートができるようにプラグイン導入
mkdir -p $(anyenv root)/plugins
git clone https://github.com/znz/anyenv-update.git $(anyenv root)/plugins/anyenv-update
Renvの導入
Renvをインストール
anyenv install Renv
Rの最新版をインストールしてみる。
~ $ Renv install 4.0.2
Configured with: --prefix=/Library/Developer/CommandLineTools/usr --with-gxx-include-dir=/Library/Developer/CommandLineTools/SDKs/MacOSX.sdk/usr/include/c++/4.2.1
Downloading https://cran.r-project.org/src/base/R-4/R-4.0.2.tar.gz...
Installing R-4.0.2...
BUILD FAILED
Inspect or clean up the working tree at /var/folders/5g/pn3jnbgx61l40hxzntszrwn80000gn/T/R-build.20200912145928.35439
Results logged to /var/folders/5g/pn3jnbgx61l40hxzntszrwn80000gn/T/R-build.20200912145928.35439.log
Last 10 log lines:
checking for f77... no
checking for frt... no
checking for pgf77... no
checking for cf77... no
checking for fort77... no
checking for fl32... no
checking for af77... no
checking whether we are using the GNU Fortran compiler... no
checking whether accepts -g... no
configure: error: No Fortran compiler found
コンパライラがみつからないとのこと。
最近Mac上で色々クリーンアップした関係かなと思われる。
brew install gcc
gccを入れて再トライ
~ $ Renv install 4.0.2
Configured with: --prefix=/Library/Developer/CommandLineTools/usr --with-gxx-include-dir=/Library/Developer/CommandLineTools/SDKs/MacOSX.sdk/usr/include/c++/4.2.1
Downloading https://cran.r-project.org/src/base/R-4/R-4.0.2.tar.gz...
Installing R-4.0.2...
BUILD FAILED
Inspect or clean up the working tree at /var/folders/5g/pn3jnbgx61l40hxzntszrwn80000gn/T/R-build.20200912150355.38735
Results logged to /var/folders/5g/pn3jnbgx61l40hxzntszrwn80000gn/T/R-build.20200912150355.38735.log
Last 10 log lines:
checking whether wctrans exists and is declared... yes
checking whether iswblank exists and is declared... yes
checking whether wctype exists and is declared... yes
checking whether iswctype exists and is declared... yes
checking for wctrans_t... yes
checking for mbstate_t... yes
checking for ICU... no
checking for ucol_open in -licucore... yes
checking for X... no
configure: error: --with-x=yes (default) and X11 headers/libs are not available
https://cran.r-project.org/bin/macosx/
を参照すると、
R 4.0.2 binary for macOS 10.13 (High Sierra) and higher, signed and notarized package. Contains R 4.0.0 framework, R.app GUI 1.72 in 64-bit for Intel Macs, Tcl/Tk 8.6.6 X11 libraries and Texinfo 6.7. The latter two components are optional and can be ommitted when choosing "custom install", they are only needed if you want to use the tcltk R package or build package documentation from sources.
との記載を発見。
Xquartzをインストール
brew cask install xquartz
再トライ
~ $ Renv install 4.0.2
Configured with: --prefix=/Library/Developer/CommandLineTools/usr --with-gxx-include-dir=/Library/Developer/CommandLineTools/SDKs/MacOSX.sdk/usr/include/c++/4.2.1
Downloading https://cran.r-project.org/src/base/R-4/R-4.0.2.tar.gz...
Installing R-4.0.2...
BUILD FAILED
Inspect or clean up the working tree at /var/folders/5g/pn3jnbgx61l40hxzntszrwn80000gn/T/R-build.20200912153001.59557
Results logged to /var/folders/5g/pn3jnbgx61l40hxzntszrwn80000gn/T/R-build.20200912153001.59557.log
Last 10 log lines:
checking bzlib.h usability... yes
checking bzlib.h presence... yes
checking for bzlib.h... yes
checking if bzip2 version >= 1.0.6... yes
checking whether bzip2 support suffices... yes
checking for lzma_version_number in -llzma... yes
checking lzma.h usability... no
checking lzma.h presence... no
checking for lzma.h... no
configure: error: "liblzma library and headers are required"
またエラー
liblzmaが必要とのことなので、これもインストール
brew intall xz
やっとのことでインストール成功!
~ $ Renv install 4.0.2
Configured with: --prefix=/Library/Developer/CommandLineTools/usr --with-gxx-include-dir=/Library/Developer/CommandLineTools/SDKs/MacOSX.sdk/usr/include/c++/4.2.1
Downloading https://cran.r-project.org/src/base/R-4/R-4.0.2.tar.gz...
Installing R-4.0.2...
Installed R-4.0.2 to /Users/yuta/.anyenv/envs/Renv/versions/4.0.2
Rのバージョンを設定
Renv global 4.0.2
結局Renv導入必要だったものまとめ
結局何が必要だったのかまとめると以下
brew install gcc
brew cask install xquartz
brew intall xz
VScodeでのR参照設定
VScodeのインストール
brew cask install visual-studio-code
RenvでインストールしたRをextensionが認識できるようにVScodeの設定に以下を記載。
{
"r.rterm.mac": "~/.anyenv/envs/Renv/shims/R"
}
パスは間違っていないはずだが以下のエラー
Cannot find R client. Please check R path in preferences and reload.
チルダの部分を/Users/usernameで書き直すと実行可能に。
{
"r.rterm.mac": "/Users/username/.anyenv/envs/Renv/shims/R"
}
現状チルダをうまく認識してくれないらしい。
おわりに
インストールでBUILD FAILEDした際に出力されるログが、
Last 10 log lines:
と、10行しか出してくれないので、一個ずつエラー対処していくのが大変でした。
まとめて出してくれるとありがたい。
resolveのための推奨コマンドも出してくれたらよい良いなと感じました。