class: center, middle, inverse, title-slide # > 📖 💻
> R markdown ###
Danielle Navarro
--- layout: true <div class="my-footer"> <!-- <span> <a href="https://robust-tools.djnavarro.net" target="_blank">robust-tools.djnavarro.net</a> </span> --> </div> --- class: middle ## What are we going to cover? - **Markdown:** a simple text-only format for writing documents - **R markdown:** a tool for mixing R code with markdown --- class: middle ## What is the assumed knowledge? - This is an introductory unit - No previous knowledge of R is needed - No previous knowledge of markdown needed --- class: middle ## What do I need to do before starting? - Sign up for a free RStudio cloud account - https://rstudio.cloud/ --- class: middle ## Where can I find the resources? - These slides: [slides.djnavarro.net/starting-rmarkdown](https://slides.djnavarro.net/starting-rmarkdown) - YouTube videos: [youtube.djnavarro.net/starting-rmarkdown](https://youtube.djnavarro.net/starting-rmarkdown) --- ## R markdown is a "chimera" .pull-left[ ![](img/chimera.jpg)<!-- --> ] -- .pull-right[ <br> ### On the input side... - Write text in markdown - Insert code using R - Write "metadata" with YAML - Insert equation with LaTeX ] --- ## R markdown is a "chimera" .pull-left[ ![](img/chimera.jpg)<!-- --> ] .pull-right[ <br> ### On the output side... - Output to HTML - Output to PDF - Output to Word - Many other variations too ] --- class: middle, inverse .hand[Lots of little things to learn!<br>We'll start with markdown...] --- class: middle, inverse background-image: url("img/silhouette.jpg") background-size: cover .pull-left-narrow[ .huge-bisque-number[1] ] .pull-right-wide[ <br><br><br> .larger[.embolden[Stating the problem...]] ] --- class: middle ![](img/word_document_surface.png)<!-- --> --- class: middle ![](img/word_document_inside.png)<!-- --> --- class: middle ## Word documents are complicated - A .docx file is a compressed folder with lots of files - Your text is buried in with a lot of formatting information --- class: middle <img src="img/html_document_surface.png" height="80%" /> --- class: middle ![](img/html_document_inside.png)<!-- --> --- class: middle ## HTML documents are complicated - Looks simple because I removed most formatting - Still unpleasant to look at --- class: middle, inverse background-image: url("img/silhouette.jpg") background-size: cover .pull-left-narrow[ .huge-bisque-number[2] ] .pull-right-wide[ <br><br><br> .larger[.embolden[Why markdown?]] ] --- class: middle ## What do we want? - Something that... you can write in **plain text** - Something that... is **human readable** - Something that... allows **formatting** - Something that... can **build** to something pretty --- class: middle, inverse .hand[Here's an example of a markdown document...] --- .pull-left-wide[ ``` # Introduction Welcome to my **awesome** class. You will learn all kinds of useful things about R markdown. ## Why should you care? - Markdown is simple and reproducible - You can make it pretty if you want to - The R Markdown variant lets you add R code ## Where can you find these slides? https://slides.djnavarro.net/starting-rmarkdown ``` ] --- class: middle, inverse .hand[Here's what the output looks like...] --- # Introduction Welcome to my **awesome** class. You will learn all kinds of useful things about R markdown. ## Why should you care? - Markdown is simple and reproducible - You can make it pretty if you want to - The R Markdown variant lets you add R code ## Where can you find these slides? https://slides.djnavarro.net/starting-rmarkdown --- class: middle ## Notice the theme! - My slides are written using markdown (...sort of) - So the markdown output you see here matches the slides theme - I'll talk about modifying themes later --- class: middle, inverse .hand[Okaaaay, but this seems really boring?] --- class: middle, inverse .hand[... hm, yeah, but let me show you where it takes you later!] --- class: middle ## Professional websites <img src="img/example_professional_site.png" width="50%" height="70%" /> --- class: middle ## Visual blogs <img src="img/example_visual_blog.png" width="50%" height="70%" /> --- class: middle ## Books <img src="img/example_book.png" width="80%" height="70%" /> --- class: middle ## Academic papers <img src="img/example_papers.png" width="60%" height="70%" /> --- class: middle ## Slide decks <img src="img/example_slides.png" width="80%" height="70%" /> --- class: middle ## Résumés <img src="img/example_resume.png" width="80%" height="70%" /> --- class: middle, inverse background-image: url("img/silhouette.jpg") background-size: cover .pull-left-narrow[ .huge-bisque-number[3] ] .pull-right-wide[ <br><br><br> .larger[.embolden[Getting ready]] ] --- class: middle, inverse .hand[Demonstration:<br>Create an RStudio Cloud project] --- class: middle, inverse background-image: url("img/silhouette.jpg") background-size: cover .pull-left-narrow[ .huge-bisque-number[4] ] .pull-right-wide[ <br><br><br> .larger[.embolden[Starting markdown]] ] --- class: middle, inverse .hand[Okay, let's dive into the details...] --- ## Emphasising text -- .pull-left[ <br><br> ### What you type... <br> ``` this is *italics* this is **bold** this is ***bold italics*** ``` ] -- .pull-right[ <br><br> ### What you get... <br> this is *italics* this is **bold** this is ***bold italics*** ] --- ## Creating lists -- .pull-left[ <br><br> ### What you type... <br> ``` - unnumbered lists - look like this 1. numbered lists 2. look like this ``` ] -- .pull-right[ <br><br> ### What you get... <br> - unnumbered lists - look like this 1. numbered lists 2. look like this ] --- ## Creating headings -- .pull-left[ <br><br> ### What you type... <br> ``` # Level 1 heading ## Level 2 heading ### Level 3 heading ``` ] -- .pull-right[ <br><br> ### What you get... <br> # Level 1 heading ## Level 2 heading ### Level 3 heading ] --- class: middle, inverse .hand[Demonstration:<br>Write a markdown document] --- class: middle, inverse .hand[Do it yourself!] - create your own RStudio Cloud project (see the demo) - open a new plain markdown document - save it as `my_first_markdown.md` - write some markdown, build it to HTML file - try to use all these markdown features --- class: middle, inverse background-image: url("img/silhouette.jpg") background-size: cover .pull-left-narrow[ .huge-bisque-number[5] ] .pull-right-wide[ <br><br><br> .larger[.embolden[More markdown]] ] --- ## Inserting hyperlinks -- .pull-left-wide[ <br><br> ### What you type... <br> ``` https://placekitten.com [Placekitten](https://placekitten.com) ``` ] -- .pull-right-narrow[ <br><br> ### What you get... <br> https://placekitten.com [Placekitten](https://placekitten.com) ] --- ## Inserting local images -- .pull-left-wide[ <br><br> ### What you type... <br> ``` ![](./img/rmarkdown.png) ``` ] -- .pull-right-narrow[ <br><br> ### What you get... <br> ![](./img/rmarkdown.png) ] --- ## Inserting images from the web -- .pull-left-wide[ <br><br> ### What you type... <br> ``` ![](https://placekitten.com/200/300) ``` ] -- .pull-right-narrow[ <br><br> ### What you get... <br> ![](https://placekitten.com/200/300) ] --- ## Using blockquotes -- .pull-left[ <br><br> ### What you type... <br> ``` This is normal text > This is a block quote ``` ] -- .pull-right[ <br><br> ### What you get... This is normal text > This is a block quote ] --- ## Using nested lists -- .pull-left[ <br><br> ### What you type... <br> ``` 1. First item in list 2. Second item in list - first item in sub-list - second item in sub-list 3. Third item in list ``` ] -- .pull-right[ <br><br> ### What you get... <br> 1. First item in list 2. Second item in list - first item in sub-list - second item in sub-list 3. Third item in list ] --- class: middle, inverse .hand[Do it yourself!] - go back to your RStudio Cloud project - open your `my_first_markdown.md` document - edit the markdown, use these new features - save the document - build it to HTML --- class: middle, inverse background-image: url("img/silhouette.jpg") background-size: cover .pull-left-narrow[ .huge-bisque-number[6] ] .pull-right-wide[ <br><br><br> .larger[.embolden[R markdown]] ] --- class: middle ## What do we want? - Something that... lets you write text in **markdown** - Something that... lets you include **R code** (e.g. for data vis) - Something that... can **build** to HTML, PDF, Word, etc --- class: middle, inverse .hand[Demonstration:<br>Write an R markdown document] --- class: middle ## Let's dissect the document we just wrote! <img src="img/my_first_rmd_plain.png" width="60%" height="70%" /> --- ## The part inside ` --- ` is the "YAML header" <img src="img/my_first_rmd_yaml.png" width="60%" height="70%" /> --- class: middle <h2>The part inside <code>```{r}</code> is R code</h2> <img src="img/my_first_rmd_r.png" width="60%" height="70%" /> --- ## The rest is markdown <img src="img/my_first_rmd_markdown.png" width="60%" height="70%" /> --- class: middle, inverse background-image: url("img/silhouette.jpg") background-size: cover .pull-left-narrow[ .huge-bisque-number[7] ] .pull-right-wide[ <br><br><br> .larger[.embolden[Custom documents]] ] --- ## The YAML header is used for customisation <img src="img/my_first_rmd_yaml.png" width="60%" height="70%" /> --- class: middle ## Wait... what's the YAML acronym? - Originally: "Yet Another Markup Language" - Later: "YAML Ain't Markup Language" - ... it really doesn't matter 🙄 --- ## Output HTML only ``` --- title: "My First R Markdown Document" author: "Danielle J. Navarro" date: "10/02/2021" output: html_document --- ``` --- ## Output HTML and PDF ``` --- title: "My First R Markdown Document" author: "Danielle J. Navarro" date: "10/02/2021" output: pdf_document: default html_document: default --- ``` -- <br> - Notice the indentation - YAML is *very* picky about this --- class: middle, inverse .hand[Demonstration!] --- ## Themes for HTML documents ``` --- output: html_document: theme: readable --- ``` -- **Possible themes**: default, cerulean, journal, flatly, darkly, readable, spacelab, united, cosmo, lumen, paper, sandstone, simplex, yeti --- class: middle, inverse .hand[Demonstration!] --- ## Table of contents ``` output: html_document: toc: true toc_float: true ``` --- class: middle, inverse .hand[Demonstration!] --- class: middle, inverse .hand[Do it yourself!] - practice editing the YAML header - try creating different outputs and themes - try creating a floating table of contents - try to get it to not float! --- class: middle, inverse background-image: url("img/silhouette.jpg") background-size: cover .pull-left-narrow[ .huge-bisque-number[8] ] .pull-right-wide[ <br><br><br> .larger[.embolden[Inserting equations]] ] --- ## An overview - Anything inside dollar signs `$` is treated as "inline" maths - Anything inside two dollar `$$` is a standalone equation - Whitespace matters: `$x$` is an equation, `$x $` is not - Equations follow "LaTeX" rules --- ## Equations are special .pull-left[ ``` This $x^2$ is inline This equation is standalone $$ a^2 + b^2 = c^2 $$ ``` ] -- .pull-right[ This `\(x^2\)` is inline This equation is standalone $$ a^2 + b^2 = c^2 $$ ] --- ## Literal $ signs are "easy" .pull-left[ ``` $ is text $10 is text $10 and $20 are text $10 $ and $20 $ are text $10$ uses math mode ``` ] -- .pull-right[ $ is text $10 is text $10 and $20 are text $10 $ and $20 $ are text `\(10\)` uses math mode ] --- ## Rescorla-Wagner model .pull-left-wide[ ``` The formula for the associative strength $V$ at time $t+1$ is given by $$ V^{t+1} = V^{t} + \Delta V $$ where $\Delta V$ is given by $$ \Delta V = \alpha \beta (\lambda - V_{tot}) $$ ``` ] --- ## Rescorla-Wagner model .pull-left-wide[ The formula for the associative strength `\(V\)` at time `\(t+1\)` is given by $$ V^{t+1} = V^{t} + \Delta V $$ where `\(\Delta V\)` is given by $$ \Delta V = \alpha \beta (\lambda - V_{tot}) $$ ] --- class: middle, inverse .hand[There's more to learn about LaTeX equations<br>but that is a topic for another time!] --- class: middle, inverse background-image: url("img/silhouette.jpg") background-size: cover .pull-left-narrow[ .huge-bisque-number[9] ] .pull-right-wide[ <br><br><br> .larger[.embolden[Publishing documents]] ] --- class: middle, inverse .hand[Demonstration!] - upload the .Rmd and .html files: e.g., dropbox, github, osf - one-click publishing with RStudio through RPubs --- class: middle background-image: url("img/silhouette.jpg") background-size: cover .hand[And we're done!<br>For more information on R markdown...] <a href="https://bookdown.org/yihui/rmarkdown/"> <img src="img/rmd-guide-cover.png" width="30%" height="30%" /> </a>