LoginSignup
11

More than 5 years have passed since last update.

Mac に RStudio を導入する

Posted at

インストール

brew tap homebrew/science
sudo brew cask install xquartz
brew install r
sudo brew cask install rstudio

xquartz が password 要求するので入力する.
反応無く遅いが裏でインストールしているので待つ.

立ち上げる

Homebrew でインストールしたので, Alfred に /opt/homebrew-cask/Caskroom/ を登録して, Alfred から立ち上げ.

R Markdown を書く

依存パッケージである knitr を入れる.

install.packages("knitr")

Preference > Sweave から
Weave Rnw files using knitr に設定する.

 2014-09-12 21.16.02.png

新規ファイルから R Markdown を選択し, 書く.

Chunk

R の実行コード ( chunk という) を書くときは

```{r}
plot(cars)
```

とする.
一般的に Markdown でコードを示すときに

```r
plot(cars)
```

となるが, これに波括弧が付いた形だ.
波カッコの後に label (id の役割), chunk の options を指定する.

```{r, first_label, warning=TRUE}
plot(1)
```

また, 文章中に任意のコード片を埋め込むことも可能である.

hoge は `r hoge` である

などと書けば, hoge を評価する.
このとき, hoge オブジェクトがなければ出力時にエラーを返す.

TeX

また, TeX 記法を使うことも可能である.

$f(x) = ax^2 + bx + c$

 2014-09-12 21.28.57.png

これは mathjax で描画されている.

results="asis" オプションを付けると, kable 関数で表が出力できる.

plog 関数で図が出力できる.

 2014-09-12 21.32.11.png

ggplot2 などを使いたければ import して, qplot などの関数を使う.

設定の共有

opts_chunk$set(options)

で global な option が設定できる.

この option 評価専用の chunk を作って,

```{r, global_options, eval=FALSE}
opts_chunk$set(options)
```

などとファイル上部で書いておくと便利.

その他の Option -> R markdown(knitr)パッケージのchunk optionまとめ - My Life as a Mock Quant

Reference

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
11