1 Using \(\LaTeX\)

2 Labeling Equations with bookdown

Equation labels must start with the prefix eq: in bookdown. All labels in bookdown must only contain alphanumeric characters, :, -, and/or /. Equation references work best for LaTeX/PDF output, and they are not well supported in Word output or e-books. For HTML output, bookdown can only number the equations with labels. Please make sure equations without labels are not numbered by either using the equation* environment or adding \nonumber or \notag to your equations. The same rules apply to other math environments, such as eqnarray, gather, align, and so on (e.g., you can use the align* environment)(Xie 2016).

To create Equation (2.1), use the code shown in the gray box below.

\[\begin{equation} f(x)=(x+a)(x+b) \tag{2.1} \end{equation}\]
\begin{equation} 
f(x)=(x+a)(x+b)
(\#eq:labelA)
\end{equation}

To refer to Equation (2.1), use \@ref(eq:labelA).

To create the aligned Equation (2.2), use the code shown in the gray box below.

\[\begin{align} f(x) &= x^4 + 7x^3 + 2x^2 \nonumber \\ &\qquad {} + 10x + 12 \tag{2.2} \end{align}\]
\begin{align}
f(x) &= x^4 + 7x^3 + 2x^2 \nonumber \\
     &\qquad {} + 10x + 12
(\#eq:aligned)      
\end{align}

To write the piece-wise function in Equation (2.3), use the code shown in the gray box below.

\[\begin{equation} u(x) = \begin{cases} \exp{(x)} & \text{if } x \geq 0 \\ 1 & \text{if } x < 0 \end{cases} \tag{2.3} \end{equation}\]
\begin{equation}
u(x) = 
\begin{cases} 
\exp{(x)} & \text{if } x \geq 0 \\
1         & \text{if } x < 0
\end{cases}
(\#eq:piece)
\end{equation}

The \overset{} and \underset{} commands typeset symbols above and below expressions. To create Equation (2.4), use the code shown in the gray box below.

\[\begin{equation} \lim_{x\to 0}{\frac{e^x-1}{2x}} \overset{\left[\frac{0}{0}\right]}{\underset{\mathrm{H}}{=}} \lim_{x\to 0}{\frac{e^x}{2}}={\frac{1}{2}} \tag{2.4} \end{equation}\]
\begin{equation}
\lim_{x\to 0}{\frac{e^x-1}{2x}}
\overset{\left[\frac{0}{0}\right]}{\underset{\mathrm{H}}{=}}
\lim_{x\to 0}{\frac{e^x}{2}}={\frac{1}{2}}
(\#eq:overunder)
\end{equation}

3 Labeling Graphs with bookdown

Figure 3.1 was created using the R code chunk and code below. Note that to refer to Figure 3.1, the name of the code chunk label is used. In this case, the code chunk label is HISTO and to refer to Figure 3.1 one uses the syntax \@ref(fig:HISTO).

```{r, label = "HISTO", fig.cap = "Write your descriptive caption here", echo = FALSE}
stuff <- rnorm(10000, 100, 15)
DF <- data.frame(x = stuff)
library(ggplot2)
ggplot(data = DF, aes(x = x)) + 
  geom_histogram(fill = "pink", color = "black", binwidth = 5) + 
  theme_bw()
```
Write your descriptive caption here

Figure 3.1: Write your descriptive caption here

4 Labeling Tables with bookdown

Table 4.1 was created using the R code chunk and code below. Note that to refer to Table 4.1, the name of the code chunk label is used. In this case, the code chunk label is FT and to refer to Table 4.1 one uses the syntax \@ref(tab:FT).

```{r, label = "FT", echo = FALSE}
knitr::kable(head(iris), booktabs = TRUE,  caption = 'The first six rows of `iris`')
```
Table 4.1: The first six rows of iris
Sepal.Length Sepal.Width Petal.Length Petal.Width Species
5.1 3.5 1.4 0.2 setosa
4.9 3.0 1.4 0.2 setosa
4.7 3.2 1.3 0.2 setosa
4.6 3.1 1.5 0.2 setosa
5.0 3.6 1.4 0.2 setosa
5.4 3.9 1.7 0.4 setosa

5 Automagically CreatingR Package References

```{r, echo = FALSE, results = "hide"}
PackagesUsed <- c("ggplot2", "bookdown")
# Write bib information
knitr::write_bib(PackagesUsed, file = "./packages.bib")
# Load packages
lapply(PackagesUsed, library, character.only = TRUE)
```

The above R code creates a file named packages.bib to cite the ggplot2 package used to create Figure 3.1. Figure 3.1 was created with ggplot2 by Wickham and Chang (2016). To cite a package in the *.bib file, use the syntax @name_of_bib_citation. This document specifies the output as bookdown::html_document2. The function bookdown::html_document2 is from bookdown written by Xie (2016).

6 Use Inline R!

Although one can compute statistics and hard code the values in a report, it is much better to use inline R code to report all answers. The mean, standard deviation, and IQR of the ranodmly generated values in DF are computed and stored in the tibble NDF. Each time this document is compiled, different values will be stored in DF and hence the values in NDF will likely be different.

library(dplyr)
NDF <- DF %>%
  summarize(Mean = mean(x), SD = sd(x), iqr = IQR(x))
NDF  
      Mean       SD      iqr
1 99.84112 14.89223 19.88716

To report the mean of the variable x in NDF using two decimal places, use the inline R code

`r round(NDF$Mean, 2)`

which returns the value 99.84.

7 Use the %>% (pipe) Operator

library(MASS)
quine %>%
  group_by(Sex, Lrn) %>%
  filter(Days > 2, Days < 10) %>%
  summarize(mean(Days), median(Days), sd(Days), IQR(Days), n())
Source: local data frame [4 x 7]
Groups: Sex [?]

     Sex    Lrn `mean(Days)` `median(Days)` `sd(Days)` `IQR(Days)` `n()`
  <fctr> <fctr>        <dbl>          <dbl>      <dbl>       <dbl> <int>
1      F     AL     5.153846              5  1.6756170           1    13
2      F     SL     5.750000              5  1.6931233           2    16
3      M     AL     5.900000              6  1.7288403           2    10
4      M     SL     5.571429              6  0.5345225           1     7
quine %>%
  filter(Days > 2, Days < 10) %>%
  ggplot(mapping = aes(x = Sex, y = Days)) +
  geom_boxplot() +
  theme_bw() + 
  facet_grid(Lrn ~ .)

References

Wickham, Hadley, and Winston Chang. 2016. Ggplot2: Create Elegant Data Visualisations Using the Grammar of Graphics. https://CRAN.R-project.org/package=ggplot2.

Xie, Yihui. 2016. Bookdown: Authoring Books and Technical Documents with R Markdown. https://github.com/rstudio/bookdown.