别人的电子书,你的电子书,都在bookdown
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 (初学单细胞分析可以完全照着这个,在学习过程中改进,我们也做了部分翻译Hemberg-lab单细胞转录组数据分析(一))。
还有很多基于Bookdown的教程,一时也想不起来,欢迎大家补充。我们前面转录组和R培训的教案也是用bookdown写作的,后续再调整下格式,出一批电子书和纸质书,有意向和需求的欢迎联系。
下面分2步讲述,自己如何构建一个Bookdown书籍,第一部分是通过bookdown示例了解其基本功能和使用,第二部分是个人在使用过程中碰到的问题和解决方式。
基本使用
安装必须软件
Rstudio
或Pandoc
二选一, bookdown
必须安装。
Install
Rstudio (version>1.0.0)
(安装和使用见Rstudio)Install
Pandoc (version>1.17.0.2)
或者参照here。如果系统新,可以直接使用系统自带的yum
或apt-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.Rmd
,index.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 ``````{r setup, include=FALSE} 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.png
和name1.pdf
文件时,会自动选择在HTML展示name1.png
文件,在PDF
输出中引入name1.pdf
格式的文件。
图的标签为fig-name
(不能有下划线),在引用时需使用如下格式\@ref(fig:fig-name)
,且fig.cap
也要设置内容。
多张图可以同时展示,图的名字以vector形式传给include_graphics
,需要设置out.width=1/number-pics
和 fig.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.```{r fig-name, fig.cap="Markdown supported string as caption", fig.align="center", echo=FALSE}
knitr::include_graphics("images/1.png")
```Suppose we have 3 pictures in `images` folder with names as `Fig1_a`, `Fig1_b`, `Fig1_c`, we can refer to them using Figure \@ref(fig:fig1).```{r fig1, fig.cap="3 sub-plots.", fig.align="center", out.width=33%, fig.show="hold"}
fig1 = list.files("images", pattern="Fig1_.*", full.names=T)
knitr::include_graphics(fig1)
```Another way of including two pics.```{r fig-name2, out.width="49%", fig.show="hold", fig.cap="Markdown supported string as caption", fig.align="center", echo=FALSE}
knitr::include_graphics(c("images/1.png", "images/2.png"))
```
~~~~~~~~~~~~~~~~
如果图或表的标题中有Markdown语法,输出为HTML时是可以正确解析的,但是输出为PDF时却不可以。这时可以使用Text Reference
。当图或表的标题太长时,也可以使用Text Reference
引用一段话作为图和表的标题。
Here is normal text.(ref:pic-label) This line can be referred in **fig.cap** and markdown syntax is supported for both `HTML` and `PDF` output.```{r pic-label, fig.cap="(ref:pic-label)"}
knitr::include_graphics("images/1.png")
```
~
输出PDF时不支持使用在线图片,可以加一个判断。
```{r fig-name, fig.cap="Markdown supported string as caption", fig.align="center", echo=FALSE}
if (!file.exists(cover_file <- 'cover.jpg')){download.file(url, cover_file, mode = 'wb')
}
knitr::include_graphics(if (identical(knitr:::pandoc_to(), 'html')) url else cover_file)
```
插入并引用表格(外部表格)
外部表格的名字中必须包含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.```{r table-id, include=FALSE}
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 modulein_header: preamble.texbefore_body: latex/before_body.texafter_body: latex/after_body.texlatex_engine: xelatexcitation_package: natbibkeep_tex: yespandoc_args: --chapterstoc_depth: 3toc_unnumbered: notoc_appendix: yesquote_footer: ["\\VA{", "}{}"]
bookdown::epub_book:stylesheet: css/style.css
bookdown::gitbook:css: style.csssplit_by: pconfig:toc:collapse: nonebefore: | #设置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/%ssharing:twitter: nogithub: nofacebook: no
其它定制
不同的文件分别用于
html
和pdf
输出# 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')"
配置全局变量自适应
HTML
和PDF
输出```{r setup, include=FALSE} 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.```{asis, echo=html}# EHBIO Gene Technology {-}```Below command will only be executed and displayed in HTML output.```{r cover, eval=html, out.width="99%"} knitr::include_graphics("ehbio/cover.png") ``` ~~~~~~~~~~~~~~~~~~~~
保留生成的markdown文件
# add below lines to last Rmd file ```{r, include=FALSE} file.rename(from="bookdown_file_name.md", to="bookdown_file_name.saved.md") ``` ~~~~~~~~~~~
包含子文件 (subfile.txt)
```{r child="subfile.txt"} ``` ~~~~~~
cahce external file ref
```{r mtime-func} mtime <- function(files){lapply(Sys.glob(files), function(x) file.info(x)$mtime) } ``````{r mtime-usage, cache=T, cache.extra=mtime(c("file1", "file2", file3))} file3 <- paste0(dir, '/', name) data1 <- read.table("file1") data2 <- read.table("file2") ``` ~~~
预览生成的WEB文件
如果没有安装Rstudio,可以在生成的book目录(有index.html
的目录)下运行python -m SimpleHTTPServer 11521
(11521为端口号,一般选较大值避免冲突), 然后就可以在浏览器输入网址http://server-ip:11521
来访问了。
References
https://bookdown.org/yihui/bookdown/get-started.html
https://github.com/rstudio/bookdown/tree/master/inst/examples
http://stackoverflow.com/questions/25236850/how-to-set-different-global-options-in-knitr-and-rstudio-for-word-and-html
Multiple output with different configs https://github.com/yihui/knitr/issues/1145
Multiple output with different configs https://github.com/yihui/knitr/issues/114://github.com/rstudio/rmarkdown/issues/614
Citation style http://rmarkdown.rstudio.com/authoring_bibliographies_and_citations.html
Save markdown http://stackoverflow.com/questions/19989325/knit-rmd-file-to-md-and-save-the-md-file-one-level-up-with-a-different-name
PDF online pic http://www.pzhao.org/zh/post/bookdown-tips/
往期精品(点击图片直达文字对应教程)
后台回复“生信宝典福利第一波”或点击阅读原文获取教程合集
别人的电子书,你的电子书,都在bookdown相关推荐
- python开源电子书_Python 开源电子书资源
转载自公众号:Mocun6 昨天给大伙儿送了书,留言区的篇幅占了整篇文章的一半,看来大家都想好(把)好(我)学(掏)习(空).今天不送纸质书了,分享几个我学 Python 一路以来收藏的几个开源电子书 ...
- 8月推荐给程序员们的电子书 | 附半价电子书福利
半价电子书福利自发布时间起24小时内有效. 8月即将结束,这个月,圈子里都在关注哪些电子书?让我们来一探究竟. 电子书榜单 1.软技能:代码之外的生存指南 [美]John Z. Sonmez(约翰 Z ...
- 别人对程序员的印象都是加班?
网页BS兴起的时候,由于互联网本身需要建立完整的架构,需要做很多重复性工作.简单的技术就可以直接兑换成市场资产,所以好多企业就要求程序员用简单的重复性的技术获取更多资产,因为简单的技术就可以赚钱.程序 ...
- 杨承润:在你没获取别人信任之前,说的话都是废话
作为一个演说大师,你只能把自己的产品销售给那些相信你的人. 所以大家一定要记住,在你没有获取大家信任之前,你说的话都是废话.在这里和大家分享三个营销的非常重要的技巧. 同频共振 大家一定要记住,任何人 ...
- 很多程序员都在抱怨加班多,觉得该做的都做了,别人没做的,自己都做了。为什么?为什么别人能拿到几万的工资,自己只能拿到零头呢?
这里写自定义目录标题 欢迎使用Markdown编辑器 新的改变 功能快捷键 合理的创建标题,有助于目录的生成 如何改变文本的样式 插入链接与图片 如何插入一段漂亮的代码片 生成一个适合你的列表 创建一 ...
- 基于微信小程序在线电子书阅读系统 电子书小程序毕业设计 毕业论文 开题报告和效果图参考
- 安卓电子书格式_Kindle的一个劲敌,小米多看电子书入手体验
文章来源:含情脉脉之林老师 前言 大家好,我是林老师,我们又见面了. 对于电子书来说,各位朋友相信第一时间就会想到是Kindle,不过这几年"泡面神器"的确走入了千家万户,成为首选 ...
- 【Javaee毕业设计】基于JAVA_JSP电子书下载系统
目 录 第1章 绪论 1 课题的研究背景.内容和意义 1 第2章 主要技术概述 3 2.1 B/S结构 3 2.2 JSP技术 4 2.2.1 JSP技术的强势 5 2.2.2 JSP技术的弱势 5 ...
- 国外电子书免费下载网站
1.FreeBookSpot FreeBookSpot是一个免费英文电子书大全网站,它提供有4485本免费电子书,分为96个分类,高达71.97GB.你可以通过分类搜索这些免费电子书,比如科学,工业, ...
最新文章
- Hibernate的数据查找,添加!
- 最长公共子序列LCS[C++题解]
- spring中的设计模式_面试官:来给我说一下 Spring 中使用了哪些设计模式?
- 【转】Create Hello-JNI with Android Studio
- 个人作业——软件产品分析
- apache hadoop_通过Apache Hadoop大规模扩展Apache Solr实时实时索引
- yii2 关系...
- 彻底卸载2345系列
- python调用photoshop_Python和Photoshop
- 李开复写给女孩子们的
- 学习日记day16 ps
- TPLINK免驱版网卡插上后无法识别到CD驱动器怎么办?
- 通过java解码web前端直接预览海康威视摄像头
- 在移动硬盘里移动视频文件到移动硬盘 另外一个文件夹 显示正在计算_移动硬盘也支持AES-256位硬件加密,希捷锦系列入手体验...
- steam同乐无法连接远程计算机,Steam远程同乐功能怎么用 Steam远程同乐功能使用教程...
- 《孩子,为你自己读书》(Yanlz+Unity+SteamVR+云技术+5G+AI+VR云游戏+多多图书馆+志存当高远+读万卷书如行万里路+术业有专攻+读书是对思想的一种升华+立钻哥哥++==)
- SpringBoot+Mybatis+Elasticsearch 实现模糊分页查询并标记关键字
- 【STM32F407】第8章 ThreadX NetXDUO之TCP服务器
- 数学补习---∑(sigma)符号
- mysql limit锁_我所理解的MySQL五:锁及加锁规则
热门文章
- 第一届大数据科学与工程国际会议(2016)征文通知
- 作者:周涛,电子科技大学大数据研究中心主任、教授、博士生导师。
- 【Java】撩开Java线程的“神秘面纱”
- 管理软件实施(4)——如何编写售前解决方案
- 马尔可夫网络,(马尔可夫随机场、无向图模型)(Markov Random Field)
- Exchange Online基于网络位置限制使用
- 微服务架构设计基础之领域驱动设计
- 怎样找回由于IO设备错误移动硬盘的文件
- 【BZOJ】1725: [Usaco2006 Nov]Corn Fields牧场的安排
- 《SQL与关系数据库理论——如何编写健壮的SQL代码》一第2章