OSXでRのパッケージtidyverseのインストールに詰まった

AdventCalendar 13日目！
人手足りないけど**だんないよ**精神でいくよ！！！

# 概要
MacでR言語に入門する際に便利パッケージである`tidyverse`のインストールに詰まったので対処法

# 環境
### OS

```sh
ProductName:	Mac OS X
ProductVersion:	10.13.6
```
### XCode

```sh
Xcode 9.3
Build version 9E145
```

### R

```r
R version 3.5.1 (2018-07-02) -- "Feather Spray"
Copyright (C) 2018 The R Foundation for Statistical Computing
Platform: x86_64-apple-darwin17.6.0 (64-bit)
```

# エラーコード
肝心のエラーコード

```r
>install.packages("tidyverse")
Installing package into ‘/usr/local/lib/R/3.5/site-library’
(as ‘lib’ is unspecified)
also installing the dependencies ‘rvest’, ‘xml2’

 URL 'https://cran.rstudio.com/src/contrib/rvest_0.3.2.tar.gz' を試しています 
Content type 'application/x-gzip' length 1597137 bytes (1.5 MB)
==================================================
downloaded 1.5 MB

 URL 'https://cran.rstudio.com/src/contrib/xml2_1.2.0.tar.gz' を試しています 
Content type 'application/x-gzip' length 251614 bytes (245 KB)
==================================================
downloaded 245 KB

 URL 'https://cran.rstudio.com/src/contrib/tidyverse_1.2.1.tar.gz' を試しています 
Content type 'application/x-gzip' length 61647 bytes (60 KB)
==================================================
downloaded 60 KB

* installing *source* package ‘xml2’ ...
**  パッケージ ‘xml2’ の解凍および MD5 サムの検証に成功しました 
Found pkg-config cflags and libs!
Using PKG_CFLAGS=-I/usr/include/libxml2
Using PKG_LIBS=-L/usr/lib -lxml2 -lz -lpthread -licucore -lm
------------------------- ANTICONF ERROR ---------------------------
Configuration failed because libxml-2.0 was not found. Try installing:
 * deb: libxml2-dev (Debian, Ubuntu, etc)
 * rpm: libxml2-devel (Fedora, CentOS, RHEL)
 * csw: libxml2_dev (Solaris)
If libxml-2.0 is already installed, check that 'pkg-config' is in your
PATH and PKG_CONFIG_PATH contains a libxml-2.0.pc file. If pkg-config
is unavailable you can set INCLUDE_DIR and LIB_DIR manually via:
R CMD INSTALL --configure-vars='INCLUDE_DIR=... LIB_DIR=...'
--------------------------------------------------------------------
ERROR: configuration failed for package ‘xml2’
* removing ‘/usr/local/lib/R/3.5/site-library/xml2’
Warning in install.packages :
  installation of package ‘xml2’ had non-zero exit status
ERROR: dependency ‘xml2’ is not available for package ‘rvest’
* removing ‘/usr/local/lib/R/3.5/site-library/rvest’
Warning in install.packages :
  installation of package ‘rvest’ had non-zero exit status
ERROR: dependencies ‘rvest’, ‘xml2’ are not available for package ‘tidyverse’
* removing ‘/usr/local/lib/R/3.5/site-library/tidyverse’
Warning in install.packages :
  installation of package ‘tidyverse’ had non-zero exit status

The downloaded source packages are in
	‘/private/var/folders/mf/h8zpddh14778rlv254chg2000000gs/T/RtmpO6u1wZ/downloaded_packages’
```

# 対処１
_ANTICONF ERROR_で「`libxml2`が入ってないよ」的なエラーが出たのでとりあえずインストール

```shell
$ brew install lib2xml
```
再実行するが、同じエラーが出る
何故？？？

# 対処２
調べていると、どうもXCode絡みのよう

- [tidyverse - Package installation in R fail on MacOS - Stack Overflow](https://stackoverflow.com/questions/50229781/package-installation-in-r-fail-on-macos) 
    - このサイトの**Olivier**さんが載せているコマンドを実行する

```sh
$ xcode-select --install
```
### 入った！！！

```r
> library(tidyverse)
─ Attaching packages ────────────── tidyverse 1.2.1 ─
✔ ggplot2 3.0.0     ✔ purrr   0.2.5
✔ tibble  1.4.2     ✔ dplyr   0.7.6
✔ tidyr   0.8.1     ✔ stringr 1.3.1
✔ readr   1.1.1     ✔ forcats 0.3.0
─ Conflicts ──────────────── tidyverse_conflicts() ─
✖ dplyr::filter() masks stats::filter()
✖ dplyr::lag()    masks stats::lag()
```
下の_Conflicts_に関してはデフォルトっぽいのでそのままでOK

# まとめ
そもそもXCodeのコマンドラインツールが入ってなかった
学習始めの環境構築で詰まるとモチベーションをグッと持っていかれる、、、