-
Notifications
You must be signed in to change notification settings - Fork 3
/
01-intro.Rmd
103 lines (76 loc) · 3.71 KB
/
01-intro.Rmd
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
```{r child = 'pre-chapter-script.Rmd'}
```
# Basics {#basics}
## Labels and references
You can label chapter and section titles using `{#label}` after them, e.g., we can reference Chapter \@ref(literature). If you do not manually label them, there will be automatic labels anyway, e.g., Chapter \@ref(theorems).
Figures and tables with captions will be placed in `figure` and `table` environments, respectively.
```{r nice-fig, fig.cap='Here is a nice figure!', out.width='80%', fig.asp=.75, fig.align='center'}
par(mar = c(4, 4, .1, .1))
plot(pressure, type = 'b', pch = 19)
```
Reference a figure by its code chunk label with the `fig:` prefix, e.g., see Figure \@ref(fig:nice-fig). Similarly, you can reference tables generated from `knitr::kable()`, e.g., see Table \@ref(tab:nice-tab).
\index{iris}
```{r nice-tab}
knitr::kable(
head(faithful, 5), caption = 'Here is a nice table!',
booktabs = TRUE
)
```
## Fitting things on the page
### Side by side figures
You can put figures side by side using `out.width=c('50%','50%'), fig.show='hold'` options to r blocks.
```{r palettes, out.width=c('50%','50%'), fig.show='hold'}
library(RColorBrewer)
display.brewer.pal(n = 8, name = "Blues")
display.brewer.pal(n = 8, "Reds")
```
### Wide R output
What happens with R output and margins? By default, R output is 80 columns wide.
That's too wide for most LaTeX page styles. The `pre-chapter-script.R` file
contains the line `options(width = 72)` which sets the R output so these big
lines will fit.
One long vector:
```{r wide}
discoveries
```
One wide matrix:
```{r wider}
matrix(runif(100), ncol = 20)
```
Here's a warning.
```{r clonk, warning=TRUE}
a <- 1:10
a[5] <- 100:200
a
```
Here's an error that's too long. The width option doesn't fix it.
Instead, there's an output hook (see [R Markdown Cookbook](https://bookdown.org/yihui/rmarkdown-cookbook/output-hooks.html))
which is in the `pre-chapter-script.R` file.
```{r bonk, error=TRUE}
sample(x = 1:10, size = 30)
```
### Tidy source code
Here we deal with source code reformatting for prettier style.
This book has a global setting in `pre-chapter-script.R` that
runs all source code through the `styler` package.
Or you can do it on a chunk-by-chunk basis using the chunk options
`tidy='styler'` or `tidy=TRUE` to use `formatR`.
Enforcing line lengths needs to be done manually (for now, not supported by
either `styler` or `formatR`)
```{r tossing}
X <- replicate(1000, { coin_toss <-sample(c("H","T"),3, replace= TRUE)
sum(coin_toss == "H")})
mean(X >= 1)
```
## Footnotes and indexing
After spending all day getting henna designs drawn, she noticed she had a foot note.[^henna]
Footnotes in html books are numbered from 1 and never reset, so they just keep going up until the end of the book. Footnotes in pdf books restart counting from 1 in each chapter. So that's one thing that will be different between the pdf and html versions of a bookdown book.
[^henna]: This joke (c) 2020 by Bryan Clair.
Index in LaTeX with `\index{Daft Punk}\index{music!Daft Punk}`:
This book features music by Daft Punk\index{Daft Punk}\index{music!Daft Punk} and poetry by Carl Sandburg\index{Sandburg, Carl}.
We have a second index for data sets, and defined two LaTeX commands `\indexpackage` and `\indexdata`.
For example, you might want to use the data sets `diamonds`\indexdata{diamonds}{ggplot2} or `mpg`\indexdata{mpg}{ggplot2}
from the `ggplot2`\indexpackage{ggplot2} package. Or maybe you prefer `DrinksWages`\indexdata{DrinksWages}{HistData}
and `Quarrels`\indexdata{DrinksWages}{HistData} from `HistData`\indexpackage{HistData}. Or, you can
be a `MASS`\indexpackage{MASS}-ochist and use `anorexia`\indexdata{anorexia}{MASS}, `GAGurine`\indexdata{GAGurine}{MASS}
and `deaths`\indexdata{deaths}{MASS}.