introduce
The R BookDown extension pack is an extension to the Markdown format, following the Knitr and RMarkDown extensions, to enable the Rmd format to support autonumbering and citations of formulas, theorems, charts, links, literature citations, and links suitable for writing books.
Under BookDown: The contents of a book can be split into multiple Rmd files, which can have executable R code, and the text results, tables, and graphics generated by R code can be automatically inserted into the generated content, and the tables and graphics can be floating typeset. The output format mainly supports gitbook format of web books, this book in the left side of the display catalog, the right side of the display content, and can automatically link to the previous chapter and the next chapter; See R Language Tutorial – Chapter 23 for a detailed introduction.
The LaTeX compiler, which is installed separately, supports converting books into a PDF file in Chinese. Can generate ePub and other formats of e-books. In addition to writing books with multiple chapters, BookDown can also be used to produce single-file research reports. Of course, xiaobians also use it for something interesting: hyperlinks.
For books, use RStudio, which builds the ability to compile entire books with one click.
Simple tutorial
Here are some simple steps to get you started writing books with Bookdown. For comprehensive documentation on BookDown, see bookdown.org/yihui/bookd…
The installation
Either method can be:
# you can either use the CRAN version
install.packages('bookdown')
# or the development version on Github
remotes::install_github('rstudio/bookdown')
Copy the code
create
Click file-new project-New Directory, then click the following icon:
Set the file name and path, and click Create
In the lower right corner are the files that come with them. Inside the box are the RMD files for each chapter. You can see that BookDown is a book composed of many RMD files. Of course, it also includes YML files, BIB format for putting literature, CSS files for making themes, etc.
compile
We find the index. RMD file, open it, and click knit to compile. The compiled result is in the bottom right corner:
You can also preview it on the web by clicking show in New Windows on the bottom right of the box to the right of the broom.
Note: The Knit button only presents the chapter you are editing (e.g. Index. RMD, 01-intro.rmd, etc.) and uses the book’s default output format.
To build all chapters of the book and all formats, use the Build Book button in the RStudio Build pane (upper right corner of the entire interface).
Well, you’ve already made books in different formats! You can find the corresponding files in the corresponding project folder.
This template does not support rendering PDF with Chinese fonts, but supports HTML with Chinese fonts.
Making Chinese books
If you need to make books with Chinese characters (of course!) We can directly apply the template that Teacher Li Dongfeng prepared for us.
The download link is bookdown-template-v0-5.zip, or you can obtain it by running [bookdown] in the official ACCOUNT command window.
Folder contains
The CBook subdirectory contains the required Chinese book template, the CArticle subdirectory contains the paper format template, and the other subdirectories have some other templates (all can be learned to use, thanks to Mr. Li! Have a chance to make up a tutorial!
There is also a MathJax directory for the display of mathematical formulas in native support web pages, see the readme.txt description file there.
Using the tutorial
Create a New project (file-new Project) and copy and paste all files inside the CBook folder into the project folder. For example:
Here are some of the files:
- index.rmd
A BookDown book usually requires an index.Rmd file, which is the original file for the home page of the resulting web site. You can write some descriptions of the book in this file, and set the title, author, date, and so on in the YAML metadata section at the beginning. Similar to the YAML file in Rmarkdown, you can make changes here, and Mr. Li also gives a detailed explanation of the internal parameters of the template. This article is for beginners, so I don’t want to explain too much, just use it first.
- xxx.Rmd
In addition to index.rmd, each of the projects. Rmd files are all in one chapter. The first line of each.rmd file should have a level 1 header beginning with a hash sign and a space, followed by a space and then a tag within braces beginning with a hash sign, such as
# random number {# RNG}Copy the code
0101-use.rmd in the template gives the basic usage of the Chinese book Bookdown template, including: installation setup, document structure, automatic numbering of graphical table formula, generation of PDF, upload to the website and so on.
This RMD file is a lot of dry goods, we do not skip!
1001-chapter01. RMD provides an internal chapter for reference. You can change the content to your own, you can create more rmD-like files, and it’s better to have names that are ordinal like, because by default you compile by ordinal. For example, my diary is set to:
After you’ve written your content, you can follow the previous basic tutorial and compile it (in two ways). Or you can use what Teacher Li recommended: In the command window, use the following command to compile:
bookdown::render_book("index.Rmd",
output_format="bookdown::gitbook", encoding="UTF-8")
Copy the code
To export PDF, you must have a Tex environment. Xie Yihui’s TinyTeX is recommended.
Then in the project folder to find the corresponding PDF file, open is your own good books! Here is a screenshot of my diary:
Xiaobian has something to say
-
If this is the first time you’ve seen my work on document communication and want to follow this tutorial to build a book, I suggest you take a look at my RMarkdown basics tutorial, because the build book contains all kinds of RMarkdown syntax, which can be a headache if you don’t know how to use it.
-
Xiaobian can only take you into a door (how to create, use, compile). While you may encounter various practical problems in the actual process of using, I suggest you use Bing (Google) English search to find the answer you want.
-
Here are some more Bookdown related references
- R Language Tutorial – Chapter 23
- bookdown: Authoring Books and Technical Documents with R Markdown
- Bookdown website