R bookdown cheatsheet

By Peng Zhao | March 14, 2017

Quick Start

What is R bookdown

  • A software for writing books or documents.
  • More elegant than MS Word. Much easier than \(\LaTeX\).
  • Users can easily insert table of contents, figures and tables with cross-reference, footnotes, and index.
  • Users can easily embed equations, citations, R scripts.
  • pdf, word and html files can be exported.
  • best choice to write reproducible documents.

Installation

  1. Download R and install it.
  2. Download RStudio and install it.
  3. Download LaTeX and install it.
  4. Download Pandoc and install it.
  5. Run RStudio. Type the following codes in the top-left panel:
install.packages("bookdown")
install.packages('servr')

Templates

  1. Download a template (Open the following webpage and click Clone or download - Download ZIP):
  1. Unzip the template to a folder.

  2. Find an .Rproj file. Double click it and open it with RSudio.

  3. Click the Build label in the top-right panel, and click build book. Done. Find your demo book in _book/ folder.

  4. In the bottom-right panel you can see some files. Open those .Rmd file, and modify them into your own book. You can remove unnecessary .Rmd files except index.Rmd.

  5. Repeat Step 4, and you will get your own book.

  6. Compare your .Rmd files with the files in _book/, and you will understand how they are connected.

Basic syntax

marks output
*Italic* Italic
**bold** bold
CO~2~ subscript
R^2^ superscript
$E = mc^2$ \(E = mc^2\) inline equation ($$ for displayed)
`[hyperlink](http://bookdown.org )` hyperlink
<dapengde@live.com> email
![](hyperlink of figure) insert a figure
> quote quote
`code` code
# Chapter One chapter title
1. First,... numbered list
- First unnumbered list
^[footnote] footnote

Chapters

# (PART) Part I {-} 
# (APPENDIX) Appendix {-} 
# References {-}
# chapter {#ID}
## section {#ID}
# chapter {#ID .unnumbered}

\@ref(ID)

Figures and tables

A figure can be inserted with R plotting codes:

```{r, fig.cap='caption', out.width='80%', fig.align='center', echo=FALSE}
plot(1:10)
```

\@ref(fig:fig1)

or with R inserting codes:

```{r img1, fig.cap='caption', out.width='80%', fig.align='center', echo=FALSE}
knitr::include_graphics("images/img1.png")
```

\@ref(fig:img1)

or with markdown basic syntax:

![caption](images/img1.png)

A table can be inserted with basic markdown syntax:

col one      col two
----------- ----------
row 1.1     row 1.2
row 2.1     row 2.2

and you will get:

col one col two
row 1.1 row 1.2
row 2.1 row 2.2

or with R codes:

```{r tab1, tidy=FALSE, echo=FALSE}
knitr::kable(
  head(iris, 20), caption = 'Here is a nice table!',
  booktabs = TRUE
)
```

\@ref(tab:tab1)

Bibliography

Bibliography entries must be saved in .bib.

Citation: [@R-bookdown]

Bibliography: # References {-}

Created a library of R packages for bibliography:

knitr::write_bib(c("knitr", "stringr"), "", width = 60)

Theorems, lemma, definitions, etc.

Full name: theorems lemma   definition  corollary   proposition example
Abbreviations:thm         lem   def         cor         prp         ex
```{Full name, label='', name=""}

```

\@ref(Abbreviation:label)

Export Word document

Insert the following line into _output.yml:

bookdown::word_document2: default 

Equations numbering

(@eq-mc) $E = mc^2$

I like Eq. (@eq-mc) so much that I am falling love with her.
  1. \(E = mc^2\)

I like Eq. (1) so much that I am falling love with her.

\begin{equation} 
E = mc^2
  (\#eq:mc2)
\end{equation} 

I like Eq. \@ref(eq:mc2) so much that I am falling love with her.

Further

See bookdown manual for more details.

comments powered by Disqus