目次

概要

Rの集計、分析結果を Markdown形式で作成するための R Markdown というパッケージが提供されている。
レポートの形式は HTML の他に Word や PDF 等も対応しているが、当記事では HTML の生成についてのみ記載する。

インストール

とりあえず rmarkdown と knitr をインストール。
尚、word文書等を作成する場合は pandoc も必要だが、当記事では扱わない為、インストールしない。

install.packages("rmarkdown")
install.packages("knitr")
install.packages("reticulate")    # コードチャンクを python などの他言語で書く場合は必要

基本的な使い方

Markdown 形式のファイルを拡張子 Rmd で作成し、render するだけ。
※Rmd の記述方法等は後述。

.Rmd の作成

sample.Rmd

---
title: "Sample1"
author: "Daisuke.M"
date: '`r Sys.time()`'
output: html_document
---

```{r include = F, message = F, warning = F}
library(knitr)
```

```{r echo = F}
plot(iris$Sepal.Length, iris$Sepal.Width)
```

rmarkdown::render の実行

library(rmarkdown)
render("sample.Rmd")

.Rmd の記述方法

基本設定

Rmd の先頭で --- から --- の間に基本的な設定を YAML形式で記述する。

参考
https://rmarkdown.rstudio.com/lesson-9.html
https://bookdown.org/yihui/rmarkdown/html-document.html

---
title: "Sample1"
author: "Daisuke.M"
date: '`r Sys.time()`'
output:
  html_document:
    includes:
      in_header: header.html
      before_body: doc_prefix.html
      after_body: doc_suffix.html
---

コードチャンク

コードチャンクにオプションを設定する事で、挙動の微調整ができる。

参考
https://rmarkdown.rstudio.com/lesson-3.html
https://yihui.name/knitr/options/

基本的なオプション

オプション説明
includeコードの実行結果を出力内容に含めたくない場合は FALSE を指定する
echoコード自体を出力内容に含めたくない場合は FALSE を指定する
messageコード実行時の message による出力を出力内容に含めたくない場合は FALSE を指定する
warningコード実行時の warning による出力を出力内容に含めたくない場合は FALSE を指定する
fig.capグラフに付けたいキャプションを指定する(alt属性 及び pタグとして出力される)

使用例)

## 実行時のコードは出力しない
```{r echo = F}
plot(iris$Sepal.Length, iris$Sepal.Width)
```

## ファイルに出力
```{r echo = F, include = F}
png("plot1.png")
plot(iris$Sepal.Length, iris$Sepal.Width)
dev.off()
```

インラインコード

前後をバックスラッシュで囲む事によってインラインコードを記述可能。

参考
https://rmarkdown.rstudio.com/lesson-4.html

### 日付を出力
`r Sys.time()`

尚、基本設定の YAML 部分に書く場合は シングルクォートで括る必要がある。

---
  :
date: '`r Sys.time()`'
  :
---

パラメータ化

Rmd 側で使用する値をパラメータ化しておいて、render 時に呼び出し側から渡す事が出来る。
参考: https://rmarkdown.rstudio.com/lesson-6.html

TODO:

表の描画

knitr::kable を使用すると データフレームの内容を HTML 等の表(table)として描画する事ができる。
参考
https://rmarkdown.rstudio.com/lesson-7.html

```{r echo = F}
kable(head(iris, 10))
```

タブの描画

出力が縦長になりすぎる場合などは、タブ化できる。

参考
https://bookdown.org/yihui/rmarkdown/html-document.html#tabbed-sections

### タブの描画 {.tabset}

#### By Product

```{r echo = F}
plot(iris$Sepal.Length, iris$Sepal.Width)
```

#### By Region

```{r echo = F}
plot(iris$Petal.Length, iris$Petal.Width)
```

基本的な Markdown記述

基本的な Markdown 記法はそこそこサポートしてる模様。

参考
https://rmarkdown.rstudio.com/lesson-8.html

### Markdown記法

- リスト1
    - **AAA**
    - **BBB**
- リスト2
    - **CCC**
    - **DDD**

1. リスト1
    - *AAA*
    - *BBB*
1. リスト2
    - *CCC*
    - *DDD*

| col1 | col2 |
|:-----|:-----|
| val1 | val2 |
| val3 | val4 |

R以外の言語で書く場合

python など他の言語でコードチャンクを書くための仕組みが提供されている。

参考
https://rmarkdown.rstudio.com/lesson-5.html
https://bookdown.org/yihui/rmarkdown/language-engines.html

sample.Rmd

```{r include = F, message = F, warning = F}
library(knitr)
library(reticulate)
```

```{python}
x = [i for i in range(10)]
print(x)
```

使用する python のパスを明示したい場合は、呼び出し側で use_python で設定するか、
コードチャンク側で {python, engine.path = '/usr/bin/python3'} のように指定する事も可能。

例) 呼び出し側で指定する場合

library(rmarkdown)
library(knitr)
library(reticulate)
use_python("/usr/bin/python3")
render("sample1.Rmd")

例) コードチャンク側で指定する場合

```{python, engine.path = '/usr/bin/python3'}
x = [i for i in range(10)]
print(x)
```

サンプル

sample1.R

library(rmarkdown)
render("rmarkdown/sample1.Rmd")

sample1.Rmd

#TODO

header.html

<link rel="stylesheet" href="style.css">

doc_suffix.html

<script src="script.js"></script>

style.css

#TODO

script.js

#TODO

実行

source("sample1.R")

実行結果

TODO:

注意点

TODO:

トップ   一覧 単語検索 最終更新   ヘルプ   最終更新のRSS