class: center, middle, inverse, title-slide # A Primer on R Markdown ###
Joshua D. Barillas
Manager, Data Analysis - AACSB International
2019-06-25
@Tampa
R User Group
--- # R Markdown .left-column[ Plain text file with 3 types of content: - YAML header surrounded by `---` - Text in markdown - Code chunks surrounded by ``` ] .right-column[  ] --- # Why Should I Care? .center[ ### Old Path ] .right-column[ .pull-left[ .center[ Query <svg style="height:24pt;fill:black;" viewBox="0 0 448 512"><path d="M413.1 222.5l22.2 22.2c9.4 9.4 9.4 24.6 0 33.9L241 473c-9.4 9.4-24.6 9.4-33.9 0L12.7 278.6c-9.4-9.4-9.4-24.6 0-33.9l22.2-22.2c9.5-9.5 25-9.3 34.3.4L184 343.4V56c0-13.3 10.7-24 24-24h32c13.3 0 24 10.7 24 24v287.4l114.8-120.5c9.3-9.8 24.8-10 34.3-.4z"/></svg> Analyze <svg style="height:24pt;fill:black;" viewBox="0 0 448 512"><path d="M413.1 222.5l22.2 22.2c9.4 9.4 9.4 24.6 0 33.9L241 473c-9.4 9.4-24.6 9.4-33.9 0L12.7 278.6c-9.4-9.4-9.4-24.6 0-33.9l22.2-22.2c9.5-9.5 25-9.3 34.3.4L184 343.4V56c0-13.3 10.7-24 24-24h32c13.3 0 24 10.7 24 24v287.4l114.8-120.5c9.3-9.8 24.8-10 34.3-.4z"/></svg> Graphics <svg style="height:24pt;fill:black;" viewBox="0 0 448 512"><path d="M413.1 222.5l22.2 22.2c9.4 9.4 9.4 24.6 0 33.9L241 473c-9.4 9.4-24.6 9.4-33.9 0L12.7 278.6c-9.4-9.4-9.4-24.6 0-33.9l22.2-22.2c9.5-9.5 25-9.3 34.3.4L184 343.4V56c0-13.3 10.7-24 24-24h32c13.3 0 24 10.7 24 24v287.4l114.8-120.5c9.3-9.8 24.8-10 34.3-.4z"/></svg> Report ] ] .pull-right[ .left[     ] ] ] .left-column[ Problems with method: * Time consuming * Copy/pasting (likelyhood of errors<svg style="height:16pt;fill:#777;" viewBox="0 0 448 512"><path d="M34.9 289.5l-22.2-22.2c-9.4-9.4-9.4-24.6 0-33.9L207 39c9.4-9.4 24.6-9.4 33.9 0l194.3 194.3c9.4 9.4 9.4 24.6 0 33.9L413 289.4c-9.5 9.5-25 9.3-34.3-.4L264 168.6V456c0 13.3-10.7 24-24 24h-32c-13.3 0-24-10.7-24-24V168.6L69.2 289.1c-9.3 9.8-24.8 10-34.3.4z"/></svg>) * Requires multiple staff/applications ] --- # Most Important .pull-left[ [](https://bookdown.org/yihui/rmarkdown/) ] .pull-right[[](https://www.rstudio.com/resources/cheatsheets/#rmarkdown) ] --- # Reproducible Workflow using R Markdown .left-column[ R Markdown Combines: * Prose * Code * Results Output formats: * Web documents __`.html`__ * Static documents with LaTeX __`.pdf`__ * MS Word __`.docx`__ * Powerpoint __`.pptx`__ * EPUB __`.epub`__ ] .right-column[ .pull-left[ 1. Text in markdown: .center[Write \*\*what\*\* you want \*however\* you want] .center[<svg style="height:12pt;fill:black;" viewBox="0 0 448 512"><path d="M413.1 222.5l22.2 22.2c9.4 9.4 9.4 24.6 0 33.9L241 473c-9.4 9.4-24.6 9.4-33.9 0L12.7 278.6c-9.4-9.4-9.4-24.6 0-33.9l22.2-22.2c9.5-9.5 25-9.3 34.3.4L184 343.4V56c0-13.3 10.7-24 24-24h32c13.3 0 24 10.7 24 24v287.4l114.8-120.5c9.3-9.8 24.8-10 34.3-.4z"/></svg>] .center[Write __what__ you want _however_ you want] 2. Inline or Chunks of Code: .center[Today's date is \` `r Sys.Date()`\`] .center[<svg style="height:12pt;fill:black;" viewBox="0 0 448 512"><path d="M413.1 222.5l22.2 22.2c9.4 9.4 9.4 24.6 0 33.9L241 473c-9.4 9.4-24.6 9.4-33.9 0L12.7 278.6c-9.4-9.4-9.4-24.6 0-33.9l22.2-22.2c9.5-9.5 25-9.3 34.3.4L184 343.4V56c0-13.3 10.7-24 24-24h32c13.3 0 24 10.7 24 24v287.4l114.8-120.5c9.3-9.8 24.8-10 34.3-.4z"/></svg>] .center[Today's date is 2019-06-25] 3. Figures (*Outputs*): .center[ <!-- --> ] ] .pull-right[  ] ] ??? Reproducible documents: If error discovered, or if additional data, can just re-compile the report and get new or corrected results. Versus having to reconstruct figures and tables, paste them into a Word document, and further hand-edit various detailed results. **LITERATE PROGRAMMING** --- # PDF .pull-left[  ] .pull-right[  ] ??? --- # HTML Documents  --- # Word Documents  --- # Presentations  --- # Dashboards  --- # Tutorials  --- # Books .left-column[ [  ](https://bookdown.org/yihui/bookdown/) ] .right-column[  ] --- # Blogs .left-column[[](https://bookdown.org/yihui/blogdown/)] .right-column[[](https://alison.rbind.io/post/2017-06-12-up-and-running-with-blogdown/)] --- # Paged HTML Documents .left-column[ Paginated HTML with R Markdown: [pagedown](https://github.com/rstudio/pagedown) Current templates include [JSS article, CV, letter, business cards](https://pagedown.rbind.io/#applications) ] .right-column[ .pull-left[  ] .pull-right[  ] ] ??? Uses Paged.js polyfill to implement CSS support R Markdown document, means all the advantages are available Most stable in Chrome browser Customizable using HTML/CSS: - add university design - work with marketing dept/webmaster - Combine the content creation and styling - Perfect for reports where styling does not change a lot (annual reports) --- class: middle, center # [Markdown](https://www.markdowntutorial.com/) --- layout:true class: middle, left --- # Headers  --- # Text  --- # Lists  --- # Hyperlinks  --- # Images  --- # Math Expressions  --- # Math Equations  --- class: middle, center # Code --- # Inline Place code within a sentence with ` `r *code here*` `. R Markdown will replace the code with its results. For example, ` `r 87 + 78` ` will evaluate to 165 --- # Chunks Insert a "chunk" of R code using backticks and braces.  When the report is rendered, R Markdown will evaluate the code and include the results, while also removing the backticks and braces. Shortcut to Insert Chunk: * **Ctrl + Alt + I** (PC) * **Cmd + Option + I** (Mac) --- # Chunk Options  --- # echo = FALSE  --- # eval = FALSE  --- # include = FALSE  --- # fig.height/fig.width  --- # How it Works .center[] --- class: middle, center # Parameterized --- # YAML .pull-left[  ] .pull-right[  ] --- # Parameters  --- # Using Parameters ```yaml --- params: location: Southern Brewing date: !r format(Sys.Date(), "%B %d, %Y") --- Welcome to this workshop, given at `r params$location` on `r params$date`. ``` .center[<svg style="height:26pt;fill:black;" viewBox="0 0 448 512"><path d="M413.1 222.5l22.2 22.2c9.4 9.4 9.4 24.6 0 33.9L241 473c-9.4 9.4-24.6 9.4-33.9 0L12.7 278.6c-9.4-9.4-9.4-24.6 0-33.9l22.2-22.2c9.5-9.5 25-9.3 34.3.4L184 343.4V56c0-13.3 10.7-24 24-24h32c13.3 0 24 10.7 24 24v287.4l114.8-120.5c9.3-9.8 24.8-10 34.3-.4z"/></svg>] Welcome to this workshop, given at Southern Brewing on June 25, 2019. --- # render() Render at the command line with the default YAML options ```r library(rmarkdown) render("my-presentation.Rmd") ``` Render at the command line with set parameters ```r library(rmarkdown) render("my-presentation.Rmd", params = list(location = "USF", date = "August 5, 2019")) ``` --- # Things to Remember 1. Document is ran in a fresh R session using knitr, therefore all libraries used should be loaded *within the document* 2. Objects created in a code chunk are available to code in later chunks --- # Knitr is MULTI-Lingual * SQL * Python * SAS * [Many more](https://bookdown.org/yihui/rmarkdown/language-engines.html) --- # engine  --- # Note for SAS  --- # Resources .pull-left[ [](https://bookdown.org/yihui/rmarkdown/) ] .pull-right[[](https://www.rstudio.com/resources/cheatsheets/#rmarkdown) ] --- class: left, middle # Reproducibility .left-column[ Everything is text so can take advantage through: - Copy/pasting code errors into search engines - Portability (minimal file size) - Version contol (git) ] .right-column[  ] --- class: center, middle # Questions?