RStudio에는 기본적인 R보다 유용한 기능이 많습니다. 그중에서는 저는 knitr 패키지를 이용하여 R 코드가 들어있는 R Markdown으로 쓰여진 글을 HTML이나 pdf 파일로 만들어 주는 기능에 관심이 많았습니다. 물론 프리뷰(Preview) 버젼인 RStudio Version 0.99.1273에는 이것 보다 더 진보한R Markdown Notebooks이 들어 있습니다.
그러나 문제는 이 기능이 영어로만 되어 있는 것은 잘 되는데, 유난히 한글만 들어가면 잘 안 된다는 것입니다. HTML로 변환하는 것은 잘 되는 것 같았는데, pdf으로 변환하는 것이 문제였습니다. 그러다가 우연히 권재명님이 쓰신 Jaimyoung/data-science-in-korean: 한국어를 포함한 데이터와 분석코드를 다루는 팁을 살펴보다가 https://raw.githubusercontent.com/Jaimyoung/data-science-in-korean/master/test-template.Rmd이라는 링크를 발견했습니다. 이 파일을 살펴보니 다음과 같이 되어 있었습니다.
title: “한글 레퍼런스 R 마크다운 템플릿”
author: “권재명”
date: “July 18, 2015”
output:
word_document:
highlight: tango
reference_docx: korean-template.docx
pdf_document:
latex_engine: xelatex
mainfont: NanumGothic
그래서 이것을 참고로 RStudio 기본 파일을 다음과 같이 수정해서 아래와 같이 수정하니, 한글이 들어 있어도 pdf 파일로 잘 변환됩니다. 아래 내용은 기본 파일에서 앞부분만 고치고 한글이 들어 있는 내용을 한 줄 넣은 것입니다.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
title: "R Notebook" | |
output: | |
pdf_document: | |
latex_engine: xelatex | |
html_notebook: default | |
html_document: default | |
mainfont: NanumGothic | |
— | |
이것은 [R Markdown](http://rmarkdown.rstudio.com) 노트북입니다. | |
This is an [R Markdown](http://rmarkdown.rstudio.com) Notebook. When you execute code within the notebook, the results appear beneath the code. | |
Try executing this chunk by clicking the *Run* button within the chunk or by placing your cursor inside it and pressing *Cmd+Shift+Enter*. | |
“`{r} | |
plot(cars) | |
“` | |
Add a new chunk by clicking the *Insert Chunk* button on the toolbar or by pressing *Cmd+Option+I*. | |
When you save the notebook, an HTML file containing the code and output will be saved alongside it (click the *Preview* button or press *Cmd+Shift+K* to preview the HTML file). |
RStudio에서 새 파일 에서 R Markdown은 선택하신 후 이미 만들어진 내용을 다 지우신 다음, 위의 코드를 붙여넣기를 하신 다음, knit라는 버튼을 눌러서, HTML과 pdf 파일로 변환하시면, 한글이 잘 나오는 것을 확인하실 수 있습니다. 참고로 pdf를 만드시려면 Latex를 설치하셔야 됩니다.