class: title-slide, bottom,center background-image: url(img/title_bkgd.png) background-position: top center background-size: 100% ## Introduction to RMarkdown ### Ziwei Ma Department of Mathematics 2022-09-12 [
@ziwei-utc](https://github.com/ziwei-utc) [
@ZiweiMa](http://twitter.com/ZiweiMa) [
https://ziwei-utc.github.io/](https://ziwei-utc.github.io/) .pull-right[ ] --- name: about-me class: middle, center, inverse # About me <img style="border-radius: 50%;" src="img/ziwei.png" width="200px"/> ### Ziwei Ma #### Assistant Professor of Statistics [
@ziwei-utc](https://github.com/ziwei-utc) [
@ZiweiMa](http://twitter.com/ZiweiMa) [
https://ziwei-utc.github.io](https://ziwei-utc.github.io/) --- class: center, middle # Let's get to know each other -- ## 👍 if you consider yourself an `R` and/or `R Markdown` beginner -- ## ❤️ if you are comfortable or very comfortable with `R` and `R Markdown` --- class: inverse #
# Today we will cover: -- ### What is RMarkdown (Rmd) -- ### Why/when is it useful -- ### What can be done in Rmd -- ### How to use it (make your first Rmd file) --- class: inverse # What is RMarkdown? -- - > "R Markdown is an **<span style='color: red;'>authoring syntax</span>** that combines the ease of *<span style='color: lightblue;'>Markdown</span>* with the *<span style='color: lightblue;'>statistical programming language R</span>*." >Baumer, B., & Udwin, D. (2015). R markdown. Wiley Interdisciplinary Reviews: Computational Statistics, 7(3), 167-177. -- - **[A brief history of RMarkdown](https://slides.yihui.org/2021-Brazilian-R-Day.html#1)** - [Literate programming](https://en.wikipedia.org/wiki/Literate_programming) -- - ### RMarkdown workflow <img src="img/rmarkdown_workflow.png" width="35%" style="display: block; margin: auto;" /> --- class: top, center <video width="1530" height="610" controls> <source src="img/What is R Markdown.mp4" type="video/mp4"> </video> --- class: top, center <img src="img/overview.png" width="90%" style="display: block; margin: auto;" /> --- class: top # What purposes is Rmd for? (according to Hadley Wickham who is the Chief Scientist at Rstudio and 2019 COPSS Presidents' Award recipients) -- - ### **Communication** with decision makers -- - ### **Collaboration** with other data scientists -- - ### Modern **notepad** - reproducibility ([A nice presentation for reproducible scicence](https://oliviergimenez.github.io/reproducible-science-workshop/slides/0_introduction.html#1)) --- # Advantages: Why using Rmd? - ### Better comprehensibility - ### Ease of usage - "on the way"-usage (documentation) - easy to learn - ### Power of usage - multiple occassions - multiple output-formats (+ possibility to switch) - compatibility with other "languages" --- # When is Rmd **not** recommended? - ### In general: The less need for communication the less is Rmd recommended --- # What can be done **
Tip: ** It is better to start with a template then customize it. There are many templates available in RStudio and more from internet, like github. .panelset.sideways[ .panel[.panel-name[Academic papers] <img src="img/example_papers.png" width="90%" height="90%" /> **
Tip: ** It is better to start with a template then customize it. ] .panel[.panel-name[Presentations] **NOTE** You may use a single Rmd file to create your presentations in difference format, like beamer, ppt, html and more. A nice talk ["One R Markdown Document, Fourteen Demos "](https://www.youtube.com/watch?v=qLEkUjxk7e8&t=755s) <img src="img/example-beamer.png" width="90%" height="90%" /> ] .panel[.panel-name[Poster] <img src="img/example-poster.png" width="90%" height="90%" /> ] .panel[.panel-name[Professional websites] <img src="img/example_professional_site.png" width="90%" height="90%" /> ] .panel[.panel-name[Visual blogs] <img src="img/example_visual_blog.png" width="90%" height="90%" /> ] .panel[.panel-name[Books] ## **A real example** [R Markdown: The Definitive Guide](https://bookdown.org/yihui/rmarkdown/) <img src="img/example_book.png" width="90%" height="90%" /> ] .panel[.panel-name[And more] See some examples of Rmd-applications and possible output formats at the [R-Studio Gallery](https://rmarkdown.rstudio.com/gallery.html) <img src="img/R-Gallery.png" width="90%" height="90%" /> ] ] --- # Make your first RMarkdown - ## **Requirement** -- - ### Option 1 #### Install R and RStudio -- - ### Option 2 #### Sign up a Rstudio Cloud account [https://rstudio.cloud/](https://rstudio.cloud/) --- # Getting Started in RStudio >RStudio is what is called an Integrated Development Environment for R. It is dependent on R but separate from it. There are several ways of using R but R Studio is arguably the most popular and convenient. It’s time to open it up and get started! When you first open up RStudio, this is what you should see: <img src="img/rstudio.png" width="75%" height="75%" /> --- ## An intutitive introdution about RStudio (*[Image source](https://bookdown.org/animestina/intro_stats_rms/getting-started-in-rstudio.html#r-as-an-interactive-envrionment)*) <img src="img/Understanding_R.jpeg" width="75%" height="75%" /> --- # Structure of a R Markdown document .center[] --- class: inverse, middle # Demo - ## Open RStudio - ## Create a new Rmd file - ## Identify 3 components (frontmatter, content, chunks) --- .center[] --- .center[] --- # Structure of a R Markdown document .center[] --- # Structure of a R Markdown document .center[] --- class: inverse, center, middle # Format your text with Markdown --- # What is Markdown? - A way to write stuff - Mostly plain words, with some formating <img src="img/md.png" width="75%" height="75%" /> --- # Headers - Use # to create headers - Multiple #'s create lower level headers <img src="img/headers.png" width="75%" height="75%" /> --- # Text - Text is rendered as plain text <img src="img/text.png" width="75%" height="75%" /> --- # Lists - Use asterisks to make bullet points - Use numbers to make numbered lists - Use 4 spaces or 1 tab for indentation <img src="img/lists.png" width="75%" height="75%" /> --- # Hyperlinks - To add a hyperlink, put your text between brackets - Then place the URL between parentheses <img src="img/url.png" width="75%" height="75%" /> --- # Equations - Write equations with Latex syntax <img src="img/eqns.png" width="75%" height="75%" /> --- # Equation blocks <img src="img/eqns2.png" width="75%" height="75%" /> --- # Images - Use a link preceded by an ! to insert an image - The link text should be - a URL if the image is hosted online - a file path if the image is saved on your computer <img src="img/images.png" width="75%" height="75%" /> --- # Tables ```md | header A | header B | |---------:|:---------| | data | data | Table:title ``` - A bit of a hassle... - Use a [generator of Tables for Markdown](https://www.tablesgenerator.com/markdown_tables) - Wait for next section to learn how to generate tables from `R` --- # R Markdown Reference Guide <https://rstudio.com/wp-content/uploads/2015/03/rmarkdown-reference.pdf> <img src="img/refguide.png" width="75%" height="75%" /> --- # Structure of a R Markdown document .center[] --- # Structure of a R Markdown document .center[] --- class: inverse, center, middle ## Code, figures and tables --- # Embed code - Insert chunk of `R` code - R Markdown runs code and includes results. <img src="img/embed.png" width="75%" height="75%" /> --- # Inline code - Place code in a sentence with - R Markdown replaces code with results <img src="img/inline.png" width="75%" height="75%" /> --- # Chunk options - By default, R markdown includes both code and results <img src="img/chunk_options.png" width="75%" height="75%" /> --- # echo - Add options between brackets after `r`. - `echo = FALSE` hides code. <img src="img/echo.png" width="75%" height="75%" /> --- # echo - Add options between brackets after `r`. - `echo = FALSE` hides code. <img src="img/echo2.png" width="75%" height="75%" /> --- # eval - `eval = FALSE` prevents code from being run - No results is displayed, only code <img src="img/eval.png" width="75%" height="75%" /> --- # fig.height, fig.width - Specify dimension of plots (in inches) with fig.width and fig.height - Separate multiple arguments with commas. <img src="img/figheightfigwidth.png" width="75%" height="75%" /> --- # message - `message = FALSE` prevents messages from appearing in output <img src="img/message.png" width="75%" height="75%" /> --- # Default chunk options - Repeating chunk options can be painful - If you have `echo = FALSE` in every single chunk, how to set the default chunk option to `echo = FALSE` ? - Use `knitr::opts_chunk$set(echo = FALSE)` - You may overwrite the default for each chunk - For chunk options, check out <https://yihui.name/knitr/options/> --- # Including tables ```r # cars is a built-in-to-R data set of cars # and their stopping distances cars %>% head(5) %>% knitr::kable(format = "html", caption = "A kable table") ``` <table> <caption>A kable table</caption> <thead> <tr> <th style="text-align:right;"> speed </th> <th style="text-align:right;"> dist </th> </tr> </thead> <tbody> <tr> <td style="text-align:right;"> 4 </td> <td style="text-align:right;"> 2 </td> </tr> <tr> <td style="text-align:right;"> 4 </td> <td style="text-align:right;"> 10 </td> </tr> <tr> <td style="text-align:right;"> 7 </td> <td style="text-align:right;"> 4 </td> </tr> <tr> <td style="text-align:right;"> 7 </td> <td style="text-align:right;"> 22 </td> </tr> <tr> <td style="text-align:right;"> 8 </td> <td style="text-align:right;"> 16 </td> </tr> </tbody> </table> - The [`kable`](https://www.rdocumentation.org/packages/knitr/versions/1.21/topics/kable) package is often used with the [`kableExtra`](https://cran.r-project.org/web/packages/kableExtra/vignettes/awesome_table_in_html.html) package - A number of other packages are available for making pretty tables, see [rmarkdown.rstudio.com](https://rmarkdown.rstudio.com/lesson-7.html) --- # R Markdown Reference Guide <https://rstudio.com/wp-content/uploads/2015/03/rmarkdown-reference.pdf> <img src="img/knitr.png" width="75%" height="75%" /> --- # Structure of a R Markdown document .center[] --- # Structure of a R Markdown document .center[] --- class: inverse, center, middle # YAML Yet Another Markup Language --- # YAML in brief - Contains the metadata of the document - Starts and ends by three dashes - Comes first in the document --- # Simplest example .center[] --- # Output formats .center[] --- # Output formats .center[] --- # Managing bibliography -- <ol> <li> Put references in a plain text file with the extension **.bib**, in **BibTex** format (my advice: use [Zotero](https://www.zotero.org)) </ol> ```bibtex *@article{Shea2014, author = {Shea, Nicholas and Boldt, Annika}, journal = {Trends in Cognitive Sciences}, pages = {186--193}, title = {{Supra-personal cognitive control}}, volume = {18}, year = {2014}, doi = {10.1016/j.tics.2014.01.006}, } ``` -- <ol start = 2> <li>Reference this file in your YAML header </ol> ```yaml --- title: "Citation test" *bibliography: example.bib output: html_document --- ``` --- # Citations <ol start = 3> <li>In your text, citations go inside brackets and separated by semicolons </ol> -- .pull-left[ ### This... Blah blah [@Shea2014; @Lottridge2012]. ] .pull-right[ ### turns into this... Blah blah (Shea et al. 2014; Lottridge et al. 2012). ] --- class: larger-body # Citations <ol start = 3> <li>In your text, citations go inside brackets and separated by semicolons </ol> .pull-left[ ### This... Blah blah [@Shea2014; @Lottridge2012]. @Shea2014 says blah. Blah blah [see @Shea2014, pp. 33-35; also @Wu2016, ch. 1]. ] .pull-right[ ### turns into this... Blah blah (Shea et al. 2014; Lottridge et al. 2012). Shea et al. (2014) says blah. Blah blah (see Shea et al. 2014, 33–35; also Wu 2016, ch. 1). ] --- class: larger-body # Citations <ol start = 3> <li>In your text, citations go inside brackets and separated by semicolons </ol> .pull-left[ ### This... Blah blah [@Shea2014; @Lottridge2012]. @Shea2014 says blah. Blah blah [see @Shea2014, pp. 33-35; also @Wu2016, ch. 1]. ] .pull-right[ ### turns into this... Blah blah (Shea et al. 2014; Lottridge et al. 2012). Shea et al. (2014) says blah. Blah blah (see Shea et al. 2014, 33–35; also Wu 2016, ch. 1). ] - Add e.g `csl: my-style.csl` in the YAML header to change to other formats - Browse through and download styles at [zotero.org/styles](https://www.zotero.org/styles) --- # Citations For an easy way to insert citations, try the [`citr`](https://github.com/crsh/citr) RStudio add-in. <img src="https://raw.githubusercontent.com/crsh/citr/master/tools/images/addin_demo.gif" width="75%" /> --- # R Markdown Reference Guide <https://rstudio.com/wp-content/uploads/2015/03/rmarkdown-reference.pdf> .center[] --- .center[]