layout: true <!-- this adds the link footer to all slides, depends on footer-small class in css--> <div class="footer-small"><span>https://github.com/mjfrigaard/csuc-data-journalism</div> --- name: title-slide class: title-slide, center, middle, inverse # Introduction to R Markdown #.fancy[Combine text, code, and output in one document] <br> .large[by Martin Frigaard] Written: October 03 2021 Updated: November 30 2021 .footer-large[.right[.fira[ <br><br><br><br><br>[Created using the "λέξις" theme](https://jhelvy.github.io/lexis/index.html#what-does-%CE%BB%CE%AD%CE%BE%CE%B9%CF%82-mean) ]]] --- <img src="img/rmarkdown_wizards.png" width="70%" height="70%" /> .footer-large[.right[Artwork by @allison_horst]] --- background-image: url(img/rmarkdown.png) background-position: 93% 10% background-size: 7% # Materials ### Link to slides: https://mjfrigaard.github.io/csuc-data-journalism/slides.html ### Link to exercises: https://mjfrigaard.github.io/csuc-data-journalism/lessons-exercises.html --- class: inverse, center background-image: url(img/rmarkdown.png) background-position: 93% 10% background-size: 7% # *What is RMarkdown?* -- ## Three technologies: -- ### 1) Markdown is a plain text markup language for capturing .blue[human-readable prose] -- ### 2) Data manipulation/graphing/statistical language engines for computing .blue[machine-readable code] -- ### 3) Multiple .blue[output options] for creating PDFs, Word docs, PowerPoints, HTML, etc. --- class: center, top # How R Markdown works ## `rmarkdown` works directly with `knitr` .leftcol[ `rmarkdown` combines YAML, markdown, and R code into a markdown document and passes it to `knitr` <img src="img/rmarkdown.png" width="20%" height="20%" style="display: block; margin: auto;" /> ] -- .rightcol[ `knitr` uses `pandoc` (a universal document conversion tool) to generate the specified document format <img src="img/knitr.png" width="20%" height="20%" style="display: block; margin: auto;" /> ] --- background-image: url(img/rmarkdown.png) background-position: 95% 8% background-size: 7% class: inverse, middle, center # Exercises ### We will create an example HTML report using the R Markdown template provided by RStudio --- background-image: url(img/rmarkdown.png) background-position: 95% 12% background-size: 7% class: left, top # Exercise 1: create a new RMarkdown file .leftcol[ ### Click on .red[File] > ### then .red[New File] > ### then .red[R Markdown] ] .rightcol[ ***Or use the drop-down menu*** <img src="img/new-rmarkdown.png" width="30%" height="30%" style="display: block; margin: auto;" /> ] --- background-image: url(img/rmarkdown.png) background-position: 93% 7% background-size: 7% class: left, top # Install required packages .leftcol[ If you're in a fresh RStudio.Cloud session, you *might* be asked to install the required packages for R Markdown, Click **Yes** .border[ <img src="img/install-req-packages.png" width="80%" height="80%" /> ]] -- .rightcol[ You will see RStudio installing the packages in the **Jobs** pane .border[ <img src="img/rmd-pckgs-jobs.png" width="180%" height="180%" /> ]] --- background-image: url(img/rmarkdown.png) background-position: 93% 7% background-size: 7% class: middle # New R Markdown File ### Enter ***'Title'*** and ***'Author'*** of your report and click ***OK*** .border[ <img src="img/rmarkdown-defaults.png" width="40%" height="40%" style="display: block; margin: auto;" /> ] --- background-image: url(img/rmarkdown.png) background-position: 93% 7% background-size: 7% class: left, top # Save your `.Rmd` file ### Click on the small floppy disk, enter a name (with .Rmd extension), and save your `.Rmd` file <img src="img/save-rmd-file.png" width="45%" height="45%" style="display: block; margin: auto;" /> --- background-image: url(img/rmarkdown.png) background-position: 93% 7% background-size: 7% class: left, top # Knit your .Rmd file ### Click on the small gear, select .blue[Preview in Viewer Pane] ### Click on the knit icon (ball of yarn) <img src="img/knit-rmd-file.png" width="40%" height="40%" style="display: block; margin: auto;" /> --- background-image: url(img/rmarkdown.png) background-position: 93% 7% background-size: 7% class: left, center ## Our First R Markdown Report! .border[ <img src="img/rmarkdown-in-action.png" width="75%" height="75%" style="display: block; margin: auto;" /> ] --- background-image: url(img/rmarkdown.png) background-position: 95% 8% background-size: 7% class: inverse, middle, center # How R Markdown Works ## (under the hood) --- background-image: url(img/how-rmd-works.png) background-position: 50% 73% background-size: 63% class: top, center # R Markdown is made up of three elements --- class: left, top # Rmarkdown combines metadata, markdown, *and* R code .leftcol[ <img src="img/r-plus-md.png" width="90%" height="90%" style="display: block; margin: auto;" /> ] .rightcol[ `.yaml` = Metadata `.md` = Prose `.R` = Code ] <br><br><br><br> > The result is a file framework for creating reproducible reports using YAML, Markdown, and computer code --- background-image: url(img/rmarkdown.png) background-position: 93% 10% background-size: 7% # R Markdown: YAML ## `.yaml` = Metadata ### ~~`.md` = Prose~~ ### ~~`.R` = Code~~ -- ### YAML is a human friendly data serialization standard for all programming languages. -- ### YAML stands for *'YAML Ain't Markup Language'* (funny, huh?) --- background-image: url(img/rmarkdown.png) background-position: 93% 10% background-size: 7% # R Markdown: YAML <br><br> ### YAML contains the information about the document we're going to create ```yaml --- title: "Monthly Report" author: "Martin Frigaard" date: "10/27/2020" output: html_document --- ``` --- background-image: url(img/rmarkdown.png) background-position: 93% 10% background-size: 7% # R Markdown: YAML format <br><br> <img src="img/yaml-formatting-01.png" width="70%" height="70%" style="display: block; margin: auto;" /> --- background-image: url(img/rmarkdown.png) background-position: 93% 10% background-size: 7% # R Markdown: YAML format <br><br> <img src="img/yaml-formatting-02.png" width="60%" height="60%" style="display: block; margin: auto;" /> --- background-image: url(img/rmarkdown.png) background-position: 93% 10% background-size: 7% # R Markdown: YAML <br><br> ### There are many YAML arguments and options ### _Indentation matters in YAML!!_ -- > Check out the [YAML Fieldguide](https://cran.r-project.org/web/packages/ymlthis/vignettes/yaml-fieldguide.html) for a comprehensive list --- background-image: url(img/rmarkdown.png) background-position: 93% 7% background-size: 7% # Example YAML output options ## Table of contents: ### `toc:` logical (`true` or `false`) ### `toc_float:` logical (`true` or `false`) ### `toc_depth:` set numerically `0` - `6` --- background-image: url(img/rmarkdown.png) background-position: 7% 93% background-size: 7% # Exercise 2: create a floating table of contents ### Change the `output` in the YAML header to the following: .leftcol40[ ```yaml output: html_document: toc: yes toc_float: true ``` ] -- .rightcol60[.center[ #### *Knit the document again* ]] --- background-image: url(img/rmarkdown.png) background-position: 7% 96% background-size: 7% # YAML output options: table of contents ### Floating table of contents (rendered) <img src="img/yaml-toc-settings.png" width="100%" height="100%" /> --- background-image: url(img/rmarkdown.png) background-position: 7% 96% background-size: 7% # Exercise 3: text highlighting and themes ### Add the following two options to your YAML header .leftcol40[ ```yaml output: html_document: toc: yes toc_float: yes highlight: zenburn theme: united ``` ] -- .rightcol60[.center[ #### *Knit the document again* ]] --- background-image: url(img/rmarkdown.png) background-position: 7% 96% background-size: 7% # YAML: text highlighting and theme options ### Text highlighting and theme options (rendered) <img src="img/yaml-highlight-theme.png" width="70%" height="70%" style="display: block; margin: auto;" /> --- background-image: url(img/rmarkdown.png) background-position: 7% 96% background-size: 7% # More YAML options ### You can change the YAML contents using the settings (small gear) <img src="img/knitr-settings.png" width="60%" height="60%" style="display: block; margin: auto;" /> --- background-image: url(img/rmarkdown.png) background-position: 7% 96% background-size: 7% # Edit R Markdown Document Options .leftcol[ #### This window gives us the ability to manually change some of the YAML settings (but not all of them!) ] -- .rightcol[ <img src="img/edit-rmd-output-options.png" width="80%" height="80%" style="display: block; margin: auto auto auto 0;" /> ] --- background-image: url(img/rmarkdown.png) background-position: 7% 96% background-size: 7% # YAML Parameters ### YAML parameters can be referred to throughout the document .leftcol[ ***Create `params` in YAML header*** ```yaml params: param1: x param2: y data: df ``` ] -- .rightcol[ ***Refer to `params` in .Rmd document*** ```r params$param1 params$param2 params$data ``` ] --- background-image: url(img/rmarkdown.png) background-position: 7% 96% background-size: 7% # Exercise 4: Using YAML parameters .leftcol40[ #### *Add the following `params` option in the YAML header* .small[ ```yaml params: small_pressure: !r head(pressure) ``` ]] -- .rightcol60[ #### *Add this code to the end of the document* <br> .border[ <img src="img/small-pressure.png" width="100%" height="100%" style="display: block; margin: auto auto auto 0;" /> <br><br> #### *Knit the document again!* ]] --- background-image: url(img/rmarkdown.png) background-position: 7% 96% background-size: 7% ## See our new plot with the `params` We can see the new plot with the reduced sample size .border[ <img src="img/params-plot.png" width="60%" height="60%" style="display: block; margin: auto;" /> ] --- background-image: url(img/rmarkdown.png) background-position: 93% 10% background-size: 7% # YAML output formats Function | Outputs -------------- | -------------------------- `html_document()` | HTML document `pdf_document()` | PDF document `word_document()` | Word document `odt_document()` | ODT document `rtf_document()` | RTF document `md_document()` | Markdown document `slidy_presentation()` | Slidy Slides (HTML) `beamer_presentation()` | Beamer Slides (PDF) `ioslides_presentation()` | ioslides (HTML) `powerpoint_presentation()` | PowerPoint (pptx) --- background-image: url(img/rmarkdown.png) background-position: 93% 10% background-size: 15% # R Markdown ### ~~`.yaml` = Metadata~~ ## `.md` = Prose ### ~~`.R` = Code~~ --- background-image: url(img/markdown.png) background-position: 93% 10% background-size: 7% class: left, top # Basic Markdown Syntax .leftcol30[.large[ #### Italics & Bold ```md *italic* **bold** _italic_ __bold__ ``` ]] -- .rightcol70[.center[.large[ *italic* **bold** _italic_ __bold__ ]]] --- background-image: url(img/markdown.png) background-position: 93% 10% background-size: 7% class: left, top # Basic Markdown Syntax .leftcol[.large[ #### Headers ```md # Header 1 ## Header 2 ### Header 3 ``` ]] -- .rightcol[.large[.center[ # Header 1 ## Header 2 ### Header 3 ]]] --- background-image: url(img/markdown.png) background-position: 93% 10% background-size: 7% class: left, top # Basic Markdown Syntax .leftcol40[ #### Bullets & Numeric Lists ```md * Item 1 * Item 2 + Item 2a + Item 2b 1. Item 1 2. Item 2 ``` ] -- .rightcol60[.medium[ * Item 1 * Item 2 + Item 2a + Item 2b 1. Item 1 2. Item 2 ]] --- background-image: url(img/markdown.png) background-position: 93% 10% background-size: 7% class: left, top # Basic Markdown Syntax .leftcol55[ #### Hyperlinks ```md https://www.biomarin.com/ [linked phrase](https://www.biomarin.com/) ``` ] -- .rightcol45[ #### *becomes...* .center[.large[ [linked phrase](https://www.biomarin.com/) ]]] --- background-image: url(img/markdown.png) background-position: 93% 10% background-size: 15% class: left, top # Basic Markdown Syntax .leftcol55[ #### Images .small[ ```md ![](https://www.r-project.org/logo/Rlogo.png) ![optional caption](https://www.r-project.org/logo/Rlogo.png) ``` ]] -- .rightcol45[ #### *becomes...* <img src="https://www.r-project.org/logo/Rlogo.png" width="30%" height="30%" style="display: block; margin: auto;" /> ] --- background-image: url(img/markdown.png) background-position: 93% 10% background-size: 7% class: left, top # Basic Markdown Syntax .leftcol[ #### Math Equations ```md $equation$ $$ equation $$ ``` ] -- <br> .rightcol[.large[ `\(equation\)` $$ equation $$ ]] --- background-image: url(img/markdown.png) background-position: 93% 10% background-size: 7% class: left, top # Basic Markdown Syntax .leftcol45[ #### Super scripts & Strike-through ```md superscript^2^ ~~strikethrough~~ ``` ] -- .rightcol55[.center[ superscript^2^ ~~strikethrough~~ ]] --- background-image: url(img/rmarkdown.png) background-position: 7% 96% background-size: 7% # Exercise 5: Markdown Formatting Delete the top portion of the markdown in `01-monthly-report.Rmd`. <img src="img/delete-rmarkdown.png" width="70%" height="70%" style="display: block; margin: auto;" /> --- background-image: url(img/rmarkdown.png) background-position: 7% 96% background-size: 7% # Exercise 5: Markdown Formatting .leftcol[ ### Add the text below to your report > *This is a monthly report generated with RMarkdown, a literate programming tool for combining text and code.* ] -- .rightcol[ ### Include the following formatting: 1. make `monthly report` italic 2. include this hyperlink for `Rmarkdown`: https://rmarkdown.rstudio.com/ 3. format `code` as code. ] -- #### *Knit the document when you're finished* --- background-image: url(img/rmarkdown.png) background-position: 7% 96% background-size: 7% # Exercise 5: Markdown Formatting (rendered) <img src="img/rmarkdown-exercise.png" width="90%" height="90%" style="display: block; margin: auto;" /> --- background-image: url(img/rmarkdown.png) background-position: 93% 10% background-size: 7% # Exercise 6: Tabsets Remove the `toc` and `toc_float` options from your YAML header ```yaml output: html_document: highlight: zenburn theme: united params: small_pressure: !r head(pressure) ``` --- background-image: url(img/rmarkdown.png) background-position: 93% 10% background-size: 7% # Exercise 6: Tabsets Make the following changes to the `R Markdown` header sections <img src="img/tabset-markdown.png" width="85%" height="85%" /> ### Knit the document again --- background-image: url(img/rmarkdown.png) background-position: 93% 10% background-size: 7% # Exercise 6: Tabsets (rendered) <img src="img/tabset-1-render.png" width="80%" height="80%" style="display: block; margin: auto;" /> --- background-image: url(img/rmarkdown.png) background-position: 93% 10% background-size: 7% # Exercise 6: Tabsets (rendered) <img src="img/tabset-2-render.png" width="80%" height="80%" style="display: block; margin: auto;" /> --- background-image: url(img/rmarkdown.png) background-position: 93% 10% background-size: 12% # R Markdown ### ~~`.yaml` = Metadata~~ ### ~~`.md` = Prose~~ ## `.R` = Code --- background-image: url(img/rmarkdown.png) background-position: 93% 10% background-size: 7% # Code chunks (`setup`) ### The first bit of R code in our RMarkdown file is the `setup` chunk <img src="img/setup-code-chunk.png" width="100%" height="100%" /> -- ### Chunks named '`setup`' are special because they can set global options -- ### '`include=FALSE`' means this code is run, but not displayed --- background-image: url(img/rmarkdown.png) background-position: 93% 10% background-size: 7% # Code chunks (`setup`) ### R Markdown document options come from the `knitr` package ### We can access both the with sytnax below: <img src="img/knitr-opts-set.png" width="75%" height="75%" style="display: block; margin: auto;" /> --- background-image: url(img/rmarkdown.png) background-position: 93% 10% background-size: 7% # Code chunks (`setup`) ### The `echo=TRUE` option controls whether we want to display the code in the code chunk -- ### Other common options regarding code are `eval`, `tidy`, `error`, `message`, and `warning` -- ### Advanced options can control language engines (`engine`), caching (`cache`, `dependson`), and plot animations (`fig.show`) --- background-image: url(img/rmarkdown.png) background-position: 93% 10% background-size: 7% # Code chunks (`setup`) ### Many options for code chunks |Option | Document Effect | |:-------------- |:-------------------------------------------- | |`include` | run code, but do/don't print code and results| |`eval` | do/don't evaluate the code | |`echo` | run code, but don't print code | |`message` | do/don't print messages (e.g. from functions)| |`warning` | do/don't print warnings | --- background-image: url(img/rmarkdown.png) background-position: 93% 10% background-size: 7% # Code chunks <br><br> .border[ <img src="img/code-chunks.png" width="70%" height="70%" style="display: block; margin: auto;" /> ] --- background-image: url(img/rmarkdown.png) background-position: 93% 10% background-size: 7% # Code chunk fences <br><br> <img src="img/code-chunk-fences.png" width="70%" height="70%" style="display: block; margin: auto;" /> --- background-image: url(img/rmarkdown.png) background-position: 93% 10% background-size: 7% # Code chunk names and arguments <img src="img/code-chunk-name-args.png" width="60%" height="60%" style="display: block; margin: auto;" /> > See the [knitr web page](https://yihui.name/knitr/options/) for complete list of options --- background-image: url(img/rmarkdown.png) background-position: 93% 10% background-size: 7% # Inserting code chunks ### Use keyboard shortcuts **CMD/CTRL + I** or **ALT/OPTION + I** <img src="img/insert-code-chunk.png" width="18%" height="18%" style="display: block; margin: auto;" /> --- background-image: url(img/rmarkdown.png) background-position: 93% 10% background-size: 7% # Edit code chunk options ### You can edit code chunk options using the icon (small gear) <img src="img/code-chunk-settings.png" width="40%" height="40%" style="display: block; margin: auto;" /> --- background-image: url(img/rmarkdown.png) background-position: 93% 10% background-size: 7% # Code Chunk Engines ## More and more code engines all the time ```r names(knitr::knit_engines$get()) ``` ``` [1] "awk" "bash" "coffee" "gawk" "groovy" "haskell" [7] "lein" "mysql" "node" "octave" "perl" "psql" [13] "Rscript" "ruby" "sas" "scala" "sed" "sh" [19] "stata" "zsh" "highlight" "Rcpp" "tikz" "dot" [25] "c" "cc" "fortran" "fortran95" "asy" "cat" [31] "asis" "stan" "block" "block2" "js" "css" [37] "sql" "go" "python" "julia" "sass" "scss" [43] "R" "bslib" "targets" ``` --- background-image: url(img/rmarkdown.png) background-position: 7% 96% background-size: 7% # Exercise 7: code chunks (kable) .leftcol45[ #### Create a new `Tables` level three header under the `Summary` heading, .small[ ```md ### Tables ``` ]] .rightcol55[ #### Insert the following code chunk under `Tables` <br> <img src="img/kable-code-chunk.png" width="100%" height="100%" style="display: block; margin: auto;" /> ] <br><br><br><br><br><br><br><br> > *insert the code block manually with the keyboard short-cut, or use the "Insert" button* --- background-image: url(img/rmarkdown.png) background-position: 93% 10% background-size: 7% # Exercise 7: code chunks (kable rendered) We can see the `small_pressure` parameter from the YAML has been rendered in the new `Tables` tab. `kable` tables are great for presenting small, summary tables. <img src="img/kable-chunk-rendered.png" width="82%" height="82%" style="display: block; margin: auto;" /> Read more about `kable` table options [here](https://bookdown.org/yihui/rmarkdown-cookbook/kable.html) --- background-image: url(img/rmarkdown.png) background-position: 93% 7% background-size: 6% # Exercise 8: code chunks (paged) #### We are going to repeat the process above, but with a larger table (`mtcars`) Insert the following code chunk above the `knitr::kable()` output: .border[ <img src="img/paged-code-chunk.png" width="80%" height="80%" style="display: block; margin: auto;" /> ] #### Knit the document --- background-image: url(img/rmarkdown.png) background-position: 7% 96% background-size: 7% # Exercise 8: code chunk (paged rendered) ### Paged tables are great for larger datasets <img src="img/paged-chunk-render.png" width="75%" height="75%" style="display: block; margin: auto;" /> --- background-image: url(img/rmarkdown.png) background-position: 93% 10% background-size: 7% # Exercise 8: paged tables <img src="img/paged-chunk-render.gif" width="70%" height="70%" style="display: block; margin: auto;" /> --- background-image: url(img/rmarkdown.png) background-position: 93% 10% background-size: 7% # Inline R Code ### R Markdown also supports inline R code -- <img src="img/inline-r-code-01.png" width="70%" height="70%" style="display: block; margin: auto;" /> --- background-image: url(img/rmarkdown.png) background-position: 93% 10% background-size: 7% # Inline R Code ### Inline R code allows us to include summaries of our analysis in the report -- <img src="img/inline-r-code-02.png" width="80%" height="80%" style="display: block; margin: auto;" /> --- background-image: url(img/rmarkdown.png) background-position: 93% 10% background-size: 7% # Exercise 9: Add Inline Code <br> ### We're going to add a Pearson correlation between speed and stopping distance .leftcol[ #### Include the following code under the `Summary` level three header ] -- .rightcol[ > The correlation between speed and > stopping distance is > 0.8068949 ] -- <br><br><br><br><br> .center[ #### Knit the document again ] --- background-image: url(img/rmarkdown.png) background-position: 93% 10% background-size: 7% # Exercise 9: Add Inline Code (rendered) <img src="img/inline-code-rendered.png" width="80%" height="80%" style="display: block; margin: auto;" /> --- background-image: url(img/rmarkdown.png) background-position: 93% 50% background-size: 20% # Make cool stuff in R Markdown! ## `bookdown` ## `blogdown` ## these slides! --- background-image: url(img/rmarkdown.png) background-position: 93% 10% background-size: 7% class: left, top # Resources <br> - **YAML**: check out the [`ymlthis` package](https://r-lib.github.io/ymlthis/) for tools and documentation for working with YAML - **Markdown**: [Commonmark](https://commonmark.org/help/tutorial) has a quick ten-twenty minute tutorial on markdown. - [R Markdown](https://bookdown.org/yihui/rmarkdown/): A comprehensive but friendly introduction to R Markdown and friends. Free online! - [R for Data Science](http://r4ds.had.co.nz/): A comprehensive but friendly introduction to the tidyverse. Free online. - [R Markdown for Scientists](https://rmd4sci.njtierney.com/): R Markdown for Scientists workshop material