如何在没有Rstudio的情况下使用bookdown写文档

2020-10-16  本文已影响0人  周运来就是我

bookdown包是一个开源的R包,可以方便地使用R Markdown编写书籍和长篇文章/报告。功能包括:从R文档生成可打印的图书和电子书。

在Rstudio(Server)中创建 bookdown项目是比较容易的,在安装了该包之后:

但是在没有Rstudio的时候要如何用呢?

在R中安装:

install.packages(bookdown)  # 安装核心R包

在Linux 中

git clone        https://github.com/rstudio/bookdown-demo   #  克隆模板一份

然后我们可以跑一个demo,看看还缺什么

cd  bookdown-demo 

sh  _build.sh 

一般的话可能会跑缺库或者包的错,常见的是缺少pandoc ,我们可以:

conda install -c conda-forge pandoc 

然后我们再:

sh  _build.sh 

出现


  |..............................................................        |  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"

基本上就编译成功了。

因为bookdown demo 默认是出html , pdf , epub格式的,而pdf和epub可能需要特殊的插件,如果遇到报错的话,可以在 _build.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')"

~                                                                                                                                                               
~                           
                                

书的模板是这样的:

这样就可以直接以书的形式出分析报告了,或者当你需要批量做可视化的时候,可以用Markdown。

https://bookdown.org/home/

上一篇 下一篇

猜你喜欢

热点阅读