- Test ability to create PDF
- Refresh R skills with 2 exercises
- Develop skills to create and format 1-page PDF document
- Build your own PDF
February 7, 2017
knitrknitr is a button at the top of the R Studio window that compiles your document
knitr is magical and so much more than just a buttonDid you run into problems? If so…
install.packages(c('rmarkdown', 'knitr'))
c('MICROSOFT', 'THE COCA-COLA COMPANY', 'NUCOR STEEL SEATTLE, INC.', 'FARMERS INSURANCE', 'UNIVERSITY OF WASHINGTON')filter and group_by() %>% summarise()contributor_employer_name variableHint
c('MICROSOFT', 'THE COCA-COLA COMPANY', 'NUCOR STEEL SEATTLE, INC.', 'FARMERS INSURANCE', 'UNIVERSITY OF WASHINGTON')filter and group_by() %>% summarise()contributor_employer_name variableHint
donor %>%
filter(contributor_employer_name %in% c('MICROSOFT', 'THE COCA-COLA COMPANY'
, 'NUCOR STEEL SEATTLE, INC.'
, 'FARMERS INSURANCE'
, 'UNIVERSITY OF WASHINGTON')) %>%
group_by() %>%
summarise()
c('MICROSOFT', 'THE COCA-COLA COMPANY', 'NUCOR STEEL SEATTLE, INC.', 'FARMERS INSURANCE', 'UNIVERSITY OF WASHINGTON')filter and group_by() %>% summarise()contributor_employer_name variableHint
donor %>%
filter(contributor_employer_name %in% c('MICROSOFT', 'THE COCA-COLA COMPANY'
, 'NUCOR STEEL SEATTLE, INC.'
, 'FARMERS INSURANCE'
, 'UNIVERSITY OF WASHINGTON')) %>%
group_by(contributor_employer_name) %>%
summarise(
avg_amount = mean(amount)
, max_amount = max(amount)
)
donor %>%
filter(contributor_employer_name %in% c('MICROSOFT', 'THE COCA-COLA COMPANY'
, 'NUCOR STEEL SEATTLE, INC.'
, 'FARMERS INSURANCE'
, 'UNIVERSITY OF WASHINGTON')) %>%
group_by(contributor_employer_name) %>%
summarise(avg_amount = mean(amount), max_amount = max(amount))
donor %>%
filter(contributor_employer_name %in% c('MICROSOFT', 'THE COCA-COLA COMPANY'
, 'NUCOR STEEL SEATTLE, INC.'
, 'FARMERS INSURANCE'
, 'UNIVERSITY OF WASHINGTON')) %>%
group_by(contributor_employer_name) %>%
summarise(avg_amount = mean(amount), max_amount = max(amount))
## # A tibble: 5 x 3
## contributor_employer_name avg_amount max_amount
## <fctr> <dbl> <dbl>
## 1 FARMERS INSURANCE 27.1 182
## 2 MICROSOFT 387 1400
## 3 NUCOR STEEL SEATTLE, INC. 1.49 10.0
## 4 THE COCA-COLA COMPANY 9.75 40.4
## 5 UNIVERSITY OF WASHINGTON 144 500
filter
filter code to subset the data as in Exercise 1<- if it helps you build your data visualizationNA values so that they are Unknown
ifelse() in the mutate() functionHint
donor_sds <- donor %>%
filter(contributor_employer_name %in% c('MICROSOFT', 'THE COCA-COLA COMPANY'
, 'NUCOR STEEL SEATTLE, INC.'
, 'FARMERS INSURANCE'
, 'UNIVERSITY OF WASHINGTON'))
Hint
donor_sds <- donor %>%
filter(contributor_employer_name %in% c('MICROSOFT', 'THE COCA-COLA COMPANY'
, 'NUCOR STEEL SEATTLE, INC.'
, 'FARMERS INSURANCE'
, 'UNIVERSITY OF WASHINGTON'))
donor_sds %>% ggplot(aes()) +
In the subset of contributors data analyzed in Exercise 1, which party received the most contributions?
donor_sds <- donor %>%
filter(contributor_employer_name %in% c('MICROSOFT', 'THE COCA-COLA COMPANY'
, 'NUCOR STEEL SEATTLE, INC.'
, 'FARMERS INSURANCE'
, 'UNIVERSITY OF WASHINGTON')) %>%
mutate(party = ifelse(party %in% NA, 'Unknown', as.character(party)))
donor_sds %>% ggplot(aes(party)) + geom_bar() + theme_economist() +
labs(x = '', y = 'Donations (#)')
In the subset of contributors data analyzed in Exercise 1, which party received the most contributions?
To what extent is there a relationship between between party and a geography variable (location, city, county, region, etc.) for the subset of contributors from Exercise 1?
donor_sds <- donor %>%
filter(contributor_employer_name %in% c('MICROSOFT', 'THE COCA-COLA COMPANY'
, 'NUCOR STEEL SEATTLE, INC.'
, 'FARMERS INSURANCE'
, 'UNIVERSITY OF WASHINGTON')) %>%
mutate(party = ifelse(party %in% NA, 'Unknown', as.character(party))) %>%
filter(! party %in% 'Unknown')
donor_sds %>% ggplot(aes(x= contributor_state, fill = party)) +
geom_bar() + theme_economist() + labs(x = 'State', y = '')
To what extent is there a relationship between between party and a geography variable (location, city, county, region, etc.) for the subset of contributors from Exercise 1?
---)YAML
--- title: "What a great title!" output: pdf_document ---
``` and { } in chunk headerCode chunks
```{r, warning=FALSE, message=FALSE} library(tidyverse) # load libraries police <- read.csv('https://goo.gl/nNAuDy') # read your data ```
Code for formatting (Markdown and LaTex)
# Use hashes for headers **List (bolded)** - Bullet 1 - Bullet 2
# Header ## Header ### Header #### Header ##### Header ###### Header
fontsize to the YAML10pt, 11pt, and 12ptfontsize variable affects all text elements--- title: "Foo" output: pdf_document fontsize: 12pt ---
black, blue, brown, cyan, darkgray, gray, green, lightgray, lime, magenta, olive, orange, pink, purple, red, teal, violet, white, yellow
Look at the \textcolor{red}{balloons}!
Look at the balloons!
flushleft, flushright, flushcenter
\begin{flushcenter}Look at the \textcolor{red}{balloons}!\end{flushcenter}
Look at the balloons!
[Add a hyperlink](https://www.a.url.com)
Bold, italicize, and underline words
**Bold words**
*Italicize words*
\underline{Underline words}
-, +, or * to create an un-ordered list1.)- Item 1 - Item 2 - Sub-item 1 - Sub-item 2
\textrm{...}\textsf{...}\texttt{...}Look at the \textcolor{red}{balloons}!
\textsf{Look at the \textcolor{red}{balloons}!}
\textrm{Look at the \textcolor{red}{balloons}!}
\texttt{Look at the \textcolor{red}{balloons}!}
header-includes text and \usepackage text---
title: "Foo"
output: pdf_document
fontsize: 12pt
header-includes:
- \usepackage{pagecolor}
---
\pagecolor{yellow}
r in the braces is the name of the code chunk ({r plot})echo=FALSE in braces. This hides your code in the output```{r plot, echo=FALSE} library(tidyverse) mpg %>% ggplot() + geom_point(aes(displ, hwy)) ```
r in the braces is the name of the code chunk ({r plot})echo=FALSE in braces. This hides your code in the outputfig.width and fig.heightfig.align to align to move the chart to the left, right, or center```{r plot, echo=FALSE, fig.width=7, fig.height=4, fig.align='right'} library(tidyverse) mpg %>% ggplot() + geom_point(aes(displ, hwy)) ```
fig.width and fig.heightfig.align to move the chart to the left, right, or centerknitr::kable() basicskable() from knitr to create tableskable() functionality is available when creating PDFsresults = 'asis' in the chunk header or else the table will not appearkable() function```{r, echo = FALSE, warning=FALSE, message=FALSE, results = 'asis'} police %>% group_by(event_clearance_ampm) %>% summarise(n = n()) %>% kable(format = 'markdown') ```
knitr::kable() basicskable() from knitr to create tableskable() functionality is available when creating PDFsresults = 'asis' in the chunk header or else the table will not appearkable() function| event_clearance_ampm | n |
|---|---|
| AM | 9026 |
| PM | 897 |
| NA | 77 |
knitr::kable() arguments and functions by formats| arguments | latex | markdown | pandoc |
|---|---|---|---|
| caption | X | X | |
| digits | X | X | X |
| align | X | X | X |
| column_spec | X | ||
| row_spec | X | ||
| kable_styling | X |
knitr::kable() arguments and functions| arguments | description |
|---|---|
| caption | Title of the table |
| digits | Number of decimal places for numbers |
| align | Cell alignment (“l”, “r”, “c”) |
| column_spec | Change column width, color, and other characteristics |
| row_spec | Change row color, text angle, and other characteristics |
| kable_styling | Change font size and placement of the table |