forked from moderndive/ModernDive_book
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.Rmd
executable file
·305 lines (260 loc) · 10.3 KB
/
index.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
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
---
title: "`r ifelse(knitr::is_latex_output(), 'Statistical Inference via Data Science: A ModernDive into R and the Tidyverse', 'Statistical Inference via Data Science')`"
subtitle: "A ModernDive into R and the Tidyverse"
author: "`r ifelse(knitr::is_latex_output(), 'Chester Ismay and Albert Y. Kim', 'Chester Ismay and Albert Y. Kim <br> Foreword by Kelly S. McConville')`"
date: "`r format(Sys.time(), '%B %d, %Y')`"
site: bookdown::bookdown_site
documentclass: krantz
bibliography: [bib/books.bib, bib/packages.bib, bib/articles.bib]
biblio-style: apalike
fontsize: '12pt, krantz2'
monofont: "Inconsolata" #"Source Code Pro"
monofontoptions: "Scale=0.85"
link-citations: yes
colorlinks: yes
lot: false
lof: false
always_allow_html: yes
github-repo: moderndive/ModernDive_book
twitter-handle: ModernDive
graphics: yes
description: "An open-source and fully-reproducible electronic textbook for teaching statistical inference using tidyverse data science tools."
cover-image: "images/logos/book_cover.png"
url: 'https\://moderndive.com/'
apple-touch-icon: "images/logos/favicons/apple-touch-icon.png"
favicon: "images/logos/favicons/favicon.ico"
---
```{r set-options, include=FALSE, purl=FALSE}
# Current version information: Date here should match the date in the YAML above.
# Remove .9000 tag and set date to release date when releasing
version <- "1.1.0"
date <- "July 2, 2020"
# Latest release information:
latest_release_version <- "1.1.0"
latest_release_date <- "July 2, 2020"
# knitr pkg is needed throughout book
if (!"knitr" %in% installed.packages()) {
install.packages("knitr", repos = "http://cran.rstudio.com")
}
library(knitr)
# Set output options
if (is_html_output()) {
options(width = 80)
}
if (is_latex_output()) {
options(width = 75)
}
options(digits = 7, bookdown.clean_book = TRUE, knitr.kable.NA = "NA")
opts_chunk$set(
tidy = FALSE,
out.width = "\\textwidth",
fig.align = "center",
comment = NA
)
# New dplyr warning message when running group_by() %>% summarize() that is not
# addressed in v1 (print edition).
# See https://github.com/moderndive/ModernDive_book/issues/353
# v2 TODO: Remove this option and fix group_by() section in Ch3
options(dplyr.summarise.inform = FALSE)
# Install CRAN packages needed
needed_CRAN_pkgs <- c(
# Packages used by book reader (except skimr, see below):
"dygraphs", "fivethirtyeight", "gapminder", "ggplot2movies", "infer", "ISLR",
"janitor", "knitr", "moderndive", "nycflights13", "scales", "tidyverse",
"broom",
# Packages only used internally for bookdown book building
"devtools", "ggrepel", "here", "kableExtra", "mvtnorm", "patchwork",
"remotes", "rmarkdown", "sessioninfo", "viridis", "webshot"
)
new_pkgs <- needed_CRAN_pkgs[!(needed_CRAN_pkgs %in% installed.packages())]
if (length(new_pkgs)) {
install.packages(new_pkgs, repos = "http://cran.rstudio.com")
}
# Install non-2.0 version of skimr so that histograms can be removed easily
# v2 TODO: Fix https://github.com/moderndive/ModernDive_book/issues/271
if (!"skimr" %in% installed.packages()) {
remotes::install_version("skimr", version = "1.0.6",
repos = "http://cran.rstudio.com")
}
```
```{r}
# Used in 95-appendixE.Rmd
needed_pkgs <- unique(c(
needed_CRAN_pkgs, "skimr", "bookdown"
))
# Automatically create a bib database for R packages
write_bib(
c(
.packages(),
"bookdown", "broom", "dplyr", "dygraphs", "fivethirtyeight", "ggplot2",
"ggplot2movies", "infer", "janitor", "kableExtra", "knitr", "moderndive",
"nycflights13", "readr", "rmarkdown", "skimr", "tibble", "tidyr",
"tidyverse", "webshot"
),
here::here("bib", "packages.bib")
)
# Check that phantomjs is installed to create screenshots of apps
if (is.null(webshot:::find_phantom())) {
webshot::install_phantomjs()
}
# Add all simulation results here
if (!dir.exists("rds")) {
dir.create("rds")
}
# Create empty docs folder which will ultimately contain output
if (!dir.exists("docs")) {
dir.create("docs")
}
# Make sure all images copy to docs folder
if (!dir.exists(here::here("docs", "images"))) {
dir.create(here::here("docs", "images"))
}
file.copy(from = "images", to = "docs", recursive = TRUE)
# These steps are only needed for generating the moderndive.com page
# with relevant links. Not needed for PDF generation.
if (is_html_output()) {
# Add all purl()'ed chapter R scripts here
if (dir.exists(here::here("docs", "scripts"))) {
unlink(here::here("docs", "scripts"), recursive = TRUE)
}
if (!dir.exists(here::here("docs", "scripts"))) {
dir.create(here::here("docs", "scripts"))
}
}
```
```{r results="asis", echo=FALSE, purl=FALSE}
if (is_html_output()) {
cat("# Welcome to ModernDive {-}")
}
```
<!-- purl() all the chapter Rmd's in a new session -->
```{bash, include=FALSE, purl=FALSE}
Rscript -e "source('purl.R', local = TRUE)"
```
```{r set-options2, include=FALSE, purl=FALSE}
if (is_html_output()) {
file.remove("purl.Rout")
# Copy all needed csv and txt files to docs/
if (!dir.exists(here::here("docs", "data"))) {
dir.create(here::here("docs", "data"))
}
data_files <- c(
"dem_score.csv", "dem_score.xlsx", "le_mess.csv", "ideology.csv",
# For Appendix B
"ageAtMar.csv", "offshore.csv", "cleSac.txt", "zinc_tidy.csv",
# For Appendix C
"movies.csv",
# For moderndive package paper
# https://www.kaggle.com/c/house-prices-advanced-regression-techniques
"train.csv", "test.csv"
)
from_vec <- here::here("data", data_files)
to_vec <- here::here("docs", "data", data_files)
purrr::walk2(from_vec, to_vec, file.copy, overwrite = TRUE)
# To be updated to include the actual link to labs website
# when Albert has those ready
file.copy("labs.html", here::here("docs", "labs.html"), overwrite = TRUE)
file.copy("regression-plane.html",
here::here("docs", "regression-plane.html"),
overwrite = TRUE
)
# Copy previous_versions/ to docs/previous_versions/
if (!dir.exists(here::here("docs", "previous_versions"))) {
dir.create(here::here("docs", "previous_versions"))
}
file.copy(from = "previous_versions", to = "docs", recursive = TRUE)
# For some reason widelogo in header needs to be done separately.
# Loaded in _includes/logo.html
file.copy(here::here("images", "logos", "wide_format.png"),
here::here("docs", "wide_format.png"),
overwrite = TRUE
)
}
# Set ggplot2 theme to be light if outputting to PDF
library(ggplot2)
if (is_latex_output()) {
theme_set(theme_light())
} else {
theme_set(theme_grey())
}
# For generating the R script files at the end of relevant chapters
generate_r_file_link <- function(file) {
if (is_latex_output()) {
cat(glue::glue("An R script file of all R code used in this chapter is available at <https://www.moderndive.com/scripts/{file}>."))
} else {
cat(glue::glue("An R script file of all R code used in this chapter is available [here](scripts/{file})."))
}
}
# To get kable tables to print nicely in .tex file
if (is_latex_output()) {
options(kableExtra.auto_format = FALSE, knitr.table.format = "latex")
}
```
```{r images, include=FALSE, purl=FALSE}
include_image <- function(path,
html_opts = "width=45%",
latex_opts = html_opts,
alt_text = "") {
if (is_html_output()) {
glue::glue("![{alt_text}]({path}){{ {html_opts} }}")
} else if (is_latex_output()) {
glue::glue("![{alt_text}]({path}){{ {latex_opts} }}")
}
}
image_link <- function(path,
link,
html_opts = "height: 200px;",
latex_opts = "width=0.2\\textwidth",
alt_text = "",
centering = TRUE) {
if (is_html_output()) {
if (centering) {
glue::glue('
<center><a target="_blank" class="page-link" href="{link}"><img src="{path}" style="{html_opts}"/></a></center>')
} else {
glue::glue('
<a target="_blank" class="page-link" href="{link}"><img src="{path}" style="{html_opts}"/></a>')
}
}
else if (is_latex_output()) {
if (centering) {
glue::glue("\\begin{{center}}
\\href{{{link}}}{{\\includegraphics[{latex_opts}]{{{path}}}}}
\\end{{center}}")
} else {
glue::glue("\\href{{{link}}}{{\\includegraphics[{latex_opts}]{{{path}}}}}")
}
}
}
```
<!--
```{block, type='announcement', purl=FALSE}
**This is a previous version (v`r version`) of *ModernDive* and may be out of date. For the current version of *ModernDive*, please go to [ModernDive.com](https://moderndive.com/).**
```
-->
<!--
```{block, type='learncheck', include=!is_latex_output(), purl=FALSE}
**Please note that you are currently looking at a preview of a future version of *ModernDive*, which is currently being edited. For the current version of *ModernDive*, please visit [ModernDive.com](https://moderndive.com/).**
```
-->
```{r, echo=FALSE, include=!is_latex_output(), purl=FALSE}
dev_version <- FALSE
```
<!-- include=FALSE for PDF sending to CRC -->
```{block, include=is_html_output(), purl=FALSE}
This is the [website](https://moderndive.com/) for *Statistical Inference via Data Science: A ModernDive into R and the Tidyverse*! Visit the [GitHub repository for this site](https://github.com/moderndive/ModernDive_book) and find the book on [Amazon](https://www.amazon.com/Statistical-Inference-via-Data-Science/dp/0367409828/). You can also purchase it at [CRC Press](https://www.routledge.com/Statistical-Inference-via-Data-Science-A-ModernDive-into-R-and-the-Tidyverse/Ismay-Kim/p/book/9780367409821) using promo code **ADC22** for a discounted price.
```
</br>
```{r results="asis", echo=FALSE, purl=FALSE}
if (is_html_output()) {
include_image(
path = "images/logos/book_cover.png",
html_opts = "width=350px"
)
}
```
</br>
<a rel="license" href="http://creativecommons.org/licenses/by-nc-sa/4.0/"><img alt="Creative Commons License" style="border-width:0" src="https://i.creativecommons.org/l/by-nc-sa/4.0/88x31.png" /></a><br />This work by [Chester Ismay](https://chester.rbind.io/) and [Albert Y. Kim](https://rudeboybert.rbind.io/) is licensed under a <a rel="license" href="http://creativecommons.org/licenses/by-nc-sa/4.0/">Creative Commons Attribution-NonCommercial-ShareAlike 4.0 International License</a>.
<!-- index.Rmd has to have some content in it or it won't create an index.html
file. Make sure to keep this in so that index.html is included. -->
# Foreword {-}