- Test ability to create PDF
- Develop skills to create and format 1-page PDF document
- Build your own PDF
October 13, 2018
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', 'kableExtra'))
---)YAML
--- title: "What a great title!" output: pdf_document ---
``` and { } in chunk headerCode chunks
```{r, warning=FALSE, message=FALSE} library(tidyverse) # load libraries crime <- read_csv('https://goo.gl/FHW2Ni') %>% as.data.frame() ```
Code for formatting (Markdown and LaTex)
# Use hashes for headers **List (bolded)** - Bullet 1 - Bullet 2
title in YAML or hashes to write a document title# 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 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'} crime %>% mutate(occurred_time_ampm = ifelse(occurred_time >= 1200, 'PM', 'AM')) %>% group_by(occurred_time_ampm) %>% summarise(n = n()) %>% kable() ```
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| occurred_time_ampm | n |
|---|---|
| AM | 7452 |
| PM | 14257 |
| NA | 1750 |
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 |