October 13, 2018

Class overview

  • Test ability to create PDF
  • Develop skills to create and format 1-page PDF document
  • Build your own PDF

Your project for the course

  • Create a 1-page PDF document
    • Include a title
    • Include at least 1 chart
    • Include at least 1 table
    • Submit the 1-page PDF to me by email on or before October 22, 2018

Brief overview of Markdown + knitr

  • Markdown is ‘an authoring framework for data science’
  • Multiple markdown languages exist
  • We will use R Markdown
  • R Markdown files integrate well with HTML, CSS, Pandoc Markdown, and Latex
  • We will use a little Latex to make PDF writing easier
  • For our purposes, knitr 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 button
  • Resources

Test ability to create PDF

  • Open a new .Rmd file (File > New File > R Markdown)
    • Select PDF from ‘Document’
  • Save the file you just created
  • Click ‘Knit’ to create PDF at the top of the R Studio window (look for the blue yarn)
  • The document should render in the R Studio viewer
  • The PDF file should appear in the same location you saved your .Rmd file

Test ability to create PDF

Did you run into problems? If so…

  1. Confirm you installed a Tex compiler (MacTex on iOS and MikTex on Windows). Install now if you haven’t already.
  2. If prompted, install Tex packages
  3. Try reinstalling the R packages below
  4. Ask for additional help
install.packages(c('rmarkdown', 'knitr', 'kableExtra'))

Anatomy of your .Rmd

Anatomy of your .Rmd

  • YAML or front-matter
  • Code chunks
  • Code for formatting (Markdown and LaTex)
  • Text

Anatomy of your .Rmd

  • YAML or front-matter
    • Use YAML to make universal configurations
    • Very top of the document between two sets of dashes (---)
  • Code chunks
  • Code for formatting (Markdown and LaTex)
  • Text

YAML

---
title: "What a great title!"
output: pdf_document
---

Anatomy of your .Rmd

  • YAML or front-matter
  • Code chunks
    • Transform data and build tables and visualizations within code chunks
    • Remember to use the 3 backward apostrophes ``` and { } in chunk header
  • Code for formatting (Markdown and LaTex)
  • Text

Code chunks

```{r, warning=FALSE, message=FALSE} library(tidyverse) # load libraries crime <- read_csv('https://goo.gl/FHW2Ni') %>% as.data.frame() ```

Anatomy of your .Rmd

  • YAML or front-matter
  • Code chunks
  • Code for formatting (Markdown and LaTex)
    • Format text with simple code
    • This is done outside of the code chunks
  • Text

Code for formatting (Markdown and LaTex)

# Use hashes for headers

**List (bolded)**
- Bullet 1
- Bullet 2

Text

Text size

  • Titles/Headers
  • Body

Text size

  • Titles/Headers
    • Write headers with hashes
    • Fewer hashes produces a larger header
    • Use title in YAML or hashes to write a document title
  • Body
# Header
## Header
### Header
#### Header
##### Header
###### Header

Text size

  • Titles/Headers
  • Body
    • Add fontsize to the YAML
    • Font size options are limited when developing PDF docs
    • Only recognizes inputs of 10pt, 11pt, and 12pt
    • fontsize variable affects all text elements
---
title: "Foo"
output: pdf_document
fontsize: 12pt
---

Text color

  • Use LaTex code to change colors
  • LaTex code is required when creating a PDF
    • Other options are available when creating other file types (i.e. HTML)
  • Predefined colors
black, blue, brown, cyan, darkgray, gray, green, lightgray, lime, magenta,  
olive, orange, pink, purple, red, teal, violet, white, yellow
  • To change text colors use the following convention
Look at the \textcolor{red}{balloons}!

Look at the balloons!

Text alignment

  • Use LaTex code to align text
  • To align text use the following convention
  • Aligment options
flushleft, flushright, flushcenter
  • To change text alignment use the following convention
\begin{flushcenter}Look at the \textcolor{red}{balloons}!\end{flushcenter}

Look at the balloons!

Other tricks for text

Add a hyperlink

[Add a hyperlink](https://www.a.url.com)



Bold, italicize, and underline words

**Bold words**
*Italicize words*
\underline{Underline words}

Bullet points

  • Use -, +, or * to create an un-ordered list
  • Use numbers followed by a period to create an ordered list (1.)
  • Indent for sub-items
- Item 1
- Item 2
  - Sub-item 1
  - Sub-item 2
  • Item 1
  • Item 2
    • Sub-item 1
    • Sub-item 2

Font style

  • ‘Off the shelf’ options are limited for font style
  • Three options
    • Roman font: \textrm{...}
    • Sans serif font: \textsf{...}
    • Teletype font: \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}!}  

Background color

  • Update the YAML
    • Note header-includes text and \usepackage text
  • Indicate the color of the page just below the YAML
---
title: "Foo"
output: pdf_document
fontsize: 12pt
header-includes:
- \usepackage{pagecolor}
---
\pagecolor{yellow}

Charts

Rendering charts

  • Create and format charts in code chunks just like in R Notebook
    • Following r in the braces is the name of the code chunk ({r plot})
    • Names must be unique
    • Also include echo=FALSE in braces. This hides your code in the output
    • You can divide code between two or more chunks if it is necessary
      • You should do this to manage your code/document and make your code easier to follow
```{r plot, echo=FALSE} library(tidyverse) mpg %>% ggplot() + geom_point(aes(displ, hwy)) ```

Rendering charts

  • Create and format charts in code chunks just like in R Notebook
    • Following r in the braces is the name of the code chunk ({r plot})
    • Names must be unique
    • Also include echo=FALSE in braces. This hides your code in the output
    • You can divide code between two or more chunks if it is necessary
      • You should do this to manage your code/document and make your code easier to follow

Chart size and alignment

  • Between braces use fig.width and fig.height
  • Start with values close to 4 and adjust accordingly
  • Use fig.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)) ```

Chart size and alignment

  • Between braces use fig.width and fig.height
  • Start with values close to 4 and adjust accordingly
  • Use fig.align to move the chart to the left, right, or center

Tables

knitr::kable() basics

  • Use kable() from knitr to create tables
  • Not all kable() functionality is available when creating PDFs
  • Include results = 'asis' in the chunk header or else the table will not appear
  • Create a table by calling a dataframe in the kable() function
```{r, echo = FALSE, warning=FALSE, message=FALSE, results = 'asis'} crime %>% mutate(occurred_time_ampm = ifelse(occurred_time >= 1200, 'PM', 'AM')) %>% group_by(occurred_time_ampm) %>% summarise(n = n()) %>% kable() ```

knitr::kable() basics

  • Use kable() from knitr to create tables
  • Not all kable() functionality is available when creating PDFs
  • Include results = 'asis' in the chunk header or else the table will not appear
  • Create a table by calling a dataframe in the kable() function
occurred_time_ampm n
AM 7452
PM 14257
NA 1750

Your project for the course

  • Create a 1-page PDF document
    • Include a title
    • Include at least 1 chart
    • Include at least 1 table
    • Submit the 1-page PDF to me by email on or before February 16, 2018

Appendix

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