bookdown包是一個(gè)開(kāi)源的R包,可以方便地使用R Markdown編寫(xiě)書(shū)籍和長(zhǎng)篇文章/報(bào)告。功能包括:從R文檔生成可打印的圖書(shū)和電子書(shū)厉膀。
在Rstudio(Server)中創(chuàng)建 bookdown項(xiàng)目是比較容易的兰吟,在安裝了該包之后:
但是在沒(méi)有Rstudio的時(shí)候要如何用呢?
在R中安裝:
install.packages(bookdown) # 安裝核心R包
在Linux 中
git clone https://github.com/rstudio/bookdown-demo # 克隆模板一份
然后我們可以跑一個(gè)demo仙逻,看看還缺什么
cd bookdown-demo
sh _build.sh
一般的話可能會(huì)跑缺庫(kù)或者包的錯(cuò)驰吓,常見(jiàn)的是缺少pandoc ,我們可以:
conda install -c conda-forge pandoc
然后我們?cè)伲?/p>
sh _build.sh
出現(xiàn)
|.............................................................. | 89%
label: nice-tab (with options)
List of 1
$ tidy: logi FALSE
|......................................................................| 100%
inline R code fragments
output file: bookdown-demo.knit.md
/miniconda3/bin/pandoc +RTS -K512m -RTS bookdown-demo.utf8.md --to html4 --from markdown+autolink_bare_uris+tex_math_single_backslash --output bookdown-demo.html --lua-filter /R/x86_64-pc-linux-gnu-library/4.0/bookdown/rmarkdown/lua/custom-environment.lua --lua-filter /R/x86_64-pc-linux-gnu-library/4.0/rmarkdown/rmarkdown/lua/pagebreak.lua --lua-filter /R/x86_64-pc-linux-gnu-library/4.0/rmarkdown/rmarkdown/lua/latex-div.lua --metadata-file /tmp/Rtmp3qoBse/file232f563231d5 --email-obfuscation none --wrap preserve --standalone --section-divs --table-of-contents --toc-depth 3 --template /R/x86_64-pc-linux-gnu-library/4.0/bookdown/templates/gitbook.html --highlight-style pygments --number-sections --css style.css --include-in-header /tmp/Rtmp3qoBse/rmarkdown-str232f61a30c2a.html --mathjax --filter /miniconda3/bin/pandoc-citeproc
Output created: _book/index.html
[1] "/_book/index.html"
基本上就編譯成功了。
因?yàn)閎ookdown demo 默認(rèn)是出html 系奉, pdf 檬贰, epub格式的,而pdf和epub可能需要特殊的插件缺亮,如果遇到報(bào)錯(cuò)的話翁涤,可以在 _build.sh
文件中設(shè)置先不出這種格式。
#!/bin/sh
set -ev
Rscript -e "bookdown::render_book('index.Rmd', 'bookdown::gitbook')"
# Rscript -e "bookdown::render_book('index.Rmd', 'bookdown::pdf_book')"
# Rscript -e "bookdown::render_book('index.Rmd', 'bookdown::epub_book')"
~
~
書(shū)的模板是這樣的: