---
title: "Making Books with R Markdown"
subtitle: "Meet bookdown"
author: "Alison Hill"
date: "2019/01/15 (updated: `r Sys.Date()`)"
output:
  xaringan::moon_reader:
    css: ["default", "css/penguin.css", "css/fonts.css"]
    lib_dir: libs
    nature:
      highlightStyle: solarized-light
      highlightLines: true
      countIncrementalSlides: false
      ratio: "16:9"
      beforeInit: "https://platform.twitter.com/widgets.js"
    seal: false 
    includes:
      in_header: header.html
---

```{r setup, include=FALSE}
options(htmltools.dir.version = FALSE)
library(knitr)
knitr::opts_chunk$set(warning = FALSE, message = FALSE)
# options(knitr.table.format = "html")
library(tidyverse)
library(babynames)
library(fontawesome) # from github: https://github.com/rstudio/fontawesome
library(DiagrammeR)
```

layout: true
  
<div class="my-footer"><span>arm.rbind.io/slides/bookdown</span></div>

<!-- this adds the link footer to all slides, depends on my-footer class in css-->

---
name: bookdown-title
background-image: url(img/karim-ghantous-333364-unsplash.jpg)
background-size: cover

# Meet bookdown

<img src="https://raw.githubusercontent.com/rstudio/hex-stickers/master/PNG/bookdown.png" alt="bookdown" width="180" />

### .fancy[Making Books in R Markdown]

.large[Alison Hill | rstudio::conf | `r Sys.Date()`]

<!-- this ends up being the title slide since seal = FALSE-->


---
exclude: true
name: lifecycle

individual files:

.Rmd to .md (via knitr)

.md to HTML (via pandoc)



HTML to lots of HTML --> BOOK (via bookdown)

---
background-image: url(img/art-lasovsky-559569-unsplash.jpg)
background-size: cover

# Hello `r emo::ji("book")`


+ Multiple R Markdown documents

+ Multiple output formats (E-books, PDF, HTML websites...)

+ Formatting features like cross-referencing, numbering of figures, equations, and tables.



---

.left-column[
# Hello `r emo::ji("book")`
]

.right-column[
```{r echo = FALSE}
knitr::include_url("https://bookdown.org/yihui/bookdown/", height = "500px")
```
https://bookdown.org/yihui/bookdown/
]


???

We'll focus on the `gitbook` output format today, which renders your book to an HTML website.

Here is an example of what one looks like- from the bookdown book!

-> Show TOC here in window

---

.left-column[
# [The simplest book](https://minimal-bookdown.netlify.com/)


]

.right-column[
```{r echo = FALSE}
knitr::include_url("https://minimal-bookdown.netlify.com/", height = "500px")
```
https://minimal-bookdown.netlify.com/

]

???

This book was built from a single file, the index.Rmd file

It has a title, and author, and a single (very short) chapter called "Hello World"

You can also see it has one and only one output format: gitbook


---

# [The simplest book](https://minimal-bookdown.netlify.com/)

```js
---
title: "A Book"
author: "Frida Gomam"
site: bookdown::bookdown_site 
documentclass: book
output:
  bookdown::gitbook: default
  #bookdown::pdf_book: default
---
# Hello World

Hi.

Bye.
```

.footnote[
https://github.com/yihui/bookdown-minimal
]

???

The index.Rmd is the only file required to build a book. 

The site parameter is the only required one here, it must be bookdown::bookdown_site.

The first and only chapter is called "Hello World"

But it is unlikely you want to write a book this simple...

You'll probably want a few chapters, and some nice extra features too.

---
class: inverse, middle, center

# `r emo::ji("book")`

# .fancy[Let's build a book right now]

--

.pull-left[
### .fancy[.center[Local with GitHub]]
### .fancy[.center[`r emo::ji("computer")` + <i class="fab fa-github fa-lg fa-fw"></i>]]
]

--

.pull-right[
### .fancy[.center[Cloud or Local (no GitHub)]]
### .fancy[.center[`r emo::ji("cloud")` | `r emo::ji("computer")`]]
]


---
name: start-here
class: inverse, middle

.pull-left[
### .fancy[.center[Local with GitHub]]
### .fancy[.center[`r emo::ji("computer")` + <i class="fab fa-github fa-lg fa-fw"></i>]]

1. Make a [repo on GitHub](https://happygitwithr.com/new-github-first.html#make-a-repo-on-github-1)

1. Make a [new RStudio project via git clone](https://happygitwithr.com/new-github-first.html#new-rstudio-project-via-git-clone) <br>
    *File > New Project > Version Control > Git*

1. Run:

```{r eval = FALSE}
bookdown:::bookdown_skeleton(getwd())
```
]

--

.pull-right[
### .fancy[.center[Cloud or Local]]
### .fancy[.center[`r emo::ji("cloud")` | `r emo::ji("computer")`]]

1. Make a new RStudio project via wizard <br>
    *File > New Project > New Directory*

1. Scroll down to select: <br>
    `r emo::ji("book")` *Book Project using bookdown*

]

--

<hr>
.center[Name it something like `<yourname>-arm-bookdown`]

---

# Project structure


```{r eval = FALSE}
directory/
├── .Rproj.user
├── book.Rproj
├── 01-intro.Rmd
├── 02-literature.Rmd
├── 03-method.Rmd
├── 04-application.Rmd
├── 05-summary.Rmd
├── 06-references.Rmd
├──  README.md
├── _bookdown.yml
├── _output.yml
├──  book.bib
├──  index.Rmd # YAML + Chapter 1 #<< 
├──  preamble.tex
└──  style.css
```

???

You should all be looking at this directory structure. The index.Rmd again is the only required file here, with some specific YAML parameters set within it.

We'll walk through these elements as we go, and what they all do.



---

# Project structure


```{r eval = FALSE}
directory/
├── .Rproj.user
├── book.Rproj
├── 01-intro.Rmd  #<<
├── 02-literature.Rmd #<<
├── 03-method.Rmd #<<
├── 04-application.Rmd #<<
├── 05-summary.Rmd #<<
├── 06-references.Rmd #<<
├──  README.md
├── _bookdown.yml
├── _output.yml
├──  book.bib
├──  index.Rmd #<< 
├──  preamble.tex
└──  style.css
```


???

each chapter = an .Rmd file


---
class: inverse, middle, center


# `r emo::ji("book")`

# .fancy[Let's render our book right now]

---
name: noknit
class: middle, inverse, center

# DO NOT KNIT!

![](https://media.giphy.com/media/Vg6vGMxVs9Sve/giphy.gif)

---

class: center, inverse, middle

# `r emo::ji("books")`

# .fancy[Preview book!]

Mouse up to "Addins" `r emo::ji("arrow_right")` "Preview Book"

```{r echo = FALSE, out.width = '70%'}
knitr::include_graphics("img/bookdown-preview.png")
```

???

What are we seeing here? Everything that is the _book folder. 

---

# Other options

## .fancy[Option 1:]
```{r eval = FALSE}
bookdown::render_book('index.Rmd')
```

--

## .fancy[Option 2:] 

```{r eval = FALSE}
bookdown::serve_book()
```

.blue["Preview Book"] Add-in calls `bookdown::serve_book()`

---
name: first-preview

.left-column[
# Hello `r emo::ji("book")`
]

.right-column[
```{r echo = FALSE}
knitr::include_url("https://skeleton-bookdown.netlify.com/", height = "500px")
```

https://skeleton-bookdown.netlify.com/

]

???

The index.Rmd contains the first "chapter" because it starts with a level one header. Notice that in your rendered book, the second chapter is called "Introduction"- but the filename was 01-intro.Rmd. Prerequisites, which is the first level-one heading in the index.Rmd became Chapter 1. And Introduction became Chapter 2.

I'll show you later how to change this if you want to.

---

# New files created


```{r eval = FALSE}
directory/
├── .Rproj.user
├── book.Rproj
├── 01-intro.Rmd
├── 02-literature.Rmd
├── 03-method.Rmd
├── 04-application.Rmd
├── 05-summary.Rmd
├── 06-references.Rmd
├──  README.md
├── _bookdown.yml
├── _output.yml
├──  book.bib
├──  index.Rmd  
├──  preamble.tex
├──  style.css
├──  _book #<<
├──  _bookdown_files #<<
└──  packages.bib  #<<
```


???

_book contains the generated book files- each .Rmd is saved as HTML files

_bookdown_files contains figure and cache files that were generated automatically from knitr

You could delete any of these and re-render your book to re-create them.


---

# `packages.bib`

This is a code chunk in `index.Rmd`

````
```{r include = FALSE}`r ''`
# automatically create a bib database for R packages
knitr::write_bib(c(
  .packages(), 'bookdown', 'knitr', 'rmarkdown'
), 'packages.bib')
```
````

---
exclude: true
name: earlydeploy


Netlify: drag and drop

Or RStudio Connect

in gitter chat copy/paste rendered link

---
name: clean
exclude: true

# Cleaning up


```{r eval = FALSE}
bookdown::clean_book()
```

```
These files/dirs can probably be removed: 

_book/

You can set options(bookdown.clean_book = TRUE) to allow 
this function to always clean up the book directory for you.
```



To do this:

```{r eval = FALSE}
bookdown::clean_book(TRUE)
```



.footnote[
this is the default setting (clean = TRUE) for `render_book`
]


---
name: section
class: middle, inverse
background-image: url(img/kelly-sikkema-450720-unsplash.jpg)
background-size: cover

---
name: outline
template: section

.right-column[
# <i class="fas fa-feather-alt"></i>.fancy[Outline]

+ Preface: [the `index.Rmd`](#index-YAML) <a href="https://bookdown.org/yihui/rmarkdown/bookdown-project.html#index-file"><i class="fas fa-book"></i></a>

+ Story: [the chapter `.Rmd` files](#chapters) <a href="https://bookdown.org/yihui/rmarkdown/bookdown-project.html#rmd-files"><i class="fas fa-book"></i></a>

+ Table of Contents: [the `_bookdown.yml`](#bookdown-YAML) <a href="https://bookdown.org/yihui/rmarkdown/bookdown-project.html#bookdown.yml"><i class="fas fa-book"></i></a>

+ Off to Press: [the `_bookdown.yml` and `_output.yml` files, & publishing](#press) <a href="https://bookdown.org/yihui/rmarkdown/bookdown-project.html#output.yml"><i class="fas fa-book"></i></a>
]


---
name: index-YAML
template: section

.right-column[

# <i class="fas fa-feather-alt"></i>.fancy[Preface: <br>the `index.Rmd` file]
<br>
<br>
<br>
### .fancy["made before" (prae + factum)] 
[-wikipedia](https://en.wikipedia.org/wiki/Preface)
]


---
name: edit-yaml

# `index.Rmd` = YAML


```js
--- 
title: "A Minimal Book Example"
author: "Yihui Xie"
date: "`r Sys.Date()`"
site: bookdown::bookdown_site
documentclass: book
bibliography: [book.bib, packages.bib]
biblio-style: apalike
link-citations: yes
description: "This is a minimal example..."
---
```

???

The `site` is **required**


---

# `index.Rmd` = YAML

.pull-left[
```js
--- 
title: "A Minimal Book Example" 
author: "Yihui Xie" 
date: "`r Sys.Date()`" 
site: bookdown::bookdown_site 
documentclass: book
bibliography: [book.bib, packages.bib]
biblio-style: apalike
link-citations: yes
description: "This is a minimal example..."
---
```
]

.pull-right[

```{r echo = FALSE}
knitr::include_graphics("img/index-YAML-annotated.png")
```

]

---
name: add-yaml

# The `default.html` template

.pull-left[

```html
<!--bookdown:title:start-->
$if(title)$
<div id="$idprefix$header">
<h1 class="title">$title$</h1>
$if(subtitle)$
<h3 class="subtitle"><em>$subtitle$</em></h3>
$endif$
$for(author)$
$if(author.name)$
<h4 class="author"><em>$author.name$</em></h4>
$if(author.affiliation)$
<address class="author_afil">
$author.affiliation$<br>$endif$
$if(author.email)$
<a class="author_email" href="mailto:#">$author.email$</a>
...
<!--bookdown:title:end-->
```
]

.pull-right[



```js
title: "A Minimal Book Example"
subtitle: "ARM workshop"
author: 
  name: Yihui Xie
  affiliation: RStudio
  email: xie@yihui.name
```


]

.footnote[
[default.html](https://github.com/rstudio/bookdown/blob/dda5f5a220058271a43ba8b81c6e6859ae3b9815/inst/templates/default.html)
]

---
name: yourturn
template: section

.right-column[
# <i class="fas fa-edit"></i> .fancy[Your turn]
]

---
name: yourturn1
template: yourturn

.right-column[
# .fancy[03:00 minutes]
### Edit your `index.Rmd` YAML, save, and re-render your book.

Ideas:

+ Change the [title and author](#edit-yaml)
+ Add a [subtitle, author affiliation, or author email](#add-yaml)<br>
    
.blue[.fancy[Hint:] Use [this link](https://github.com/rstudio/bookdown/blob/dda5f5a220058271a43ba8b81c6e6859ae3b9815/inst/templates/default.html) and find the `bookdown` title start/end (line 125).]

]


???

At end of time:

So those are the YAML parameters that change things you see

But most YAML parameters change things you never see

If you change the title, you may notice that the sidebar title has NOT changed! We'll show you why in a bit...

---

# Meta <i class="fas fa-tags"></i>



```html
<head>

<meta charset="utf-8">
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<meta name="generator" content="pandoc" />
<meta name="viewport" content="width=device-width, initial-scale=1">
<meta property="og:title" content="$pagetitle$" />
<meta property="og:type" content="book" />
$if(url)$<meta property="og:url" content="$url$" />$endif$
$if(cover-image)$<meta property="og:image" content="$url$$cover-image$" />$endif$
$if(description)$<meta property="og:description" content="$description$" />$endif$
$if(github-repo)$<meta name="github-repo" content="$github-repo$" />$endif$
```

.footnote[
[default.html](https://github.com/rstudio/bookdown/blob/dda5f5a220058271a43ba8b81c6e6859ae3b9815/inst/templates/default.html)
]

???

This is the default.html template for our book's metadata. Some are static, like the character set. But others, like the last 4 here, are actually pulling values from our index YAML. 

So you can see that if there is a url specified, it is saved as a meta tag; same with cover-image. In fact, the cover-image YAML parameter, combined with url, is needed for any kind of social sharing of your site. The description will also show up.


---
name: meta


# Viewing Meta <i class="fas fa-tags"></i>

```js
url: 'https\://alison-bookdown.netlify.com/' 
cover-image: images/books.jpg # relative to index
description: "This is a minimal example using the bookdown package for the rstudio::conf Advanced R Markdown Workshop."
github-repo: rstudio-education/arm-companion-rsc2019
```

---
template: meta
name: meta-twitter


```{r echo = FALSE, out.width = "70%", fig.align='center'}
knitr::include_graphics("img/bookdown-meta-twitter.png")
```


.footnote[
https://bookdown.org/yihui/bookdown/usage.html
]

???

This explains why you won't see any changes when you add some YAML parameters to index.Rmd

The changes are fed to Pandoc to generate meta-data for your book website, but cover-image for example does not actually add a cover-image to your repo (try it!).

Demo within browser --> inspect element --> search

I'll explain github-repo a bit later, but if you have one you may as well add it here.

---
template: meta
name: meta-community


```{r echo = FALSE, out.width = "40%", fig.align='center'}
knitr::include_graphics("img/bookdown-meta-community.png")
```


.footnote[
https://bookdown.org/yihui/bookdown/usage.html
]

---
name: pandoc-meta

# Viewing Meta <i class="fas fa-tags"></i>

```
<meta property="og:url" content="https://alison-bookdown.netlify.com/">
<meta property="og:image" content="https://alison-bookdown.netlify.com/images/books.jpg">
<meta name="twitter:card" content="summary">
<meta name="twitter:title" content="Notes from the Advanced R Markdown Workshop">
<meta name="twitter:description" content="This is a minimal example using the bookdown package for the rstudio::conf Advanced R Markdown Workshop.">
<meta name="twitter:image" content="https://alison-bookdown.netlify.com/images/books.jpg">
```

Test this out:

https://cards-dev.twitter.com/validator

Use this link:

https://alison-bookdown.netlify.com/






---
name: yourturn-2
template: yourturn

.right-column[
# .fancy[03:00 minutes]
<br>
### Add new YAML metadata tags to your `index.Rmd`, and re-render your book.


Ideas:

+ Add [url, cover-image, description, github-repo](#meta)

.blue[.fancy[Hint:] see all possible Pandoc EPUB metadata parameters [here](https://pandoc.org/MANUAL.html#epub-metadata).]

]


---
name: chapters
template: section

.right-column[

# <i class="fas fa-feather-alt"></i>.fancy[Story: <br>the chapter `.Rmd` files]
<br>
<br>
<br>
## .fancy[Examples:] 
1. `index.Rmd`, 
1. `01_*.Rmd`, 
1. `02_*.Rmd`,
1. `99_*.Rmd`

]

???

Each chapter is an R markdown file. 

None of the .Rmd files, other than index, have a YAML. 

Each file should begin with a level-one header, like # Chapter Title.

Each chapter has a dedicated page and is split into subchapters, indicated by lower-level headers, e.g., ## A subchapter within the chapter.

The numbered chapter names are a naming convention- you don't have to use them. Any .Rmd files with begin with an underscore are skipped.


---
name: headers
class: middle

## .center[.fancy[Numbered Chapters (default)]]

.pull-left[
`index.Rmd`
```{r eval = FALSE}
# Prerequisites

This is a _sample_ book written in **Markdown**. 
```
<hr>
`01-intro.Rmd`
```{r eval = FALSE}
# Introduction

Blah blah blah.
```
<hr>
`02-literature.Rmd`
```{r eval = FALSE}
# Literature

Here is a review of existing methods.
```

]

.pull-right[
### Chapter 1  Prerequisites

This is a _sample_ book written in **Markdown**. 
<hr>
### Chapter 2  Introduction

Blah blah blah.
<hr>
### Chapter 3  Literature

Here is a review of existing methods.
]


---
class: middle
name: headers-manip

## .center[.fancy[Un-numbered Chapters]]

.pull-left[
`index.Rmd`
```{r eval = FALSE}
# Prerequisites {-} #<<

This is a _sample_ book written in **Markdown**. 
```
<hr>
`01-intro.Rmd`
```{r eval = FALSE}
# Introduction 

Blah blah blah.
```
<hr>
`02-literature.Rmd`
```{r eval = FALSE}
# Literature

Here is a review of existing methods.
```

]

.pull-right[
### Prerequisites

This is a _sample_ book written in **Markdown**. 
<hr>
### Chapter 1  Introduction

Blah blah blah.
<hr>
### Chapter 2  Literature

Here is a review of existing methods.
]

???

You can un-number a chapter by following the header text with curly brackets, with .unnumbered, like I did for "Prerequisites" here.


---
class: middle
name: headers-cross

## .center[.fancy[Label & Cross-Reference Chapters/Subchapters]]

.pull-left[
`index.Rmd`
```{r eval = FALSE}
# Prerequisites {-#pre} 

This is a _sample_ book written in **Markdown**. 
```
<hr>
`01-intro.Rmd`
```{r eval = FALSE}
# Introduction {#intro}

Next we review the [literature](#lit)... #<<
```
<hr>
`02-literature.Rmd`
```{r eval = FALSE}
# Literature {#lit}

Following Chapter \@ref(intro),... #<<
```

]

.pull-right[
### Prerequisites

This is a _sample_ book written in **Markdown**. 
<hr>
### Chapter 1  Introduction

Next we review the [literature](https://skeleton-bookdown.netlify.com/literature.html)...
<hr>
### Chapter 2  Literature

Following Chapter [1](https://skeleton-bookdown.netlify.com/intro.html),...
]

???


bookdown supports a nice way for explicitly setting the header ID label. If you follow the header text with an opening curly bracket (separated from the text with a least one space), a hash, the ID label and a closing curly bracket, the ID label is set on the header. 

Why would you want to do that? You would want to set ID labels so you can more easily:

a) link to specific chapters/subchapters
b) cross-reference within the book to chapters/subchapters

---
name: figure-caption

## .center[.fancy[Caption a figure]]


.pull-left[

````
```{r pressure-plot, fig.cap='Look!'}`r ''`
par(mar = c(4, 4, .1, .1))
plot(pressure, type = 'b', pch = 19)
```
````

]

.pull-right[
.center[

```{r pressure-plot, out.width='75%', echo = FALSE, fig.retina = 3}
par(mar = c(4, 4, .1, .1))
plot(pressure, type = 'b', pch = 19)
```

**Fig. 1:** Look!]

]

---
name: figure-cross

## .center[.fancy[Cross-reference a figure]]


.pull-left[

See Figure `\@ref(fig:pressure-plot)`.

````
```{r pressure-plot}`r ''`
par(mar = c(4, 4, .1, .1))
plot(pressure, type = 'b', pch = 19)
```
````

]

.pull-right[

See Figure [1](https://skeleton-bookdown.netlify.com/intro.html).

.center[

```{r ref.label = 'pressure-plot', out.width='75%', echo = FALSE, fig.retina = 3}
```

]

]


---
name: table-caption

## .center[.fancy[Caption a table]]


.pull-left[

````
```{r iris-tab}`r ''`
knitr::kable(
  head(iris[,1:4], 3), caption = 'Look!',
  booktabs = TRUE
)
```
````

]

.pull-right[
.center[
**Table 1:** Look!
```{r iris-tab, out.width='50%', echo = FALSE}
knitr::kable(
  head(iris[,1:4], 3),
  booktabs = TRUE,
  format = "html"
)
```
]

]

---
name: figs-tabs-cross

## .center[.fancy[Cross-reference Figures & Tables]]

.pull-left[

````
```{r pressure-plot, fig.cap='Look!'}`r ''`
par(mar = c(4, 4, .1, .1))
plot(pressure, type = 'b', pch = 19)
```
````

````
```{r iris-tab}`r ''`
knitr::kable(
  head(iris[,1:4], 3), caption = 'Look!',
  booktabs = TRUE
)
```
````


See Figure `\@ref(fig:pressure-plot)`.

Don't miss Table `\@ref(tab:iris-tab)`.




]

.pull-right[
See Figure [1](#figure-caption).

See Table [1](#table-caption).

]


---

# More

<i class="fas fa-external-link-alt"></i> [Equations](https://bookdown.org/yihui/bookdown/markdown-extensions-by-bookdown.html#equations)

<i class="fas fa-external-link-alt"></i> [Citations](https://bookdown.org/yihui/bookdown/citations.html)

<i class="fas fa-external-link-alt"></i> [Theorems and proofs](https://bookdown.org/yihui/bookdown/markdown-extensions-by-bookdown.html#theorems)

.footnote[
[All components](https://bookdown.org/yihui/bookdown/components.html)
]

---
name: yourturn3
template: yourturn

.right-column[
# .fancy[10:00 minutes]
<br>
### Add/edit chapters in your book. Try using some of these elements:

+ [Markdown headers](#headers) like `#, ##, ###` .fancy[(to un-number: `{-}`, to label: `{#label}`)]
+ [Cross-references](#headers-cross) to chapters/sub-sections like `\@ref(id)`
+ [Figures](#figure-caption) and [tables](#table-caption) with captions
+ [Figures](#figure-cross) and [tables](#table-cross) cross-references like `\@ref(fig:my-plot)` & `\@ref(tab:my-tab)`

]


---
name: toc
template: section

.right-column[

# <i class="fas fa-feather-alt"></i>.fancy[Table of Contents]

<br>
<br>
<br>
Examples: 

<i class="fas fa-link"></i> https://skeleton-bookdown.netlify.com/

<i class="fas fa-link"></i> https://alison-bookdown.netlify.com/
]

???


---
name: toc-default

# Chapters/Subchapters Only

.pull-left[
`index.Rmd`
```{r eval = FALSE}
# Prerequisites

This is a _sample_ book written in **Markdown**. 
```
`01-intro.Rmd`
```{r eval = FALSE}
# Introduction

Blah blah blah.
```

`02-literature.Rmd`
```{r eval = FALSE}
# Literature

Here is a review of existing methods.
```

]

.pull-right[
.center[
```{r echo = FALSE, out.width='65%'}
knitr::include_graphics("img/bookdown-skeleton-toc.png")
```
]
]


---
name: toc-num-parts

# Parts

.pull-left[
`index.Rmd`
```{r eval = FALSE}
# Prerequisites {-}
```
`01-intro.Rmd`
```{r eval = FALSE}
# (PART) Act one {-}  #<<

# Introduction
```

`02-literature.Rmd`
```{r eval = FALSE}
# Literature
```

`03-methods.Rmd`
```{r eval = FALSE}
# (PART) Act two {-}  #<<

# Methods
```

]

.pull-right[
.center[
```{r echo = FALSE, out.width='70%'}
knitr::include_graphics("img/bookdown-skeleton-toc_numparts.png")
```
]
]

???

Parts are just groups of chapters and do not have dedicated pages, but according to the gitbook::theme, it will show in the navigation.

You add them with a first-level header followed by PART in all caps within parentheses. You 


---
name: toc-parts

# Un-numbered Parts

.pull-left[
`index.Rmd`
```{r eval = FALSE}
# Prerequisites {-}
```
`01-intro.Rmd`
```{r eval = FALSE}
# (PART\*) Act one {-} #<<

# Introduction
```

`02-literature.Rmd`
```{r eval = FALSE}
# Literature
```

`03-methods.Rmd`
```{r eval = FALSE}
# (PART\*) Act two {-}  #<<

# Methods
```

]

.pull-right[
.center[
```{r echo = FALSE, out.width='70%'}
knitr::include_graphics("img/bookdown-skeleton-toc_parts.png")
```
]
]

???

Parts already need to have the dash inside the curly braces, which was how we previously removed the numbering from individual chapters like Prerequisites.

To create unnumbered parts, you need to add a forward slash and asterisk after PART, within the parentheses.

---
name: toc-appendix

# Appendix

.pull-left[
`index.Rmd`
```{r eval = FALSE}
# Prerequisites {-}
```
`01-intro.Rmd`
```{r eval = FALSE}
# (PART\*) Act one {-}

# Introduction
```


...

`05-summary.Rmd`
```{r eval = FALSE}
# (APPENDIX) Other stuff {-} #<<

# Final Words
```

]

.pull-right[
.center[
```{r echo = FALSE, out.width='65%'}
knitr::include_graphics("img/bookdown-skeleton-toc_appendix.png")
```
]
]

??? 

We can also add an appendix, which by default is an unnumbered part.

The numbering style of appendices will be automatically changed in LaTeX/PDF and HTML output (usually in the form A, A.1, A.2, B, B.1, …).

---
name: toc-collapse

# TOC Sidebar

New file: `_output.yml`

.pull-left[

```js
bookdown::gitbook:
  config:
    toc:
      collapse: none 
      scroll_highlight: yes
      before: |
        <li><a href="./">A Minimal Book Example</a></li>
      after: |
        <li><a href="https://github.com/rstudio/bookdown" target="blank">Published with bookdown</a></li>
```
]

.pull-right[
`collapse`:

+ `section` (`#`, ~~`##`, `###`~~)

+ `subsection` (`#`, `##`, `###`)

+ `none` (`#`, `##`, `###`, ad infinitum)

]

---
name: yourturn4
template: yourturn

.right-column[
# .fancy[05:00 minutes]
<br>
### Change how your sidebar TOC looks & works. Try using some of these elements:

+ [Parts](#toc-num-parts) with  `# (PART) My unique part {-}`
+ [Un-numbered parts](#toc-parts) with  `# (PART\*) My unique part {-}`
+ [Appendix](#toc-appendix) with  `# (APPENDIX) My appendix {-}`
+ [Collapsing TOC](#toc-collapse) with the `_output.yml`

]
  
---
exclude: true

# Add R Code

```{r eval = FALSE}
rmarkdown::find_external_resources(here::here("index.Rmd"))
rmarkdown::yaml_front_matter(here::here("index.Rmd"))
rmarkdown::metadata$title
rmarkdown::resolve_output_format("index.Rmd", output_format = "bookdown::gitbook")$pandoc
```



---
name: press
template: section

.right-column[

# <i class="fas fa-scroll"></i>.fancy[Off to press:]

## .fancy[the `_bookdown.yml` and `_output.yml` files]
## .fancy[& publishing]

]


---
name: render

# `_bookdown.yml`

This YAML file gets fed to the `bookdown::render_book` function:

```{r gitbook-args, code=formatR::usage(bookdown::render_book, output=FALSE, fail='none'), eval=FALSE, R.options=list(width=50)}
```


--

Arguments worth mentioning:

```{r eval = FALSE}
new_session: yes 
output_dir: "docs" # for gh-pages
rmd_files: ["index.Rmd", "02-literature.Rmd", "01-intro.Rmd"]
```

--

.footnote[
Read more:
+ https://bookdown.org/yihui/rmarkdown/bookdown-project.html#bookdown.yml
+ https://bookdown.org/yihui/bookdown/configuration.html
+ https://blog.github.com/2016-08-17-simpler-github-pages-publishing/
]

???

This file has no dash delimiters.

the default config file for the `bookdown::render_book()` function is `_bookdown.yml`

`new_session`: render each chapter in its own R session?

`output_dir`: default = "_book"; "docs" if you want to publish to GH Pages
]

---
name: output

# `_output.yml`

For each output format, different configurations are possible, for example:

```{r gitbook-args, code=formatR::usage(bookdown::gitbook, output=FALSE, fail='none'), eval=FALSE, R.options=list(width=50)}
```

--

Most arguments are passed to [`rmarkdown::html_document()`](https://github.com/rstudio/rmarkdown/blob/master/R/html_document.R):

```js
bookdown::gitbook:
  css: [css/arm.css]
  highlight: kate
  config: ???
```

--

.footnote[
+ [css: use Hao Zhu's `bookdownThemeEditor`!](https://github.com/hebrewseniorlife/bookdownThemeEditor)
+ [highlight](https://bookdown.org/yihui/bookdown/yaml-options.html): `r knitr::combine_words(rmarkdown:::highlighters(), before = '\x60')`
]

---
name: output-config



.pull-left[
### .center[.fancy[Gitbook configurations]]
```{r eval = FALSE}
gitbook_config = function(config = list()) {
  default = list(
    sharing = list(
      github = FALSE, facebook = TRUE, twitter = TRUE, google = FALSE,
      linkedin = FALSE, weibo = FALSE, instapaper = FALSE, vk = FALSE,
      all = c('facebook', 'google', 'twitter', 'linkedin', 'weibo', 'instapaper')
    ),
    fontsettings = list(theme = 'white', family = 'sans', size = 2),
    edit = list(link = NULL, text = NULL),
    history = list(link = NULL, text = NULL),
    download = NULL,
    # toolbar = list(position = 'static'),
    toc = list(collapse = 'subsection')
)
```
]

.pull-right[
### .center[.fancy[Sample `_output.yml`]]
```js
bookdown::gitbook:
  css: [css/arm.css]
  highlight: kate
  config:
    toc: <!--covered earlier!-->
    download: ["rmd"]
    edit:
      link: https://github.com/USERNAME/REPO/edit/BRANCH/%s
      text: "Suggest an edit"
    sharing:
      github: yes
      facebook: no
      twitter: yes
```

Very important!
1. Edit link to read: <br>
    https://github.com/apreshill/armed/edit/master/%s
1. For GitHub sharing, add to `index.Rmd` YAML: <br>
    `github-repo: apreshill/armed`
]


.footnote[
https://github.com/rstudio/bookdown/blob/master/R/gitbook.R
]

---
exclude: true
name: publish

Options:

1. RStudio Connect `r emo::ji("cloud")` + `r emo::ji("computer")`
1. Netlify `r emo::ji("computer")`
1. GitHub Pages

---
name: yourturn5
template: yourturn

.right-column[
# .fancy[05:00 minutes]
<br>
### Tune your `gitbook` output by editing these 2 YAML files:

+ [`_bookdown.yml`](#render)
+ [`_output.yml`](#output) (.fancy[don't forget about [`config`!](#output-config)])
  + Try adding the GitHub edit button, link to a repo, change the highlight

]



---
exclude: true
name: publish
class: inverse, middle, center

```{r echo = FALSE}
knitr::include_graphics("https://media.giphy.com/media/atcqQ5PuX41J6/giphy.gif")
```


# .fancy[Let's publish a book right now]

Options:

1. RStudio Connect `r emo::ji("cloud")` + `r emo::ji("computer")`
1. GitHub + Netlify `r emo::ji("computer")`


---
exclude: true
name: rsconnect

# RStudio Connect `r emo::ji("cloud")` + `r emo::ji("computer")`


+ `account =`
+ `server =` (defaults to `beta.rstudioconnect.com`)

```{r publish-args, code=formatR::usage(bookdown::publish_book, output=FALSE, fail='none'), eval=FALSE, R.options=list(width=50)}
```

---
exclude: true
name: netlify

OPEN WITH DRAG AND DROP

in gitter chat copy/paste rendered link

# GitHub + Netlify `r emo::ji("computer")`

1. Preview your book one more time.
1. Add/commit/push your edits to your remote repository.
1. In your repo on GitHub `_book` folder
1. Go to [Netlify.com](https://www.netlify.com/)
  + Login
  + Select "New Site from Git"
  + Select "GitHub"
  + Find & select your repository with your bookdown project
  + Leave "Build" empty
  + Set "Publish Directory" to `_book` (unless you set the `output_dir` in your [`_bookdown.yml`](#render) to something else!)