Bookdown平台分享了哪些书籍,如何使用Bookdown分享书籍

欢迎关注生信宝典公众号 https://mp.weixin.qq.com/s/i71OMaUu6QtcY0pt1njHQA

bookdown是著名R包作者谢益辉开发的,支持采用Rmarkdown (R代码可以运行)或普通markdown编写文档,然后编译成HTML, WORD, PDF, Epub等格式。样式清新,使用简单,值得拥有。

在Bookdown的官网,有很多免费的用bookdown写的R书籍,如Hadley Wickham等撰写的《R for Data Science》,Roger D. Peng撰写的《R Programming for Data Science》, 陈总的《液体活检口袋书》,益辉的《R语言忍者秘笈》,《单细胞数据整体分析流程》https://hemberg-lab.github.io/scRNA.seq.course/index.html (初学单细胞分析可以完全照着这个,在学习过程中改进)。

还有很多基于Bookdown的教程,一时也想不起来,欢迎大家补充。我们前面转录组R培训的教案也是用bookdown写作的,后续再调整下格式,出一批电子书和纸质书,有意向和需求的欢迎联系。

下面分2步讲述,自己如何构建一个Bookdown书籍,第一部分是通过bookdown示例了解其基本功能和使用,第二部分是个人在使用过程中碰到的问题和解决方式。

基本使用

安装必须软件

RstudioPandoc二选一, bookdown必须安装。

  • Install Rstudio (version>1.0.0) (安装和使用见Rstudio)

  • Install Pandoc (version>1.17.0.2)或者参照here。如果系统新,可以直接使用系统自带的yumapt-get;如果没有权限或系统比较老,Pandoc的安装可以使用conda,具体配置见Conda配置,配置好运行conda install -c conda-forge pandoc即可安装。

  • In R install.packages("bookdown")

Demo示例

克隆或下载https://github.com/rstudio/bookdown-demo示例文件,编译成功后,依葫芦画葫芦修改.

编译成书

运行下载的示例中的bash _build.sh_book目录下就是成书.

The content of _build.sh is:

#!/bin/sh
Rscript -e "bookdown::render_book('index.Rmd', 'bookdown::gitbook')"
# 生成pdf需要安装好latex,如果不需要可以注释掉
Rscript -e "bookdown::render_book('index.Rmd', 'bookdown::pdf_book')"

在前面的内容运转起来后,再看后面的内容。

Customize our bookdown

准备Rmd文件
基本规则
  • 一个典型的bookdown文档包含多个章节,每个章节在一个R Markdown文件里面 (文件的语法可以是pandoc支持的markdown语法,但后缀必须为Rmd)。
  • 每一个章节都必须以# Chapter title开头。后面可以跟一段概括性语句,概述本章的内容,方便理解,同时也防止二级标题出现在这一页。默认系统会按照文件名的顺序合并Rmd文件。
  • 另外章节的顺序也可在_bookdown.yml文件中通过rmd_files:["file1.Rmd", "file2.Rmd", ..]指定。
  • 如果有index.Rmdindex.Rmd总是出现在第一个位置。通常index.Rmd里面也需要有一章节,如果不需要对这一章节编号的话,可以写作# Preface {-}, 关键是{-}
  • 在第一个出现的Rmd文件中 (通常是index.Rmd),可以定义Pandoc相关的YAML metadata, 比如标题、作者、日期等 (去掉#及其后的内容)。

    ~~~~

    title: "My book"
    author: #可以写多行信息,都会被当做Author处理
    - "CT"
    - "CY"
    - "chentong_biology@163.com"
    date: "`r Sys.Date()`"
    documentclass: article #可以为book或article
    
    # 如果需要引用参考文献,则添加下面三行内容
    
    bibliography: [database.bib]  #指定存储参考文献的bib文件,endote或zotero都可以导出这种引文格式
    biblio-style: apalike  #设定参考文献显示类型
    link-citations: yes
    knitr::opts_chunk$set(echo = FALSE, fig.align="center", out.width="95%", fig.pos='H')
    knitr::opts_chunk$set(cache = FALSE, autodep=TRUE)
    set.seed(0304)

    ~~~~~~

插入并引用图片(外部图片)

插入图片最好使用knitr::include_graphics,可以同时适配HTML和PDF输出。另外当目录下同时存在name1.pngname1.pdf文件时,会自动选择在HTML展示name1.png文件,在PDF输出中引入name1.pdf格式的文件。

图的标签为fig-name(不能有下划线),在引用时需使用如下格式\@ref(fig:fig-name),且fig.cap也要设置内容。

多张图可以同时展示,图的名字以vector形式传给include_graphics,需要设置out.width=1/number-picsfig.show="hold"

~~~~
Insert a single pic and refer as Figure \@ref(fig:fig-name). echo=FALSE will hide the code block and display the output of r command only. These options can be set globally as indicated below.

 
 
 
 
 
插入并引用表格(外部表格)

外部表格的名字中必须包含tab:, 然后是表格的实际名字,格式为(\#tab:table-name); 引用时使用Table \@ref(tab:table-name)。 表格名字中不能有下划线。

Check Table \@ref(tab:seq-sum) for detail.

Table: (\#tab:seq-sum) Summary of sequencing reads 测序量总结 (对于双端测序,  *\_1* 表示左端reads, *\_2* 表示右端reads) 

----------------------------------------------------------------------
Sample     Total reads     Total bases Sequence length (nt)     GC content (%) Encoding               
-------- ------------- --------------- ---------------------- ---------------- -----------------------
T8_1        37,106,941   5,566,036,721 138-150                              47 Sanger / Illumina 1.9  

T8_2        37,106,941   5,566,034,285 138-150                              47 Sanger / Illumina 1.9  
----------------------------------------------------------------------
插入并引用表格(内部表格)

插入表格推荐使用knitr::kable,只要提供数据矩阵,用r读取就可以了。

~~~~~~~~~~~~~~~~~~~~~~~~

Check Table \@ref(tab:table-id) for detail.

a <- as.data.frame(matrix(rnorm(20), nrow=4))
knitr::kable(a, caption="Test table",  booktabs=TRUE)

~~~~~~~~~~~~~~~~~~~~~~~~~

插入脚注

text^[footnote] is used to get the footnote.

where `type` may be `article`,  `book`,  `manual`,  and so on.^[The type name is case-insensitive,  so it does not matter if it is `manual`,  `Manual`,  or `MANUAL`.]
插入引文

假如我们的bib文件中内容如下,如果我们要引用这个文章,只要写 [@chen_m6a_2015]就可以了。


@article{chen_m6a_2015,
    title = {m6A {RNA} {Methylation} {Is} {Regulated} by {MicroRNAs} and {Promotes} {Reprogramming} to {Pluripotency}},
    volume = {16},
    issn = {1934-5909, 1875-9777},
    url = {http://www.cell.com/cell-stem-cell/abstract/S1934-5909(15)00017-X},
    doi = {10.1016/j.stem.2015.01.016},
    language = {English},
    number = {3},
    urldate = {2016-12-08},
    journal = {Cell Stem Cell},
    author = {Chen, Tong and Hao, Ya-Juan and Zhang, Ying and Li, Miao-Miao and Wang, Meng and Han, Weifang and Wu, Yongsheng and Lv, Ying and Hao, Jie and Wang, Libin and Li, Ang and Yang, Ying and Jin, Kang-Xuan and Zhao, Xu and Li, Yuhuan and Ping, Xiao-Li and Lai, Wei-Yi and Wu, Li-Gang and Jiang, Guibin and Wang, Hai-Lin and Sang, Lisi and Wang, Xiu-Jie and Yang, Yun-Gui and Zhou, Qi},
    month = mar,
    year = {2015},
    pmid = {25683224},
    pages = {289--301},
}
准备YML配置文件
_bookdown.yml

配置输入和输出文件参数。

book_filename: "输出文件的名字" 
output_dir: "输出目录的名字,默认_book"
language:
  ui:
      chapter_name: "" 
_output.yml

配置产生输出文件的命令行参数。

bookdown::pdf_book:
  template: ehbio.tex #使用自己定制的pandoc latex模板
  includes: # or only customize part latex module
    in_header: preamble.tex
    before_body: latex/before_body.tex
    after_body: latex/after_body.tex
  latex_engine: xelatex
  citation_package: natbib
  keep_tex: yes
  pandoc_args: --chapters
  toc_depth: 3
  toc_unnumbered: no
  toc_appendix: yes
  quote_footer: ["\\VA{", "}{}"]
bookdown::epub_book:
  stylesheet: css/style.css
bookdown::gitbook:
  css: style.css
  split_by: section
  config:
    toc:
      collapse: none
      before: | #设置toc开头和结尾的链接
          <li><a href="http://www.ehbio.com"><img src="ehbio_logo.png" width="100%"></a></li>
      after: |
          <li><a href="mailto:ct@ehbio.com" target="blank">ct@ehbio.com</a></li>
    download: [pdf, epub, mobi]
    edit: https://github.com/rstudio/bookdown/edit/master/inst/examples/%s
    sharing:
      twitter: no
      github: no
      facebook: no
其它定制
  • 不同的文件分别用于htmlpdf输出

    
    # in _bookdown.yml 
    
    rmd_files:
    html: ["index.Rmd", "file2.Rmd"]
    latex: ["index_pdf.Rmd", "file3.Rmd"]
    
    
    # Different render way
    
    
    #!/bin/sh
    
    Rscript -e "bookdown::render_book('index.Rmd', 'bookdown::gitbook')"
    Rscript -e "bookdown::render_book('index_pdf.Rmd', 'bookdown::pdf_book')"
  • 配置全局变量自适应HTMLPDF输出

    ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

    library(knitr)
    output <- opts_knit$get("rmarkdown.pandoc.to")
    html = FALSE
    latex = FALSE
    opts_chunk$set(echo = FALSE, fig.align="center", fig.show="hold")
    if (output=="html") {
    html = TRUE
    }
    if (output=="latex") {
    opts_chunk$set(out.width="95%", out.height='0.7\\textheight', out.extra='keepaspectratio', fig.pos='H')
    latex = TRUE
    }
    
    #knitr::opts_chunk$set(cache = FALSE,  autodep=TRUE)
    
    set.seed(0304)

    Below text will only appear in HTML output.

    
    
    # EHBIO Gene Technology {-}
    
    

    Below command will only be executed and displayed in HTML output.

预览生成的WEB文件

如果没有安装Rstudio,可以在生成的book目录(有index.html的目录)下运行python -m SimpleHTTPServer 11521 (11521为端口号,一般选较大值避免冲突), 然后就可以在浏览器输入网址http://server-ip:11521来访问了。

References

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

生信宝典

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值