forked from moderndive/ModernDive_book
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.Rmd
executable file
·247 lines (209 loc) · 8.79 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
---
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}
# 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 <- "0.7.0.9000"
date <- format(Sys.time(), '%B %d, %Y')
# Latest release information:
latest_release_version <- "0.6.1"
latest_release_date <- "August 28, 2019"
# Set output options
if(knitr:::is_html_output())
options(width = 80)
if(knitr:::is_latex_output())
options(width = 75)
options(digits = 7, bookdown.clean_book = TRUE, knitr.kable.NA = 'NA')
knitr::opts_chunk$set(
tidy = FALSE,
out.width = '\\textwidth',
fig.align = "center",
comment = NA
)
# CRAN packages needed
needed_CRAN_pkgs <- c(
# Data packages:
"nycflights13", "ggplot2movies", "fivethirtyeight", "gapminder", "ISLR",
# Explicitly used packages:
"tidyverse", "rmarkdown", "knitr", "janitor", "skimr",
"infer",
"moderndive",
# Internally used packages:
"webshot", "mvtnorm", "remotes", "devtools", "dygraphs", "gridExtra",
"kableExtra", "scales", "viridis", "ggrepel"
)
new_pkgs <- needed_CRAN_pkgs[!(needed_CRAN_pkgs %in% installed.packages())]
if(length(new_pkgs)) {
install.packages(new_pkgs, repos = "http://cran.rstudio.com")
}
# GitHub packages needed
if(!"patchwork" %in% installed.packages()){
remotes::install_github("thomasp85/patchwork")
}
#remotes::install_github("tidymodels/infer")
#remotes::install_github("moderndive/moderndive)
needed_pkgs <- unique(c(needed_CRAN_pkgs, "patchwork", "moderndive", "infer"))
# Check that phantomjs is installed to create screenshots of apps
if(is.null(webshot:::find_phantom()))
webshot::install_phantomjs()
# Automatically create a bib database for R packages
knitr::write_bib(
c(.packages(), "bookdown", "knitr", "rmarkdown", "nycflights13",
"ggplot2", "webshot", "dygraphs", "dplyr",
"ggplot2movies", "fivethirtyeight", "tibble", "readr", "tidyr",
"janitor", "infer", "skimr", "kableExtra", "moderndive", "tidyverse",
"broom"
),
"bib/packages.bib"
)
# Add all simulation results here
if(!dir.exists("rds"))
dir.create("rds")
# Create empty docs folder
if(!dir.exists("docs")) dir.create("docs")
# Make sure all images copy to docs folder
if(!dir.exists("docs/images")) dir.create("docs/images")
system("cp -r images/* docs/images/")
# These steps are only needed for generating the moderndive.com page
# with relevant links. Not needed for PDF generation.
if(knitr::is_html_output()){
# Add all knitr::purl()'ed chapter R scripts here
system("rm -rf docs/scripts/")
if(!dir.exists("docs/scripts"))
dir.create("docs/scripts")
system("R CMD batch purl.R")
system("rm purl.Rout")
# Copy all needed csv and txt files to docs/
# Should switch to use purrr here at some point
if(!dir.exists("docs/data"))
dir.create("docs/data")
file.copy("data/dem_score.csv", "docs/data/dem_score.csv", overwrite = TRUE)
file.copy("data/dem_score.xlsx", "docs/data/dem_score.xlsx", overwrite = TRUE)
file.copy("data/le_mess.csv", "docs/data/le_mess.csv", overwrite = TRUE)
file.copy("data/ideology.csv", "docs/data/ideology.csv", overwrite = TRUE)
# For Appendix B
file.copy("data/ageAtMar.csv", "docs/data/ageAtMar.csv", overwrite = TRUE)
file.copy("data/offshore.csv", "docs/data/offshore.csv", overwrite = TRUE)
file.copy("data/cleSac.txt", "docs/data/cleSac.txt", overwrite = TRUE)
file.copy("data/zinc_tidy.csv", "docs/data/zinc_tidy.csv", overwrite = TRUE)
# To be updated to include the actual link to labs (likely in blogdown format?)
# when Albert has those ready
file.copy("labs.html", "docs/labs.html", overwrite = TRUE)
# Copy previous_versions/ to docs/previous_versions/
# Should switch to use purrr here at some point
if(!dir.exists("docs/previous_versions"))
dir.create("docs/previous_versions/")
system("cp -r previous_versions/* docs/previous_versions/")
# For some reason logo needs to be done separately.
# Loaded in _includes/logo.html
file.copy("images/logos/wide_format.png", "docs/wide_format.png",
overwrite = TRUE)
}
# Set ggplot2 theme to be light if outputing to PDF.
library(ggplot2)
if(knitr::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(knitr::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(knitr::is_latex_output())
options(kableExtra.auto_format = FALSE, knitr.table.format = "latex")
```
```{r images, include=FALSE}
include_image <- function(path,
html_opts = "width=45%",
latex_opts = html_opts,
alt_text = ""){
if(knitr:::is_html_output()){
glue::glue("![{alt_text}]({path}){{ {html_opts} }}")
} else if(knitr:::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(knitr:::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(knitr:::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}}}}}')
}
}
```
```{r echo=FALSE, results="asis"}
if(knitr::is_html_output())
cat("# Special Announcement {-}")
```
<!-- include=FALSE for PDF sending to CRC -->
```{block, type='announcement', purl=FALSE, include=knitr::is_html_output()}
**We're excited to announce that we've signed a book deal with CRC Press! We will be publishing our first fully complete online version of ModernDive in Summer 2019, with a corresponding print edition to follow in Fall 2019. Don't worry though, our content will remain freely available on [ModernDive.com](https://moderndive.com/).**
```
<center>
```{r echo=FALSE, purl=FALSE, results="asis"}
if(knitr::is_html_output()){
include_image(path = "images/logos/crc_press.jpg",
html_opts="height=150px")
}
```
</center>
<!--
```{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=!knitr::is_latex_output(), purl=FALSE}
**Please note that you are currently looking at the "development version" of ModernDive, which is a work in progress currently being edited and thus subject to frequent change. For the latest "released version" of ModernDive, which is updated around twice a year, please visit [ModernDive.com](https://moderndive.com/).**
```
```{r, echo=FALSE, include=!knitr::is_latex_output(), purl=FALSE}
dev_version <- TRUE
```
<!-- index.Rmd has to have some content in it or it won't create an index.html file. When we remove the Special Announcement, make sure to keep this in so that index.html is included. -->
# Foreword {-}