Ubuntu14.04とRで、knitrで日本語のPDFを出力するための設定方法をお伝えする。

Rでの作業は実際にはRStudioを用いている。

それでは、Ubuntuの設定からお伝えする。

Ubuntuの設定

Ubuntuに日本語のTex環境をインストールする。


$ sudo apt-get install texlive texlive-lang-cjk xdvik-ja

次に、R markdownファイルをPDFに出力するための実行エンジンとなるxetexをインストールする。


$ sudo apt-get install texlive-xetex

Texの組版の元となるBXjsclsを設定する。
BXjsclsは「https://github.com/zr-tex8r/BXjscls」からダウンロードしておく。


# zipの解凍
$ unzip BXjscls-master

# ディレクトリ名の変更
$ mv BXjscls-master BXjscls

# ディレクトリとその配下に対して、所有者とグループの変更
$ sudo chown -R root:root BXjscls

# ディレクトリの移動
$ sudo mv BXjscls /usr/share/texmf/tex/latex

最後に、BXjsclsをきちんと認識させるために texlive-doc-ja パッケージをインストールする。


sudo apt-get install texlive-doc-ja

 

もし、texlive-doc-ja パッケージがすでにインストールされていた場合は、一度アンインストール「sudo apt-get remove texlive-doc-ja」した後に、再度texlive-doc-jaをインストールする。

そうしないと、RStudio上で(ドキュメントクラスbxjsarticleを用いた)PDF出力の際に、次のようなエラーが発生する。


! Package keyval Error: xelatex undefined.

See the keyval package documentation for explanation.
Type  H   for immediate help.
 ...                                              

l.232 \ProcessOptions\relax

pandoc: Error producing PDF from TeX source
 エラー: pandoc document conversion failed with error 43
 実行が停止されました

RStudioの設定

RStudioを立ち上げた後、次のコマンドで knitr パッケージをインストールする。
すでにインストールしてあれば、次のコマンドを実行する必要はない。
また、PDF出力の際に他に必要なパッケージをインストールするよう催促された場合は、
そのパッケージもインストールする必要がある。


> install.packages("knitr")

グラフの日本語対応

ここまでで、knitr を用いたPDF出力の準備が整ったが、
グラフ中に日本語を用いた時に警告が表示されるという問題がある。

次のR markdownを実行してみると、


---
title: "サンプル"
author: "山田 太郎"
date: "2015年11月1日"
output: 
    pdf_document:
        latex_engine: xelatex
        number_sections: true
        toc: true
documentclass: bxjsarticle
classoption: xelatex,ja=standard
geometry: no
mainfont: Takao Pゴシック
monofont: Takao Pゴシック
---

# 最初

これはサンプルです。

## 次

```{r}
plot(x = iris$Sepal.Length, y = iris$Sepal.Width, xlab = 'がく片の長さ', ylab = 'がく片の幅')
```

次のような大量の警告がPDF内に入ってしまい、また、グラフの日本語も「.(ドット)」に置き換えられてしまう。


## Warning in title(...): 'mbcsToSbcs' 中の ' がく片の⻑さ ' で変換に失敗 : 
## をドットで置き換えました

how-to-set-up-for-outputting-a-pdf-of-the-japanese-at-knitr-in-ubuntu-1404-and-r -1

この問題の対応は
Windows 環境下での PDF 生成で日本語文字を問題なく扱える R Markdown 設定
が参考になる。

次のようにR markdownを書き換えると、正しくPDF出力される。


---
title: "サンプル"
author: "山田 太郎"
date: "2015年11月1日"
output: 
    pdf_document:
        latex_engine: xelatex
        number_sections: true
        toc: true
documentclass: bxjsarticle
classoption: xelatex,ja=standard
geometry: no
mainfont: Takao Pゴシック
monofont: Takao Pゴシック
---

```{r, include=FALSE}
library(knitr)

# 出力フォーマットが TeX(PDF含む)の場合のみ対処する
if (knitr::opts_knit$get("rmarkdown.pandoc.to") %in% c("beamer", "latex")) {

  # conversion failure on '...' in 'mbcsToSbcs' の Warning 発生の workaround
  options(device = function(file, width = 7, height = 7, ...) {
    cairo_pdf(tempfile(), width = width, height = height, ...)
  })
  
  ## 1. cairo_pdf を使う方法
  # * family には OS にインストールされているフォント名を指定する。
  knitr::opts_chunk$set(dev="cairo_pdf", dev.args=list(family="Takao Pゴシック"))
}
```

# 最初

これはサンプルです。

## 次

```{r}
plot(x = iris$Sepal.Length, y = iris$Sepal.Width, xlab = 'がく片の長さ', ylab = 'がく片の幅')
```

how-to-set-up-for-outputting-a-pdf-of-the-japanese-at-knitr-in-ubuntu-1404-and-r -2

Ubuntu,R knitrで日本語のPDFを出力するための設定

Ubuntu,R knitrで日本語のPDFを出力するための設定」への1件のフィードバック

コメントは受け付けていません。